Module: Reducers::Transformers
- Included in:
- Reducible
- Defined in:
- lib/reducers.rb
Instance Method Summary collapse
- #drop(n) ⇒ Object
- #drop_while(&block) ⇒ Object
- #duplicate_on(&block) ⇒ Object
- #flat_map(&block) ⇒ Object
- #grep(patt) ⇒ Object
- #group_by ⇒ Object
- #map(&block) ⇒ Object
- #mapcat(&block) ⇒ Object
- #mash ⇒ Object
- #reject(&block) ⇒ Object
- #select(&block) ⇒ Object
- #take(n) ⇒ Object
- #take_while(&block) ⇒ Object
- #to_a ⇒ Object
Instance Method Details
#drop(n) ⇒ Object
82 83 84 85 |
# File 'lib/reducers.rb', line 82 def drop(n) add_proc dropping(n) self end |
#drop_while(&block) ⇒ Object
92 93 94 95 |
# File 'lib/reducers.rb', line 92 def drop_while(&block) add_proc drop_while_proc(&block) self end |
#duplicate_on(&block) ⇒ Object
41 42 43 |
# File 'lib/reducers.rb', line 41 def duplicate_on(&block) group_by(&block).map{|x| x[1]}.select{|x| x.size > 1} end |
#flat_map(&block) ⇒ Object
97 98 99 100 |
# File 'lib/reducers.rb', line 97 def flat_map(&block) map(&block).flatten self end |
#grep(patt) ⇒ Object
58 59 60 |
# File 'lib/reducers.rb', line 58 def grep(patt) select{|x| x =~ patt} end |
#group_by ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/reducers.rb', line 45 def group_by() groups = reduce({}) do |h,i| result = yield i if h[result] h[result] << i else h[result] = [i] end h end groups.lazy2 end |
#map(&block) ⇒ Object
18 19 20 21 |
# File 'lib/reducers.rb', line 18 def map(&block) add_proc mapping(&block) self end |
#mapcat(&block) ⇒ Object
72 73 74 75 |
# File 'lib/reducers.rb', line 72 def mapcat(&block) add_proc mapcatting(&block) self end |
#mash ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/reducers.rb', line 23 def mash reduce({}) do |result, input| r = yield input case r when Hash nk, nv = *r.to_a[0] when Range nk, nv = r.first, r.last else nk, nv = *r end result[nk] = nv result end end |
#reject(&block) ⇒ Object
67 68 69 70 |
# File 'lib/reducers.rb', line 67 def reject(&block) add_proc filtering(&(block.complement)) self end |
#select(&block) ⇒ Object
62 63 64 65 |
# File 'lib/reducers.rb', line 62 def select(&block) add_proc filtering(&block) self end |
#take(n) ⇒ Object
77 78 79 80 |
# File 'lib/reducers.rb', line 77 def take(n) add_proc taking(n) self end |
#take_while(&block) ⇒ Object
87 88 89 90 |
# File 'lib/reducers.rb', line 87 def take_while(&block) add_proc take_while_proc(&block) self end |
#to_a ⇒ Object
103 104 105 |
# File 'lib/reducers.rb', line 103 def to_a force end |