Module: Lab42::Stream::HigherOrder

Included in:
Lab42::Stream
Defined in:
lib/lab42/stream/higher_order.rb

Instance Method Summary collapse

Instance Method Details

#__combine__(op, *streams) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/lab42/stream/higher_order.rb', line 11

def __combine__ op, *streams
  return empty_stream if streams.any?( &:empty? )
  values = streams.map( &:head )
  new_head = op.(head, *values)
  cons_stream( new_head ){
    tail.__combine__( op, *streams.map( &:tail ) )
  }
end

#combine(*streams_and_op, &operation) ⇒ Object



5
6
7
8
9
# File 'lib/lab42/stream/higher_order.rb', line 5

def combine *streams_and_op, &operation
  op = streams_and_op.pop unless self.class === streams_and_op.last
  op = Behavior.make1( op, &operation )
  __combine__( op, *streams_and_op )
end

#split_into(n) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/lab42/stream/higher_order.rb', line 20

def split_into n
  indexed = with_index
  n.times.map do | i |
    indexed
      .filter{ |_, idx| idx % n == i }
      .map( :first )
  end
end