Methods
elements
elements!
elementwise
every
every!
ewise
per
threaded_map
threaded_map_send
to_elem
Classes and Modules
Class Enumerable::ElementorClass Enumerable::Enumerator
Public Instance methods
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]
This method is also aliased as
ewise
[ + ]
# 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]
This method is also aliased as
elements
[ + ]
# File lib/more/facets/elementor.rb, line 57 def every @_every ||= to_elem end
In place version of every.
This method is also aliased as
elements!
[ + ]
# 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
[ + ]
# 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