Class: Lab42::Stream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lab42/stream.rb,
lib/lab42/stream/empty.rb,
lib/lab42/stream/delayed.rb,
lib/lab42/stream/version.rb

Direct Known Subclasses

Delayed, Empty

Defined Under Namespace

Classes: Delayed, Empty

Constant Summary collapse

ConstraintError =
Class.new RuntimeError
Version =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



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

def head
  @head
end

#promiseObject (readonly)

Returns the value of attribute promise.



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

def promise
  @promise
end

Instance Method Details

#__flatmap__(a_proc) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/lab42/stream.rb', line 105

def __flatmap__ a_proc
  hh = a_proc.( head )
  if hh.empty?
    tail.__flatmap__ a_proc
  else
    cons_stream( hh.head ){ hh.tail + tail.__flatmap__( a_proc ) }
  end
end

#__inject_while__(ival, cond, red) ⇒ Object

Raises:



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lab42/stream.rb', line 66

def __inject_while__ ival, cond, red
  raise ConstraintError unless cond.(ival)
  s = self
  loop do
    new_val = red.(ival, s.head)
    return ival unless cond.(new_val)
    ival = new_val
    s = s.tail
    return ival if s.empty?
  end
end

#append(other) ⇒ Object Also known as: +

Raises:

  • (ArgumentError)


15
16
17
18
# File 'lib/lab42/stream.rb', line 15

def append other
  raise ArgumentError, "not a stream #{other}" unless self.class === other
  cons_stream( head ){ tail.append other }
end

#drop(n = 1) ⇒ Object

Raises:

  • (ArgumentError)


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

def drop n = 1
  raise ArgumentError, "not a non negative number" if n < 0
  t = self
  loop do
    return t if n.zero?
    n -=1
    t = t.tail
  end
end

#eachObject



31
32
33
34
35
36
37
# File 'lib/lab42/stream.rb', line 31

def each
  t = self
  loop do
    yield t.head
    t = t.tail
  end
end

#empty?Boolean

Returns:

  • (Boolean)


39
# File 'lib/lab42/stream.rb', line 39

def empty?; false end

#filter(*args, &blk) ⇒ Object

Raises:

  • (ArgumentError)


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

def filter *args, &blk
  # TODO: Get this check and a factory to create a proc for this into core
  raise ArgumentError, "use either a block or arguments" if args.empty? && !blk || !args.empty? && blk
  filter_by_proc mk_proc( blk || args )
end

#filter_by_proc(prc) ⇒ Object



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

def filter_by_proc prc
  if prc.(head)
    cons_stream(head){ tail.filter_by_proc prc }
  else
    # TODO: Replace this with Delayed Stream (1 off)
    tail.filter_by_proc prc 
  end
end

#flatmap(*args, &blk) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/lab42/stream.rb', line 56

def flatmap *args, &blk
  if args.empty?
    __flatmap__ blk
  elsif args.size == 1 && args.first.respond_to?( :call )
    __flatmap__ args.first
  else
    __flatmap__ sendmsg(*args)
  end
end

#make_cyclicObject



78
79
80
81
82
# File 'lib/lab42/stream.rb', line 78

def make_cyclic
  cons_stream( head ){
    tail.append( make_cyclic )
  }
end

#map(*args, &blk) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
# File 'lib/lab42/stream.rb', line 84

def map *args, &blk
  # TODO: Get this check and a factory to create a proc for this into core/fn
  raise ArgumentError, "use either a block or arguments" if args.empty? && !blk || !args.empty? && blk
  transform_by_proc mk_proc( blk || args )
end

#reduce_while(cond, red = nil, &reducer) ⇒ Object



90
91
92
93
# File 'lib/lab42/stream.rb', line 90

def reduce_while cond, red=nil, &reducer
  red ||= reducer
  tail.__inject_while__ head, cond, red
end

#tailObject



95
96
97
# File 'lib/lab42/stream.rb', line 95

def tail
  promise.()
end

#to_streamObject



99
# File 'lib/lab42/stream.rb', line 99

def to_stream; self end

#transform_by_proc(prc) ⇒ Object



101
102
103
# File 'lib/lab42/stream.rb', line 101

def transform_by_proc prc
  cons_stream( prc.(head) ){ tail.transform_by_proc prc }
end