GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Implements pattern-based method dispatch for Ruby, inspired by Topher Cyll's multi.
Clone URL: git://github.com/dyoder/functor.git
Updated docs.
dyoder (author)
Mon Jun 16 10:20:58 -0700 2008
commit  0c45188e0a96235c33c3f8bd71b487a6daed670b
tree    1be9ddb3dac8376941b313d0db9bf7284e657559
parent  d2f0932485a31b610edf8b580ffd59296bedc71c
...
1
 
 
 
 
 
2
3
4
...
13
14
15
16
 
17
18
19
 
 
20
21
22
23
24
25
 
26
27
28
...
33
34
35
36
 
 
 
37
38
39
...
42
43
44
45
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
...
 
1
2
3
4
5
6
7
8
...
17
18
19
 
20
21
22
23
24
25
26
27
28
29
30
 
31
32
33
34
...
39
40
41
 
42
43
44
45
46
47
...
50
51
52
 
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
0
@@ -1,4 +1,8 @@
0
-Functor provides pattern-based function and method dispatch for Ruby. To use it in a class:
0
+Functor provides pattern-based function and method dispatch for Ruby, originally inspired by Topher Cyll's multi gem.
0
+
0
+= Method Functors
0
+
0
+To use it in a class:
0
 
0
   class Repeater
0
     attr_accessor :times
0
@@ -13,16 +17,18 @@ Functor provides pattern-based function and method dispatch for Ruby. To use it
0
   r.repeat( "-" ) # => "- - - - -"
0
   r.repeat( 7.3 ) # => ArgumentError!
0
 
0
-Warning: This defines a class instance variable @__functors behind the scenes as a side-effect. Also, although inheritance works within a functor method, super does not. To call the parent method, you need to call it explicitly using the #functors class method, like this:
0
+Warning: This defines a class instance variable <tt>@__functors</tt> behind the scenes as a side-effect. Also, although inheritance works within a functor method, super does not. To call the parent method, you need to call it explicitly using the <tt>#functors</tt> class method, like this:
0
 
0
   A.functors[ :foo ].apply( self, 'bar' )
0
 
0
+= Stand-Alone Functors
0
+
0
 You can also define Functor objects directly:
0
 
0
   fib = Functor.new do
0
     given( 0 ) { 0 }
0
     given( 1 ) { 1 }
0
- given( 2..10000 ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
0
+ given( Integer ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
0
   end
0
   
0
 You can use functors directly with functions taking a block like this:
0
@@ -33,7 +39,9 @@ You can call a functor as a method using #apply:
0
 
0
   fun.apply( obj, 7 )
0
   
0
-which is actually how the method dispatch is implemented.
0
+which is actually how the method functors are implemented.
0
+
0
+= Pattern Matching
0
 
0
 Arguments are matched first using === and then ==, so anything that supports these methods can be matched against. In addition, you may pass "guards," any object that responds to #call and which take and object (the argument) and return true or false. This allows you to do things like this:
0
 
0
@@ -42,4 +50,22 @@ Arguments are matched first using === and then ==, so anything that supports the
0
     given( lambda { |x| x % 2 == 1 } ) { 'silver' }
0
   end
0
 
0
-which will return "white" and "silver" alternately for a sequence of numbers.
0
\ No newline at end of file
0
+which will return "white" and "silver" alternately for a sequence of numbers.
0
+
0
+= Precedence
0
+
0
+Precedence is defined in order of declaration: first-come, first-serve, aka FIFO. Thus, you need to be careful in how you define your functor. The Fibonacci example above would not work properly if the Integer pattern was given first. That said, it is possible to redefine earlier cases, which, in effect, "demotes" it, as if it had not been declared before. So the following will work properly:
0
+
0
+ fib = Functor.new do
0
+ given( Integer ) { |n| raise "this would start an infinite loop ..." }
0
+ given( 0 ) { 0 }
0
+ given( 1 ) { 1 }
0
+ # but this will "demote" the Integer pattern and now it will work ...
0
+ given( Integer ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
0
+ end
0
+
0
+This isn't perfect, but it is very easy to predict, simple to implement, and reasonably fast, which other approaches (such as implementing a precedence scheme) are not.
0
+
0
+= Credits And Support
0
+
0
+Functor was written by Dan Yoder, Matthew King, and Lawrence Pit. Send email to dan at zeraweb.com for support or questions.
0
\ No newline at end of file

Comments

    No one has commented yet.