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
new(*targets, &function)
# File lib/facets/more/functor.rb, line 45
  def initialize(*targets, &function)
    @self     = function.binding.self
    @targets  = targets
    @function = function
  end
Public Instance methods
instance_function()
# File lib/facets/more/functor.rb, line 64
  def instance_function ; @function ; end
instance_objects()
# File lib/facets/more/functor.rb, line 63
  def instance_objects  ; @objects  ; end
instance_self()

TODO use xxx notation?

# File lib/facets/more/functor.rb, line 61
  def instance_self     ; @self     ; end
instance_targets()
# File lib/facets/more/functor.rb, line 62
  def instance_targets  ; @targets  ; end
method_missing(op, *args, &blk)
# File lib/facets/more/functor.rb, line 51
  def method_missing(op, *args, &blk)
    if block_given?
      @function.call(op, *(@targets + args), &blk)
    else
      @function.call(op, *(@targets + args))
    end
  end