|
7 | 7 |
|
8 | 8 | ## Synopsis |
9 | 9 |
|
10 | | -By definition a *Functor* is simply a first class method, but these are common |
11 | | -in Ruby in the form of Method and Proc classes. So for Ruby a Functor is a more |
12 | | -specialized Higher Order Function. Essentally, a Functor can vary its behavior |
13 | | -accorrding to the operation applied to it. |
| 10 | +By definition a *Functor* is simply a first class method, but these are quite |
| 11 | +common in Ruby with the `Method`, `UnboundMethod` and `Proc` classes. So for |
| 12 | +Ruby we define a Functor as a *Higher Order Function*. Essentially, a functor |
| 13 | +can vary its behavior according to the operation applied to it. Consider the |
| 14 | +following simplistic example. |
14 | 15 |
|
15 | 16 | f = Functor.new { |op, x| x.send(op, x) } |
16 | 17 |
|
17 | | - (f + 1) #=> 2 |
18 | | - (f + 2) #=> 4 |
19 | | - (f + 3) #=> 6 |
20 | | - (f * 1) #=> 1 |
21 | | - (f * 2) #=> 4 |
22 | | - (f * 3) #=> 9 |
| 18 | + f + 1 #=> 2 |
| 19 | + f + 2 #=> 4 |
| 20 | + f + 3 #=> 6 |
| 21 | + f * 1 #=> 1 |
| 22 | + f * 2 #=> 4 |
| 23 | + f * 3 #=> 9 |
23 | 24 |
|
24 | | -You can also think of the Functor as an *anonymous generic delegator*. Instead |
25 | | -of having to create a specialized delegator class, we can use a Functor. Functors |
26 | | -are perfect when the delegation require is minimal. |
| 25 | +Notice that the constructor takes a block, the first argument of which is always |
| 26 | +the method operating on the functor. All subsequent arguments are optional |
| 27 | +dependent upon the use case. |
27 | 28 |
|
28 | | -NOTE: Ruby functors are not the same thing a Haskell functors. In Ruby, the |
29 | | -Enumerator is most closely akin to Haskell's idea of a functor. |
| 29 | +We can also think of the Functor as an *anonymous/generic delegator*. Instead |
| 30 | +of having to create a specialized delegator class, a functor can be used instead. |
| 31 | +Functors are perfect when the delegation required is minimal. |
| 32 | + |
| 33 | +**NOTE** Ruby functors are not the same thing a Haskell functors. In Ruby |
| 34 | +Enumerators are most akin to Haskell's idea of a functor. |
| 35 | + |
| 36 | + |
| 37 | +## Batteries Included |
| 38 | + |
| 39 | +In addition to the Functor class, this library includes a dozen or so useful |
| 40 | +core extensions that take advantage of the power of the functor. Have a look |
| 41 | +at the demo directory for a rundown on the methods available and how they |
| 42 | +work. |
30 | 43 |
|
31 | 44 |
|
32 | 45 | ## Copyrights |
|
0 commit comments