Module: Enumerable

Included in:
LinkedListDelegator, Pipe, TreeDelegator, EnumerableOperator::Diagonal, EnumerableOperator::Product, EnumerableOperator::Sum
Defined in:
lib/agents/sets/enum/by.rb,
lib/agents/sets/enum/nest.rb,
lib/agents/sets/enum/pipe.rb,
lib/agents/sets/enum/tree.rb,
lib/agents/sets/enum/cluster.rb

Defined Under Namespace

Classes: ByBreadthDelegator, ByDepthDelegator, LinkedListDelegator, Pipe, TreeDelegator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nest(items_left, get_cur, go_next, compare) ⇒ Object

Handles a single level, recursing when the depth increases and backing out when the depth decreases.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/agents/sets/enum/nest.rb', line 25

def Enumerable.nest items_left, get_cur, go_next, compare
  # should handle compare.arity == 2 like a <=> proc
  result = []; item = depth = nil
  while items_left[]
    item = get_cur[]
    depth = compare[item]
    base_depth ||= depth
      
    if depth < base_depth
      break
    elsif depth > base_depth
      result << nest(items_left, get_cur, go_next, compare)
    else
      result << item; go_next[]
    end
  end
  return result
end

Instance Method Details

#each_cluster(n = 2) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/agents/sets/enum/cluster.rb', line 5

def each_cluster n = 2
  tuple = [nil] * n
  
  count = n-1
  each { |x|
    tuple.shift
    tuple.push x
    if count == 0
      yield tuple
    else
      count -= 1
    end
  }
end

#each_with_neighbors(n = 1, empty = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/agents/sets/enum/cluster.rb', line 20

def each_with_neighbors n = 1, empty = nil
  nbrs = [empty] * (2 * n + 1)
  offset = n
  
  each { |x|
    nbrs.shift
    nbrs.push x
    if offset == 0  # offset is now the offset of the first element, x0,
      yield nbrs    #   of the sequence from the center of nbrs, or 0,
    else            #   if x0 has already passed the center.
      offset -= 1
    end
  }
  
  n.times {
    nbrs.shift
    nbrs.push empty
    if offset == 0
      yield nbrs
    else
      offset -= 1
    end
  }
      
  self
end

#group(&test) ⇒ Object



44
45
46
# File 'lib/agents/sets/enum/nest.rb', line 44

def group(&test)
  nest { |x| test[x] ? 1 : 0 }
end

#nest(&compare) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/agents/sets/enum/nest.rb', line 5

def nest(&compare)
  ary = to_a
  s = ary.size
  i = 0
  
  # wrap into Array::Iterator?
  items_left  = proc { i < s }
  get_cur     = proc { ary[i] }
  go_next     = proc { i += 1 }
  
  result = nil
  while items_left[]
    level_ary = Enumerable.nest items_left, get_cur, go_next, compare
    result = result ? level_ary.unshift(result) : level_ary
  end
  result || []
end

#pipe(filter_spec = nil, *args, &filter_proc) ⇒ Object



54
55
56
# File 'lib/agents/sets/enum/pipe.rb', line 54

def pipe filter_spec = nil, *args, &filter_proc
  Pipe.new self, filter_spec, *args, &filter_proc
end