Module: Kernel

Defined in:
lib/lab42/stream/empty.rb,
lib/lab42/stream/kernel.rb,
lib/lab42/stream/auto_import.rb,
lib/lab42/stream/kernel/extensions.rb

Overview

class Empty

Instance Method Summary collapse

Instance Method Details

#binop_streams(op, stream1, stream2) ⇒ Object



3
4
5
6
7
# File 'lib/lab42/stream/kernel.rb', line 3

def binop_streams op, stream1, stream2
  combine_streams stream1, stream2 do |e1, e2|
    e1.send op, e2
  end
end

#combine_streams(stream, *streams_and_ops, &operation) ⇒ Object



9
10
11
# File 'lib/lab42/stream/kernel.rb', line 9

def combine_streams stream, *streams_and_ops, &operation
  stream.combine( *streams_and_ops, &operation )
end

#cons_stream(head, &tail) ⇒ Object



13
14
15
# File 'lib/lab42/stream/kernel.rb', line 13

def cons_stream head, &tail
  Lab42::Stream.new head, tail
end

#cons_stream_n(first_head, *more_heads, &tail) ⇒ Object



17
18
19
20
21
22
# File 'lib/lab42/stream/kernel.rb', line 17

def cons_stream_n first_head, *more_heads, &tail
  return cons_stream( first_head, &tail ) if more_heads.empty?
  return cons_stream( first_head ){
    cons_stream_n( *more_heads, &tail )
  }
end

#const_stream(const) ⇒ Object



24
25
26
# File 'lib/lab42/stream/kernel.rb', line 24

def const_stream const
  c = cons_stream( const ){ c }
end

#cyclic_stream(*args) ⇒ Object



28
29
30
31
32
33
# File 'lib/lab42/stream/kernel.rb', line 28

def cyclic_stream *args
  args = args.first if
  args.size == 1 && Enumerable === args.first

  finite_stream( args ).make_cyclic
end

#empty_streamObject



60
# File 'lib/lab42/stream/empty.rb', line 60

def empty_stream; Empty.new end

#falseObject



4
# File 'lib/lab42/stream/kernel/extensions.rb', line 4

def false; false end

#finite_stream(enum) ⇒ Object



35
36
37
38
39
40
# File 'lib/lab42/stream/kernel.rb', line 35

def finite_stream enum
  e = enum.lazy
  cons_stream( e.peek ){ finite_stream e.drop( 1 ) }
rescue StopIteration
  empty_stream
end

#flatmap(stream, *args, &blk) ⇒ Object



42
43
44
# File 'lib/lab42/stream/kernel.rb', line 42

def flatmap stream, *args, &blk
  stream.flatmap( *args, &blk )
end

#iterate(*args, &blk) ⇒ Object Also known as: stream_by



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lab42/stream/kernel.rb', line 61

def iterate *args, &blk
  if blk
    cons_stream(*args){ iterate( blk.(*args), &blk ) }
  else
    rest = args.drop 1
    if Method === rest.first 
      cons_stream( args.first ){ iterate( rest.first.(*([args.first] + rest.drop(1))), *rest ) }
    else
      cons_stream( args.first ){ iterate( sendmsg(*rest).(args.first), *rest ) }
    end
  end
end

#merge_streams(*streams) ⇒ Object

TODO: Reimplement with a cursor into streams to avoid the (potentially) costly array arithm in the tail def



48
49
50
51
52
53
54
# File 'lib/lab42/stream/kernel.rb', line 48

def merge_streams *streams
  s = streams.reject( &:empty? )
  return empty_stream if s.empty?
  cons_stream s.first.head do
    merge_streams(*(s.drop(1) + [s.first.tail]))
  end
end

#merge_streams_by(*streams_and_beh, &blk) ⇒ Object



56
57
58
59
# File 'lib/lab42/stream/kernel.rb', line 56

def merge_streams_by *streams_and_beh, &blk
  beh = blk || streams_and_beh.pop
  __merge_streams_by__ beh, streams_and_beh
end

#nilObject



5
# File 'lib/lab42/stream/kernel/extensions.rb', line 5

def nil; nil end

#selfObject



6
# File 'lib/lab42/stream/kernel/extensions.rb', line 6

def self; self end

#trueObject



3
# File 'lib/lab42/stream/kernel/extensions.rb', line 3

def true; true end