Class: Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/streams.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head = nil, &block) ⇒ Stream

Returns a new instance of Stream.



4
5
6
7
# File 'lib/streams.rb', line 4

def initialize( head = nil, &block )
  @head = head
  @delayed_tail = block || head && Proc.new { Stream.new }
end

Instance Attribute Details

#delayed_tailObject (readonly)

Returns the value of attribute delayed_tail.



2
3
4
# File 'lib/streams.rb', line 2

def delayed_tail
  @delayed_tail
end

#headObject (readonly)

Returns the value of attribute head.



2
3
4
# File 'lib/streams.rb', line 2

def head
  @head
end

Instance Method Details

#==(other) ⇒ Object



110
111
112
# File 'lib/streams.rb', line 110

def ==( other )
  head == other.head && tail == other.tail
end

#all?(&block) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/streams.rb', line 122

def all?( &block )
  return true if empty?
  block.call( head ) && tail.all?( &block )
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/streams.rb', line 127

def any?( &block )
  return false if empty?
  block.call( head ) || tail.any?( &block )
end

#append(other) ⇒ Object



55
56
57
58
59
# File 'lib/streams.rb', line 55

def append( other )
  return self if other.empty? 
  return other if empty?
  Stream.new( head ) { tail.append( other ) }
end

#at(n) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
# File 'lib/streams.rb', line 67

def at( n )
  raise ArgumentError, "Index out of bounds" if empty? && n >= 0
  return head if n.zero?
  tail.at( n - 1 )
end

#drop(n) ⇒ Object



73
74
75
76
77
# File 'lib/streams.rb', line 73

def drop( n )
  return self if n.zero?
  return Stream.new if empty?
  tail.drop( n - 1 )
end

#each(&block) ⇒ Object



22
23
24
25
26
# File 'lib/streams.rb', line 22

def each( &block )
  return if empty?
  block.call( head )
  tail.each( &block )
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  head.nil?
end

#include?(element) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/streams.rb', line 105

def include?( element )
  return false if empty?
  head == element || tail.include?( element )
end

#inspectObject



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

def inspect
  "[" + join( ", " ) + "]"
end

#join(separator = "") ⇒ Object



95
96
97
98
99
# File 'lib/streams.rb', line 95

def join( separator = "" )
  return "" if empty?
  return head.to_s if tail.empty?
  head.to_s + separator + tail.join( separator )
end

#lastObject



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

def last
  return nil if empty?
  return head if tail.empty?
  tail.last
end

#map(&block) ⇒ Object



28
29
30
31
# File 'lib/streams.rb', line 28

def map( &block )
  return self if empty?
  Stream.new( block.call( head ) ) { tail.map( &block ) }
end

#merge(other) ⇒ Object



49
50
51
52
53
# File 'lib/streams.rb', line 49

def merge( other )
  return self if other.empty?
  return other if self.empty?
  Stream.new( head + other.head ) { tail.merge( other.tail ) }
end

#reject(&block) ⇒ Object



43
44
45
46
47
# File 'lib/streams.rb', line 43

def reject( &block )
  select do | element |
    !block.call( element )
  end
end

#select(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/streams.rb', line 33

def select( &block )
  return self if empty?
  
  if block.call( head )
    return Stream.new( head ) { tail.select( &block ) }
  end

  tail.select( &block )
end

#sizeObject



17
18
19
20
# File 'lib/streams.rb', line 17

def size
  return 0 if empty?
  1 + tail.size
end

#tailObject



9
10
11
# File 'lib/streams.rb', line 9

def tail
  @tail ||= delayed_tail && delayed_tail.call
end

#take(n) ⇒ Object



61
62
63
64
65
# File 'lib/streams.rb', line 61

def take( n )
  return Stream.new if empty? || n.zero?
  return Stream.new( head ) if n == 1
  Stream.new( head ) { tail.take( n - 1 ) }
end

#take_while(&block) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/streams.rb', line 114

def take_while( &block )
  if block.call( head )
    Stream.new( head ) { tail.take_while( &block ) }
  else
    Stream.new
  end
end

#to_aObject



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

def to_a
  return [] if empty?
  [ head ] + tail.to_a
end

#uniqObject



79
80
81
82
# File 'lib/streams.rb', line 79

def uniq
  return self if empty?
  Stream.new( head ) { tail.select { | element | element != head }.uniq }
end