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
19
20
# File 'lib/lab42/stream/higher_order.rb', line 11

def __combine__ op, *streams
  # TODO: Decide if we can continue if one of the streams is empty iff op.arity < 0
  #       for now no!
  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



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

def combine *streams_and_op, &operation
  op = streams_and_op.pop unless self.class === streams_and_op.last
  op = operation.make_behavior op
  # TODO: Decide what to do if op.arity and streams_and_op.size.succ do not match????
  __combine__( op, *streams_and_op )
end

#split_into(n) ⇒ Object



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

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