Class: Array
Direct Known Subclasses
Instance Method Summary collapse
- #apply(to) ⇒ Object
- #hmap(&block) ⇒ Object
- #mall?(op, &block) ⇒ Boolean
- #mavg(&block) ⇒ Object
- #meach(*args, &block) ⇒ Object
- #mmid(&sorter) ⇒ Object
- #msum(initial = 0.0, op = :+, &block) ⇒ Object
- #msym ⇒ Object
- #return_first(&block) ⇒ Object (also: #earliest)
- #sequester(crush = true) ⇒ Object
- #unempty? ⇒ Boolean (also: #notempty?)
- #values ⇒ Object
Instance Method Details
#apply(to) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/ext/array.rb', line 52 def apply(to) map do |m| if m.is_a?(Symbol) to.send(m) else m.to_s.walk(to) end end end |
#hmap(&block) ⇒ Object
86 87 88 |
# File 'lib/ext/array.rb', line 86 def hmap(&block) each.with_object({}, &block) end |
#mall?(op, &block) ⇒ Boolean
76 77 78 79 80 81 82 83 84 |
# File 'lib/ext/array.rb', line 76 def mall?(op, &block) if op all? { |item| item.send(op) } elsif block all? { |item| item.instance_exec(item, &block) } else all? { |item| item } end end |
#mavg(&block) ⇒ Object
21 22 23 |
# File 'lib/ext/array.rb', line 21 def mavg(&block) msum(&block) / size end |
#meach(*args, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ext/array.rb', line 62 def meach(*args, &block) if block map { |item| instance_exec(item, *args, &block) } elsif args.size == 1 map(&args.first.sym) else #args.each.with_object({}) do |action, o| # o[action.sym] = map(&action.sym) #end method = args.shift map { |item| item.send(method, *args) } end end |
#mmid(&sorter) ⇒ Object
25 26 27 28 |
# File 'lib/ext/array.rb', line 25 def mmid(&sorter) sorted = sort(&sorter) length.odd? ? sorted[length / 2].f! : (sorted[length/2 - 1].f! + sorted[length/2].f!).f! / 2 end |
#msum(initial = 0.0, op = :+, &block) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/ext/array.rb', line 13 def msum(initial = 0.0, op = :+, &block) if block inject(initial) { |m, val| m.send(op, block[val]) } else map(&:f!).inject(initial, op) end end |
#msym ⇒ Object
50 |
# File 'lib/ext/array.rb', line 50 def msym() map(&:sym) end |
#return_first(&block) ⇒ Object Also known as: earliest
42 43 44 45 46 |
# File 'lib/ext/array.rb', line 42 def return_first(&block) returned = nil each { |item| break if (returned = block.call(item)) } returned end |
#sequester(crush = true) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/ext/array.rb', line 34 def sequester(crush = true) if crush compact.size <= 1 ? compact.first : self else size <= 1 ? first : self end end |
#unempty? ⇒ Boolean Also known as: notempty?
7 8 9 |
# File 'lib/ext/array.rb', line 7 def unempty?() !empty? end |
#values ⇒ Object
30 31 32 |
# File 'lib/ext/array.rb', line 30 def values() self end |