### Home Classes Methods

### In Files

- lib/facets/functor.rb

### Parent

Object

### Methods

- ::cache
- ::new
- #method_missing
- #to_proc

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Lazy
- Lazy::Future
- Lazy::Promise
- Lazy::Promise::DIVERGES
- Lazy::PromiseSafe
- Enumerable
- Enumerable::Recursor
- Method
- Method::Composition
- Module
- Module::InstanceFunction
- Array
- Binding
- Class
- Comparable
- Denumerable
- Denumerator
- Dir
- Enumerator
- Exception
- Facets
- FalseClass
- File
- FileTest
- Fixnum
- Float
- Functor
- Hash
- Indexable
- Instance
- Integer
- Kernel
- MatchData
- NA
- NilClass
- Numeric
- Object
- ObjectSpace
- Proc
- Process
- Range
- Regexp
- String
- Struct
- Symbol
- Time
- TrueClass
- UnboundMethod

No matching classes.

# 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 more specialized as a Higher-order function or 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

### Constants

EXCEPTIONS

### Public Class Methods

cache(*key, &function) click to toggle source

Functors can be somewhat inefficient if a new Functor is frequently recreated for the same use. So this cache can be used to speed things up.

The `key` will always be an array, wich makes it easier to cache functor for multiple factors.

    # File lib/facets/functor.rb, line 23
    def self.cache(*key, &function)
      @cache ||= {}
      if function
        @cache[key] = new(&function)
      else
        @cache[key]
      end
    end

new(&function) click to toggle source

    # File lib/facets/functor.rb, line 66
    def initialize(&function)
      @function = function
    end

### Public Instance Methods

to_proc() click to toggle source

    # File lib/facets/functor.rb, line 71
    def to_proc
      @function
    end

### Private Instance Methods

method_missing(op, *args, &blk) click to toggle source

Any action against the functor is processesd by the function.

    # File lib/facets/functor.rb, line 88
    def method_missing(op, *args, &blk)
      @function.call(op, *args, &blk)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.