Module: Enumerable
- Defined in:
- lib/enumerabler.rb
Instance Method Summary collapse
- #collector(&blk) ⇒ Object (also: #mapper)
- #dropper(n) ⇒ Object
- #dropper_while(&blk) ⇒ Object
- #finder_all(&blk) ⇒ Object (also: #all_finder, #selector)
- #flat_mapper(&blk) ⇒ Object (also: #concat_collector)
- #grepper(pat, &blk) ⇒ Object
- #rejecter(&blk) ⇒ Object
- #taker(n) ⇒ Object
- #taker_while(&blk) ⇒ Object
- #zipper(*ary, &blk) ⇒ Object
Instance Method Details
#collector(&blk) ⇒ Object Also known as: mapper
28 29 30 31 32 33 34 |
# File 'lib/enumerabler.rb', line 28 def collector(&blk) Enumerator.new do |e| each do |x| e << blk[x] end end end |
#dropper(n) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/enumerabler.rb', line 93 def dropper(n) Enumerator.new do |e| i = 0 each do |x| e << x if i >= n i += 1 end end end |
#dropper_while(&blk) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/enumerabler.rb', line 103 def dropper_while(&blk) Enumerator.new do |e| flag = false each do |x| flag = true unless flag || blk[x] e << x if flag end end end |
#finder_all(&blk) ⇒ Object Also known as: all_finder, selector
10 11 12 13 14 15 16 |
# File 'lib/enumerabler.rb', line 10 def finder_all(&blk) Enumerator.new do |e| each do |x| e << x if blk[x] end end end |
#flat_mapper(&blk) ⇒ Object Also known as: concat_collector
37 38 39 40 41 42 43 44 45 |
# File 'lib/enumerabler.rb', line 37 def flat_mapper(&blk) Enumerator.new do |e| each do |a| blk[a].each do |x| e << x end end end end |
#grepper(pat, &blk) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/enumerabler.rb', line 2 def grepper(pat, &blk) Enumerator.new do |e| each do |x| e << (blk ? blk[x] : x) if pat === x end end end |
#rejecter(&blk) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/enumerabler.rb', line 20 def rejecter(&blk) Enumerator.new do |e| each do |x| e << x unless blk[x] end end end |
#taker(n) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/enumerabler.rb', line 73 def taker(n) Enumerator.new do |e| i = 0 each do |x| break if i == n e << x i += 1 end end end |
#taker_while(&blk) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/enumerabler.rb', line 84 def taker_while(&blk) Enumerator.new do |e| each do |x| break unless blk[x] e << x end end end |
#zipper(*ary, &blk) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/enumerabler.rb', line 48 def zipper(*ary, &blk) Enumerator.new do |e| if ary.all? {|a| a.is_a?(Array) } i = 0 each do |x| e << [x] + ary.map {|a| a[i] } i += 1 end else ary = ary.map! {|a| a.to_enum } each do |x| a = ary.map.with_index do |a, i| next nil if a == nil begin a.next rescue StopIteration ary[i] = nil end end e << [x] + a end end end end |