functor 0.3.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/doc/HISTORY +2 -1
- data/doc/README +34 -8
- data/doc/rdoc/classes/Functor.html +207 -0
- data/doc/rdoc/classes/Functor.src/M000001.html +18 -0
- data/doc/rdoc/classes/Functor.src/M000002.html +19 -0
- data/doc/rdoc/classes/Functor.src/M000003.html +18 -0
- data/doc/rdoc/classes/Functor.src/M000004.html +19 -0
- data/doc/rdoc/classes/Functor.src/M000005.html +16 -0
- data/doc/rdoc/classes/Functor/Method.html +131 -0
- data/doc/rdoc/classes/Functor/Method.src/M000006.html +25 -0
- data/doc/rdoc/classes/Object.html +147 -0
- data/doc/rdoc/classes/Object.src/M000007.html +25 -0
- data/doc/rdoc/created.rid +1 -0
- data/doc/rdoc/files/doc/HISTORY.html +111 -0
- data/doc/rdoc/files/doc/README.html +210 -0
- data/doc/rdoc/files/lib/functor_rb.html +108 -0
- data/doc/rdoc/files/lib/object_rb.html +101 -0
- data/doc/rdoc/fr_class_index.html +29 -0
- data/doc/rdoc/fr_file_index.html +30 -0
- data/doc/rdoc/fr_method_index.html +33 -0
- data/doc/rdoc/index.html +24 -0
- data/doc/rdoc/rdoc-style.css +208 -0
- data/lib/functor.rb +16 -11
- data/test/guards.rb +18 -8
- metadata +31 -2
data/lib/functor.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "#{File.dirname(__FILE__)}/object"
|
2
2
|
|
3
3
|
class Functor
|
4
4
|
|
@@ -37,28 +37,33 @@ class Functor
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def apply( object, *args, &block )
|
40
|
-
object.instance_exec( *args, &match(
|
40
|
+
object.instance_exec( *args, &match( args, &block ) )
|
41
41
|
end
|
42
42
|
|
43
43
|
def call( *args, &block )
|
44
|
-
|
45
|
-
match( *args, &block ).call( *args )
|
44
|
+
match( args, &block ).call( *args )
|
46
45
|
end
|
47
46
|
|
48
47
|
def to_proc ; lambda { |*args| self.call( *args ) } ; end
|
49
48
|
|
50
49
|
private
|
51
50
|
|
52
|
-
def match(
|
53
|
-
args
|
54
|
-
pattern, action = @rules.find { |
|
55
|
-
|
56
|
-
return action
|
51
|
+
def match( args, &block )
|
52
|
+
args << block if block_given?
|
53
|
+
pattern, action = @rules.find { | p, a | match?( args, p ) }
|
54
|
+
action or raise argument_error( args )
|
57
55
|
end
|
58
56
|
|
59
57
|
def match?( args, pattern )
|
60
|
-
|
61
|
-
|
58
|
+
args.zip( pattern ).all? { | arg, rule | pair?( arg, rule ) } if args.length == pattern.length
|
59
|
+
end
|
60
|
+
|
61
|
+
def pair?( arg, rule )
|
62
|
+
( rule.is_a?( Proc ) and rule.call( arg ) ) or rule === arg or rule == arg
|
63
|
+
end
|
64
|
+
|
65
|
+
def argument_error( args )
|
66
|
+
ArgumentError.new( "argument mismatch for argument(s): #{ args.map{ |arg| arg.inspect }.join(', ') }." )
|
62
67
|
end
|
63
68
|
|
64
69
|
end
|
data/test/guards.rb
CHANGED
@@ -1,15 +1,25 @@
|
|
1
1
|
require 'test/helpers'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
describe "Dispatch should support guards" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@stripe = Functor.new do
|
7
|
+
given( lambda { |x| x % 2 == 0 } ) { 'white' }
|
8
|
+
given( lambda { |x| x % 2 == 1 } ) { 'silver' }
|
9
|
+
end
|
7
10
|
|
8
|
-
|
11
|
+
@safe_divide = Functor.new do
|
12
|
+
given( lambda { |x| x == 0 }, Integer ) { |x,y| false }
|
13
|
+
given( Integer, Integer ) { |x,y| ( y / ( x * 1.0 )) }
|
14
|
+
end
|
15
|
+
end
|
9
16
|
|
10
|
-
specify "
|
11
|
-
[*0..9].map(
|
17
|
+
specify "such as odd or even numbers" do
|
18
|
+
[*0..9].map( &@stripe ).should == %w( white silver ) * 5
|
12
19
|
end
|
13
20
|
|
14
|
-
|
21
|
+
specify "even with multiple arguments" do
|
22
|
+
@safe_divide.call( 0,7 ).should == false
|
23
|
+
end
|
15
24
|
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: functor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Yoder
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2008-
|
14
|
+
date: 2008-07-11 00:00:00 -07:00
|
15
15
|
default_executable:
|
16
16
|
dependencies: []
|
17
17
|
|
@@ -25,6 +25,35 @@ extra_rdoc_files: []
|
|
25
25
|
|
26
26
|
files:
|
27
27
|
- doc/HISTORY
|
28
|
+
- doc/rdoc
|
29
|
+
- doc/rdoc/classes
|
30
|
+
- doc/rdoc/classes/Functor
|
31
|
+
- doc/rdoc/classes/Functor/Method.html
|
32
|
+
- doc/rdoc/classes/Functor/Method.src
|
33
|
+
- doc/rdoc/classes/Functor/Method.src/M000006.html
|
34
|
+
- doc/rdoc/classes/Functor.html
|
35
|
+
- doc/rdoc/classes/Functor.src
|
36
|
+
- doc/rdoc/classes/Functor.src/M000001.html
|
37
|
+
- doc/rdoc/classes/Functor.src/M000002.html
|
38
|
+
- doc/rdoc/classes/Functor.src/M000003.html
|
39
|
+
- doc/rdoc/classes/Functor.src/M000004.html
|
40
|
+
- doc/rdoc/classes/Functor.src/M000005.html
|
41
|
+
- doc/rdoc/classes/Object.html
|
42
|
+
- doc/rdoc/classes/Object.src
|
43
|
+
- doc/rdoc/classes/Object.src/M000007.html
|
44
|
+
- doc/rdoc/created.rid
|
45
|
+
- doc/rdoc/files
|
46
|
+
- doc/rdoc/files/doc
|
47
|
+
- doc/rdoc/files/doc/HISTORY.html
|
48
|
+
- doc/rdoc/files/doc/README.html
|
49
|
+
- doc/rdoc/files/lib
|
50
|
+
- doc/rdoc/files/lib/functor_rb.html
|
51
|
+
- doc/rdoc/files/lib/object_rb.html
|
52
|
+
- doc/rdoc/fr_class_index.html
|
53
|
+
- doc/rdoc/fr_file_index.html
|
54
|
+
- doc/rdoc/fr_method_index.html
|
55
|
+
- doc/rdoc/index.html
|
56
|
+
- doc/rdoc/rdoc-style.css
|
28
57
|
- doc/README
|
29
58
|
- lib/functor.rb
|
30
59
|
- lib/object.rb
|