Module: Enumerable
- Included in:
- Enumerating::Concatenator, Enumerating::Filter, Enumerating::Merger, Enumerating::Prefetcher, Enumerating::Zipper
- Defined in:
- lib/enumerating/filtering.rb,
lib/enumerating/threading.rb,
lib/enumerating/prefetching.rb
Instance Method Summary collapse
- #[](n) ⇒ Object
- #collecting ⇒ Object (also: #mapping)
- #dropping(n) ⇒ Object
- #dropping_while ⇒ Object
- #prefetching(size) ⇒ Object
- #rejecting ⇒ Object
- #selecting ⇒ Object (also: #finding_all)
- #taking(n) ⇒ Object
- #taking_while ⇒ Object
- #threading(max_threads, &block) ⇒ Object
- #uniqing ⇒ Object
- #uniqing_by ⇒ Object
Instance Method Details
#[](n) ⇒ Object
114 115 116 |
# File 'lib/enumerating/filtering.rb', line 114 def [](n) dropping(n).first end |
#collecting ⇒ Object Also known as: mapping
29 30 31 32 33 34 35 |
# File 'lib/enumerating/filtering.rb', line 29 def collecting Enumerating::Filter.new do |output| each do |element| output.call yield(element) end end end |
#dropping(n) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/enumerating/filtering.rb', line 95 def dropping(n) Enumerating::Filter.new do |output| each_with_index do |element, index| next if index < n output.call(element) end end end |
#dropping_while ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/enumerating/filtering.rb', line 104 def dropping_while Enumerating::Filter.new do |output| taking = false each do |element| taking ||= !yield(element) output.call(element) if taking end end end |
#prefetching(size) ⇒ Object
37 38 39 |
# File 'lib/enumerating/prefetching.rb', line 37 def prefetching(size) Enumerating::Prefetcher.new(self, size) end |
#rejecting ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/enumerating/filtering.rb', line 49 def rejecting Enumerating::Filter.new do |output| each do |element| output.call(element) unless yield(element) end end end |
#selecting ⇒ Object Also known as: finding_all
39 40 41 42 43 44 45 |
# File 'lib/enumerating/filtering.rb', line 39 def selecting Enumerating::Filter.new do |output| each do |element| output.call(element) if yield(element) end end end |
#taking(n) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/enumerating/filtering.rb', line 75 def taking(n) Enumerating::Filter.new do |output| if n > 0 each_with_index do |element, index| output.call(element) throw Enumerating::Filter::DONE if index + 1 == n end end end end |
#taking_while ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/enumerating/filtering.rb', line 86 def taking_while Enumerating::Filter.new do |output| each do |element| throw Enumerating::Filter::DONE unless yield(element) output.call(element) end end end |
#threading(max_threads, &block) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/enumerating/threading.rb', line 6 def threading(max_threads, &block) collecting do |item| Thread.new { block.call(item) } end.prefetching(max_threads - 1).collecting do |thread| thread.join; thread.value end end |
#uniqing ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/enumerating/filtering.rb', line 57 def uniqing Enumerating::Filter.new do |output| seen = Set.new each do |element| output.call(element) if seen.add?(element) end end end |
#uniqing_by ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/enumerating/filtering.rb', line 66 def uniqing_by Enumerating::Filter.new do |output| seen = Set.new each do |element| output.call(element) if seen.add?(yield element) end end end |