Module: Lazily::Enumerable
- Includes:
- Enumerable
- Included in:
- Concatenator, Dequeuer, Filter, Merger, Prefetcher, Proxy, Zipper
- Defined in:
- lib/lazily/zipping.rb,
lib/lazily/filtering.rb,
lib/lazily/threading.rb,
lib/lazily/enumerable.rb,
lib/lazily/prefetching.rb,
lib/lazily/concatenating.rb
Instance Method Summary collapse
-
#[](n) ⇒ Object
The nth element.
- #chunk(*args, &block) ⇒ Object
-
#collect(&transformation) ⇒ Enumerable
(also: #map)
Transform elements using the block provided.
-
#compact ⇒ Enumerable
Ignore nil values.
-
#concat(*others) ⇒ Enumerable
Concatenates this and other Enumerables.
-
#drop(n) ⇒ Enumerable
Ignore the first n elements.
-
#drop_while(&predicate) ⇒ Enumerable
Reject elements while a predicate returns true.
- #flat_map(&block) ⇒ Object (also: #collect_concat)
-
#flatten(level = 1) ⇒ Enumerable
Flatten the collection, such that Enumerable elements are included inline.
-
#grep(pattern) ⇒ Enumerable
Select elements matching a pattern.
- #in_threads(max_threads, &block) ⇒ Object
- #lazily ⇒ Object
-
#lazy? ⇒ Boolean
True.
- #prefetch(size) ⇒ Object
-
#reject {|element| ... } ⇒ Enumerable
Select elements that fail a test.
-
#select(&predicate) ⇒ Enumerable
(also: #find_all)
Select elements using a predicate block.
- #slice_before(*args, &block) ⇒ Object
-
#take(n) ⇒ Enumerable
Select the first n elements.
-
#take_while(&predicate) ⇒ Enumerable
Select elements while a predicate returns true.
-
#uniq ⇒ Enumerable
Remove duplicate values.
- #zip(*others) ⇒ Object
Instance Method Details
#[](n) ⇒ Object
Returns the nth element.
219 220 221 |
# File 'lib/lazily/filtering.rb', line 219 def [](n) drop(n).first end |
#chunk(*args, &block) ⇒ Object
211 212 213 |
# File 'lib/lazily/filtering.rb', line 211 def chunk(*args, &block) super.lazily end |
#collect(&transformation) ⇒ Enumerable Also known as: map
Transform elements using the block provided.
12 13 14 15 16 17 18 |
# File 'lib/lazily/filtering.rb', line 12 def collect(&transformation) filter("collect") do |yielder| each do |element| yielder.call yield(element) end end end |
#compact ⇒ Enumerable
Ignore nil values.
193 194 195 196 197 198 199 |
# File 'lib/lazily/filtering.rb', line 193 def compact filter("compact") do |yielder| each do |element| yielder.call(element) unless element.nil? end end end |
#concat(*others) ⇒ Enumerable
Concatenates this and other Enumerables.
32 33 34 |
# File 'lib/lazily/concatenating.rb', line 32 def concat(*others) Lazily.concat(self, *others) end |
#drop(n) ⇒ Enumerable
Ignore the first n elements.
117 118 119 120 121 122 123 124 |
# File 'lib/lazily/filtering.rb', line 117 def drop(n) filter("drop") do |yielder| each_with_index do |element, index| next if index < n yielder.call(element) end end end |
#drop_while(&predicate) ⇒ Enumerable
Reject elements while a predicate returns true.
132 133 134 135 136 137 138 139 140 |
# File 'lib/lazily/filtering.rb', line 132 def drop_while(&predicate) filter("drop_while") do |yielder| take = false each do |element| take ||= !yield(element) yielder.call(element) if take end end end |
#flat_map(&block) ⇒ Object Also known as: collect_concat
181 182 183 |
# File 'lib/lazily/filtering.rb', line 181 def flat_map(&block) map(&block).flatten end |
#flatten(level = 1) ⇒ Enumerable
Flatten the collection, such that Enumerable elements are included inline.
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/lazily/filtering.rb', line 169 def flatten(level = 1) filter("flatten") do |yielder| each do |element| if level > 0 && element.respond_to?(:each) element.flatten(level - 1).each(&yielder) else yielder.call(element) end end end end |
#grep(pattern) ⇒ Enumerable
Select elements matching a pattern.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/lazily/filtering.rb', line 148 def grep(pattern) filter("grep") do |yielder| each do |element| if pattern === element result = if block_given? yield element else element end yielder.call(result) end end end end |
#in_threads(max_threads, &block) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/lazily/threading.rb', line 7 def in_threads(max_threads, &block) collect do |item| Thread.new { block.call(item) } end.prefetch(max_threads - 1).collect do |thread| thread.join.value end end |
#lazily ⇒ Object
12 13 14 |
# File 'lib/lazily/enumerable.rb', line 12 def lazily self end |
#lazy? ⇒ Boolean
Returns true.
8 9 10 |
# File 'lib/lazily/enumerable.rb', line 8 def lazy? true end |
#prefetch(size) ⇒ Object
7 8 9 |
# File 'lib/lazily/prefetching.rb', line 7 def prefetch(size) Prefetcher.new(self, size) end |
#reject {|element| ... } ⇒ Enumerable
Select elements that fail a test.
46 47 48 49 50 51 52 |
# File 'lib/lazily/filtering.rb', line 46 def reject filter("reject") do |yielder| each do |element| yielder.call(element) unless yield(element) end end end |
#select(&predicate) ⇒ Enumerable Also known as: find_all
Select elements using a predicate block.
28 29 30 31 32 33 34 |
# File 'lib/lazily/filtering.rb', line 28 def select(&predicate) filter("select") do |yielder| each do |element| yielder.call(element) if yield(element) end end end |
#slice_before(*args, &block) ⇒ Object
203 204 205 |
# File 'lib/lazily/filtering.rb', line 203 def slice_before(*args, &block) super.lazily end |
#take(n) ⇒ Enumerable
Select the first n elements.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/lazily/filtering.rb', line 84 def take(n) filter("take") do |yielder, all_done| if n > 0 each_with_index do |element, index| yielder.call(element) throw all_done if index + 1 == n end end end end |
#take_while(&predicate) ⇒ Enumerable
Select elements while a predicate returns true.
101 102 103 104 105 106 107 108 |
# File 'lib/lazily/filtering.rb', line 101 def take_while(&predicate) filter("take_while") do |yielder, all_done| each do |element| throw all_done unless yield(element) yielder.call(element) end end end |
#uniq ⇒ Enumerable #uniq(&block) ⇒ Enumerable
Remove duplicate values.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lazily/filtering.rb', line 63 def uniq filter("uniq") do |yielder| seen = Set.new each do |element| key = if block_given? yield element else element end yielder.call(element) if seen.add?(key) end end end |