Class | Functor |
In: |
lib/facets/more/functor.rb
|
Parent: | Proc |
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.
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
Functor is created with a block the first argument of which is always the operator/method the functor will recieve. You can pass thru additional arguments along with the function that will be passed to the function as the initial parameters after the operator.