Class: Enumerator::Lazy
Overview
== Acknowledgements
Based on https://github.com/yhara/enumerable-lazy
Inspired by https://github.com/antimon2/enumerable_lz
http://jp.rubyist.net/magazine/?0034-Enumerable_lz (ja)
Constant Summary
Constants included
from Enumerable
Enumerable::NONE
Instance Attribute Summary
Attributes inherited from Enumerator
#args, #fib, #meth, #obj
Instance Method Summary
collapse
Methods inherited from Enumerator
#+, #each, #each_with_index, #feed, #initialize_copy, #inspect, #next, #next_values, #peek, #peek_values, produce, #rewind, #with_index, #with_object
Methods included from Enumerable
__update_hash, #all?, #any?, #chain, #count, #cycle, #detect, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_index, #first, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reverse_each, #sort, #sort_by, #tally, #to_h
Constructor Details
#initialize(obj, &block) ⇒ Lazy
Returns a new instance of Lazy
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 30
def initialize(obj, &block)
super(){|yielder|
begin
obj.each{|x|
if block
block.call(yielder, x)
else
yielder << x
end
}
rescue StopIteration
end
}
end
|
Instance Method Details
#drop(n) ⇒ Object
89
90
91
92
93
94
95
96
97
98
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 89
def drop(n)
dropped = 0
Lazy.new(self){|yielder, val|
if dropped < n
dropped += 1
else
yielder << val
end
}
end
|
#drop_while(&block) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 100
def drop_while(&block)
dropping = true
Lazy.new(self){|yielder, val|
if dropping
if not block.call(val)
yielder << val
dropping = false
end
else
yielder << val
end
}
end
|
#flat_map(&block) ⇒ Object
Also known as:
collect_concat
138
139
140
141
142
143
144
145
146
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 138
def flat_map(&block)
Lazy.new(self){|yielder, val|
ary = block.call(val)
# TODO: check ary is an Array
ary.each{|x|
yielder << x
}
}
end
|
#grep(pattern) ⇒ Object
81
82
83
84
85
86
87
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 81
def grep(pattern)
Lazy.new(self){|yielder, val|
if pattern === val
yielder << val
end
}
end
|
#map(&block) ⇒ Object
Also known as:
collect
57
58
59
60
61
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 57
def map(&block)
Lazy.new(self){|yielder, val|
yielder << block.call(val)
}
end
|
#reject(&block) ⇒ Object
73
74
75
76
77
78
79
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 73
def reject(&block)
Lazy.new(self){|yielder, val|
unless block.call(val)
yielder << val
end
}
end
|
#select(&block) ⇒ Object
Also known as:
find_all
64
65
66
67
68
69
70
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 64
def select(&block)
Lazy.new(self){|yielder, val|
if block.call(val)
yielder << val
end
}
end
|
#take(n) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 114
def take(n)
if n == 0
return Lazy.new(self){raise StopIteration}
end
taken = 0
Lazy.new(self){|yielder, val|
yielder << val
taken += 1
if taken >= n
raise StopIteration
end
}
end
|
#take_while(&block) ⇒ Object
128
129
130
131
132
133
134
135
136
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 128
def take_while(&block)
Lazy.new(self){|yielder, val|
if block.call(val)
yielder << val
else
raise StopIteration
end
}
end
|
#to_enum(meth = :each, *args, &block) ⇒ Object
Also known as:
enum_for
45
46
47
48
49
50
51
52
53
54
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 45
def to_enum(meth=:each, *args, &block)
unless self.respond_to?(meth)
raise ArgumentError, "undefined method #{meth}"
end
lz = Lazy.new(self, &block)
lz.obj = self
lz.meth = meth
lz.args = args
lz
end
|
#uniq(&block) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 161
def uniq(&block)
hash = {}
Lazy.new(self){|yielder, val|
if block
v = block.call(val)
else
v = val
end
unless hash.include?(v)
yielder << val
hash[v] = val
end
}
end
|
#zip(*args, &block) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
|
# File 'mrbgems/mruby-enum-lazy/mrblib/lazy.rb', line 149
def zip(*args, &block)
enums = [self] + args
Lazy.new(self){|yielder, val|
ary = enums.map{|e| e.next}
if block
yielder << block.call(ary)
else
yielder << ary
end
}
end
|