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
@@ -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
-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
A.functors[ :foo ].apply( self, 'bar' )
0
You can also define Functor objects directly:
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
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
-which is actually how the method dispatch is implemented.
0
+which is actually how the method functors are implemented.
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
@@ -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
-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
+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
+ given( Integer ) { |n| raise "this would start an infinite loop ..." }
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
+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
+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.