Class: Functor

Inherits:
Object show all
Defined in:
lib/core/facets/functor.rb

Overview

Functor

By definition a Functor is simply a first class method, but these are common in the form of Method and Proc. So for Ruby 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  #=> 4
f * 3  #=> 9

Instance Method Summary collapse

Constructor Details

#initialize(&function) ⇒ Functor

Returns a new instance of Functor.



51
52
53
# File 'lib/core/facets/functor.rb', line 51

def initialize(&function)
  @function = function
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(op, *args, &blk) ⇒ Object

Any action against the functor is processesd by the function.



60
61
62
# File 'lib/core/facets/functor.rb', line 60

def method_missing(op, *args, &blk)
  @function.call(op, *args, &blk)
end

Instance Method Details

#to_procObject



55
56
57
# File 'lib/core/facets/functor.rb', line 55

def to_proc
  @function
end