lib/core/facets/enumerable/ewise.rb in facets-2.8.1 vs lib/core/facets/enumerable/ewise.rb in facets-2.8.2

- old
+ new

@@ -1,23 +1,23 @@ require 'facets/functor' module Enumerable + EWISE = {} + # Returns an elementwise Functor designed to make R-like - # elementwise operations possible. + # elementwise operations possible. This is very much like + # the #every method, but it treats array argument specially. # - # [1,2].elementwise + 3 #=> [4,5] - # [1,2].elementwise + [4,5] #=> [5,7] - # [1,2].elementwise + [[4,5],3] #=> [[5,7],[4,5] + # [1,2].ewise + 3 #=> [4,5] + # [1,2].ewise + [4,5] #=> [5,7] + # [1,2].ewise + [[4,5],3] #=> [[5,7],[4,5] # - #-- # Special thanks to Martin DeMello for helping to develop this. - #++ - def elementwise(count=1) - @_elementwise_functor ||= [] - @_elementwise_functor[count] ||= Functor.new do |op,*args| + def ewise(count=1) + EWISE[[self,count]] ||= Functor.new do |op,*args| if args.empty? r = self count.times do r = r.collect{ |a| a.send(op) } end @@ -44,18 +44,24 @@ r end end end - # Concise alias for #elementwise. + # Long-term for #ewise. # # a = [1,2] - # a.ewise + 3 #=> [4,5] - # a.ewise + [4,5] #=> [5,7] - # a.ewise + [[4,5],3] #=> [[5,7],[4,5] + # a.elementwise + 3 #=> [4,5] + # a.elementwise + [4,5] #=> [5,7] + # a.elementwise + [[4,5],3] #=> [[5,7],[4,5] # - # Note this used to be #ew as weel as the '%' operator. - # Both of whihc are deprecated. + alias_method :elementwise, :ewise - alias_method :ewise, :elementwise + # Operator equivalent of #elementwise. + # + # This was deprecated along with it's file "op_tilde.rb". + # + #def ~@ + # ewise + #end end +