Class Enumerable::Elementor
Class Enumerable::Enumerator
Alias for every
Alias for every!
Returns an elementwise Functor designed to make R-like elementwise operations possible.
[1,2].elementwise + 3 #=> [4,5] [1,2].elementwise + [4,5] #=> [5,7] [1,2].elementwise + [[4,5],3] #=> [[5,7],[4,5]
[ + ]
# File lib/more/facets/elementwise.rb, line 16 def elementwise(count=1) @_elementwise_functor ||= [] @_elementwise_functor[count] ||= Functor.new do |op,*args| if args.empty? r = self count.times do r = r.collect{ |a| a.send(op) } end r else r = args.collect do |arg| if Array === arg #arg.kind_of?(Enumerable) x = self count.times do ln = (arg.length > length ? length : arg.length ) x = x.slice(0...ln).zip(arg[0...ln]).collect{ |a,b| a.send(op,b) } #slice(0...ln).zip(arg[0...1n]).collect{ |a,b| b ? a.send(op,b) : nil } end x else x = self count.times do x = x.collect{ |a| a.send(op,arg) } end x end end r.flatten! if args.length == 1 r end end end
Returns an elemental object. This allows you to map a method on to every element.
r = [1,2,3].every + 3 #=> [4,5,6]
[ + ]
# File lib/more/facets/elementor.rb, line 57 def every @_every ||= to_elem end
In place version of every.
[ + ]
# File lib/more/facets/elementor.rb, line 63 def every! raise NoMethodError unless respond_to?(:map!) @_every_inplace ||= to_elem(:map!) end
Alias for elementwise
Without a block: wrap the Enumerable object in such a way that map, select and similar operations are performed "horizontally" across a series of blocks, instead of building an array of results at each step. This reduces memory usage, allows partial results to be provided early, and permits working with infinite series.
(1..1_000_000_000).filter.select{ |i| i % 2 == 0 }. map{ |i| i + 100 }. take(10).to_a
With a block: the block acts as an arbitrary filter on the data. Unlike map, it can choose to drop elements from the result, and/or add additional ones. The first object passed to the block is the receiver of the output.
(1..1_000_000_000). filter { |out,i| out << i if i % 2 == 0 }. # like select filter { |out,i| out << i + 100 }. # like map take(10).each { |i| puts i }
Use a method like to_a or to_h at the end of the chain if you want an Array or Hash built with the results, or each { … } if you just want to output each result and discard it.
[ + ]
# File lib/more/facets/filter.rb, line 30 def filter(&blk) if block_given? Enumerator::Filter.new do |output| each do |*input| yield output, *input end end else Enumerator::Filter.new do |output| each do |*input| output.yield *input end end end end
[ + ]
# File lib/more/facets/elementor.rb, line 46 def per @__per__ ||= Functor.new do |op| Elementor.new(self, op) end end
Like Enumerable#map but each iteration is processed via a separate thread.
CREDIT Sean O‘Halpin
[ + ]
# File lib/more/facets/thread.rb, line 37 def threaded_map #:yield: map{ |e| Thread.new(e){ |t| yield(t) } }.map{ |t| t.value } end
Like Enumerable#map_send but each iteration is processed via a separate thread.
CREDIT Sean O‘Halpin
[ + ]
# File lib/more/facets/thread.rb, line 46 def threaded_map_send(meth, *args, &block) map{ |e| Thread.new(e){ |t| t.send(meth, *args, &block) } }.map{ |t| t.value } end