Functor
By definition a Functor is simply a first class method, but these are common in the form of Method and Proc. So here a Functor is a bit more specialized as a 1st class metafunction. Essentally, a Functor can vary its behavior accorrding to the operation applied to it.
Synopsis
f = Functor.new { |op, x| x.send(op, x) } f + 1 #=> 2 f + 2 #=> 4 f + 3 #=> 6 f * 1 #=> 1 f * 2 #=> 2 f * 3 #=> 9
Methods
Public Class methods
[ show source ]
# File lib/facets/more/functor.rb, line 44 def initialize(*targets, &function) @targets = targets @function = function end
Public Instance methods
[ show source ]
# File lib/facets/more/functor.rb, line 59 def instance_function; @function ; end
[ show source ]
# File lib/facets/more/functor.rb, line 58 def instance_objects ; @targets ; end
[ show source ]
# File lib/facets/more/functor.rb, line 57 def instance_targets ; @targets ; end
[ show source ]
# File lib/facets/more/functor.rb, line 49 def method_missing(op, *args, &blk) if block_given? @function.call(op, *(@targets + args), &blk) else @function.call(op, *(@targets + args)) end end