Class: Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-stream.rb

Direct Known Subclasses

EmptyStream

Defined Under Namespace

Classes: EmptyStream

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head, &block) ⇒ Stream

Returns a new instance of Stream.



10
11
12
13
# File 'lib/ruby-stream.rb', line 10

def initialize(head, &block)
  @head = head
  @tail_block = block
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



8
9
10
# File 'lib/ruby-stream.rb', line 8

def head
  @head
end

Class Method Details

.continually(&block) ⇒ Object



2
3
4
5
6
# File 'lib/ruby-stream.rb', line 2

def self.continually(&block)
  Stream.new(yield) do
    Stream.continually(&block)
  end
end

Instance Method Details

#[](n) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby-stream.rb', line 28

def [](n)
  if n == 0
    self.head
  elsif n < 0
    nil
  else
    last_stream = self
    n.times {
      return nil if last_stream.kind_of?(EmptyStream)
      last_stream = last_stream.tail
    }

    last_stream.head
  end
end

#drop(n) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/ruby-stream.rb', line 81

def drop(n)
  if n <= 0
    Stream.new(head) do
      tail
    end
  else
    tail.drop(n - 1)
  end
end

#each(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby-stream.rb', line 50

def each(&block)
  last_stream = self

  until last_stream.kind_of?(EmptyStream)
    block.call(last_stream.head)
    last_stream = last_stream.tail
  end

  nil
end

#filter(&block) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/ruby-stream.rb', line 121

def filter(&block)
  if block.call(head)
    Stream.new(head) do
      tail.filter(&block)
    end
  else
    tail.filter(&block)
  end
end

#fold_left(*args, &block) ⇒ Object Also known as: inject, reduce



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ruby-stream.rb', line 103

def fold_left(*args, &block)
  zero = args[0]
  symbol = args[1] || args[0]

  scan = if block
    self.scan(zero, &block)
  elsif args.length == 2
    self.scan(zero, &symbol.to_proc)
  elsif args.length == 1
    self.drop(1).scan(head, &symbol.to_proc)
  end

  scan.last
end

#lastObject



19
20
21
22
23
24
25
26
# File 'lib/ruby-stream.rb', line 19

def last
  result = nil
  self.each do |ele|
    result = ele
  end

  result
end

#lengthObject



44
45
46
47
48
# File 'lib/ruby-stream.rb', line 44

def length
  counter = 0
  self.each { |ele| counter += 1 }
  counter
end

#map(&block) ⇒ Object



91
92
93
94
95
# File 'lib/ruby-stream.rb', line 91

def map(&block)
  Stream.new(yield head) do
    tail.map(&block)
  end
end

#scan(zero, &block) ⇒ Object



97
98
99
100
101
# File 'lib/ruby-stream.rb', line 97

def scan(zero, &block)
  Stream.new(zero) do
    tail.scan(block.call(zero, head), &block)
  end
end

#tailObject



15
16
17
# File 'lib/ruby-stream.rb', line 15

def tail
  @tail_block.call
end

#take(n) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ruby-stream.rb', line 61

def take(n)
  if n <= 0
    EmptyStream.new
  else
    Stream.new(head) do
      tail.take(n - 1)
    end
  end
end

#take_while(&block) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/ruby-stream.rb', line 71

def take_while(&block)
  if block.call(head)
    Stream.new(head) do
      tail.take_while(&block)
    end
  else
    EmptyStream.new
  end
end