Class: Rubylude

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

Constant Summary collapse

VERSION =
"0.1.0"
Infinity =
1.0 / 0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator) ⇒ Rubylude

Generator: create a Rubylude from a lambda function



7
8
9
# File 'lib/rubylude.rb', line 7

def initialize(generator)
  @generator = Fiber.new { generator.call; nil }
end

Class Method Details

.cycle(values) ⇒ Object

Generator: inject these values indefinitely



19
20
21
22
23
24
25
# File 'lib/rubylude.rb', line 19

def self.cycle(values)
  Rubylude.new ->() {
    loop do
      values.each {|v| Fiber.yield v }
    end
  }
end

.range(start, stop = Infinity, step = 1) ⇒ Object

Generator: generate values from ‘start` to `stop`, separated by `step`



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

def self.range(start, stop=Infinity, step=1)
  Rubylude.new ->() {
    current = start - step
    if step > 0
      while (current += step) <= stop
        Fiber.yield current
      end
    else
      while (current += step) >= stop
        Fiber.yield current
      end
    end
  }
end

.unfold(values) ⇒ Object

Generator: takes an array and inject one element at a time



12
13
14
15
16
# File 'lib/rubylude.rb', line 12

def self.unfold(values)
  Rubylude.new ->() {
    values.each {|v| Fiber.yield v }
  }
end

Instance Method Details

#apply(&blk) ⇒ Object

Transformer: modify each element with the given block



53
54
55
56
57
58
59
# File 'lib/rubylude.rb', line 53

def apply(&blk)
  Rubylude.new ->() {
    while value = @generator.resume
      Fiber.yield blk.call(value)
    end
  }
end

#drop(nb) ⇒ Object

Transformer: drop the first ‘nb` elements



79
80
81
82
83
84
85
86
# File 'lib/rubylude.rb', line 79

def drop(nb)
  Rubylude.new ->() {
    nb.times { @generator.resume }
    while value = @generator.resume
      Fiber.yield value
    end
  }
end

#filter(&blk) ⇒ Object

Transformer: let pass only the elements that return true for the block



44
45
46
47
48
49
50
# File 'lib/rubylude.rb', line 44

def filter(&blk)
  Rubylude.new ->() {
    while value = @generator.resume
      Fiber.yield value if blk.call(value)
    end
  }
end

#take(nb) ⇒ Object

Transformer: let pass only the first ‘nb` elements



62
63
64
65
66
# File 'lib/rubylude.rb', line 62

def take(nb)
  Rubylude.new ->() {
    nb.times { Fiber.yield @generator.resume }
  }
end

#takeWhile(&blk) ⇒ Object

Transformer: if one element returns false, block it and all the following



69
70
71
72
73
74
75
76
# File 'lib/rubylude.rb', line 69

def takeWhile(&blk)
  Rubylude.new ->() {
    while value = @generator.resume
      return unless blk.call(value)
      Fiber.yield value
    end
  }
end

#to_aObject

Consumer: returns an array



96
97
98
99
100
# File 'lib/rubylude.rb', line 96

def to_a
  results = []
  traverse { |value| results << value }
  results
end

#traverse(&blk) ⇒ Object

Consumer: do an operation on each element



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

def traverse(&blk)
  while value = @generator.resume
    blk.call(value)
  end
end