https://github.com/rubyworks/functor
Higher Order Functions for Ruby
https://github.com/rubyworks/functor
Last synced: 6 months ago
JSON representation
Higher Order Functions for Ruby
- Host: GitHub
- URL: https://github.com/rubyworks/functor
- Owner: rubyworks
- License: other
- Created: 2011-12-07T19:55:13.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2015-02-24T02:15:42.000Z (almost 11 years ago)
- Last Synced: 2023-04-10T08:21:03.166Z (almost 3 years ago)
- Language: Ruby
- Homepage: http://rubyworks.github.com/functor
- Size: 121 KB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Functor
[Homepage](http://rubyworks.github.com/functor) |
[Source Code](http://github.com/rubyworks/functor) |
[Report an Issue](http://github.com/rubyworks/functor/issues)
## Synopsis
By definition a *Functor* is simply a first class method, but these are quite
common in Ruby with the `Method`, `UnboundMethod` and `Proc` classes. So for
Ruby we define a Functor as a *Higher Order Function*. Essentially, a functor
can vary its behavior according to the operation applied to it. Consider the
following simplistic example.
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
Notice that the constructor takes a block, the first argument of which is always
the method operating on the functor. All subsequent arguments are optional
dependent upon the use case.
We can also think of the Functor as an *anonymous/generic delegator*. Instead
of having to create a specialized delegator class, a functor can be used instead.
Functors are perfect when the delegation required is minimal.
**NOTE** Ruby functors are not the same as Haskell functors. In Ruby
Enumerators are more akin to Haskell's idea of a functor.
## Batteries Included
In addition to the Functor class, this library includes a dozen or so useful
core extensions that take advantage of the power of the functor. Have a look
at the demo directory for a rundown on the methods available and how they
work.
## Copyrights
Copyright (c) 2004 Rubyworks
Functor is distributable in accordance with the *BSD-2-Clause* license.
See LICENSE.txt for details.