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
- #binop_streams(op, stream1, stream2) ⇒ Object
- #combine_streams(stream, *streams_and_ops, &operation) ⇒ Object
- #cons_stream(head, &tail) ⇒ Object
- #const_stream(const) ⇒ Object
- #cyclic_stream(*args) ⇒ Object
- #empty_stream ⇒ Object
- #false ⇒ Object
- #finite_stream(enum) ⇒ Object
- #flatmap(stream, *args, &blk) ⇒ Object
- #iterate(*args, &blk) ⇒ Object (also: #stream_by)
-
#merge_streams(*streams) ⇒ Object
TODO: Reimplement with a cursor into streams to avoid the (potentially) costly array arithm in the tail def.
- #nil ⇒ Object
- #self ⇒ Object
- #true ⇒ Object
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 |
#const_stream(const) ⇒ Object
17 18 19 |
# File 'lib/lab42/stream/kernel.rb', line 17 def const_stream const c = cons_stream( const ){ c } end |
#cyclic_stream(*args) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/lab42/stream/kernel.rb', line 21 def cyclic_stream *args args = args.first if args.size == 1 && Enumerable === args.first finite_stream( args ).make_cyclic end |
#empty_stream ⇒ Object
60 |
# File 'lib/lab42/stream/empty.rb', line 60 def empty_stream; Empty.new end |
#false ⇒ Object
4 |
# File 'lib/lab42/stream/kernel/extensions.rb', line 4 def false; false end |
#finite_stream(enum) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/lab42/stream/kernel.rb', line 28 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
35 36 37 |
# File 'lib/lab42/stream/kernel.rb', line 35 def flatmap stream, *args, &blk stream.flatmap( *args, &blk ) end |
#iterate(*args, &blk) ⇒ Object Also known as: stream_by
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lab42/stream/kernel.rb', line 49 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
41 42 43 44 45 46 47 |
# File 'lib/lab42/stream/kernel.rb', line 41 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 |
#nil ⇒ Object
5 |
# File 'lib/lab42/stream/kernel/extensions.rb', line 5 def nil; nil end |
#self ⇒ Object
6 |
# File 'lib/lab42/stream/kernel/extensions.rb', line 6 def self; self end |
#true ⇒ Object
3 |
# File 'lib/lab42/stream/kernel/extensions.rb', line 3 def true; true end |