Module: Enumerable
- Included in:
- Enumerating::Concatenator, Enumerating::Filter, Enumerating::Merger, Enumerating::Prefetcher, Enumerating::ThreadJoiner, Enumerating::ThreadStarter, 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
110 111 112 |
# File 'lib/enumerating/filtering.rb', line 110 def [](n) dropping(n).first end |
#collecting ⇒ Object Also known as: mapping
25 26 27 28 29 30 31 |
# File 'lib/enumerating/filtering.rb', line 25 def collecting Enumerating::Filter.new do |output| each do |element| output.call yield(element) end end end |
#dropping(n) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/enumerating/filtering.rb', line 91 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
100 101 102 103 104 105 106 107 108 |
# File 'lib/enumerating/filtering.rb', line 100 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
45 46 47 48 49 50 51 |
# File 'lib/enumerating/filtering.rb', line 45 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
35 36 37 38 39 40 41 |
# File 'lib/enumerating/filtering.rb', line 35 def selecting Enumerating::Filter.new do |output| each do |element| output.call(element) if yield(element) end end end |
#taking(n) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/enumerating/filtering.rb', line 71 def taking(n) Enumerating::Filter.new do |output| if n > 0 each_with_index do |element, index| output.call(element) break if index + 1 == n end end end end |
#taking_while ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/enumerating/filtering.rb', line 82 def taking_while Enumerating::Filter.new do |output| each do |element| break unless yield(element) output.call(element) end end end |
#threading(max_threads, &block) ⇒ Object
46 47 48 49 |
# File 'lib/enumerating/threading.rb', line 46 def threading(max_threads, &block) threads = Enumerating::ThreadStarter.new(self, block) Enumerating::ThreadJoiner.new(threads.prefetching(max_threads - 1)) end |
#uniqing ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/enumerating/filtering.rb', line 53 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
62 63 64 65 66 67 68 69 |
# File 'lib/enumerating/filtering.rb', line 62 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 |