functor 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/functor.rb +81 -51
- data/metrics/benchmark.rb +39 -0
- data/metrics/helpers.rb +6 -0
- data/metrics/many_args.rb +71 -0
- data/metrics/one_arg.rb +61 -0
- data/test/fib.rb +2 -2
- data/test/functor.rb +6 -4
- data/test/guards.rb +2 -2
- data/test/inheritance.rb +13 -8
- data/test/matchers.rb +4 -4
- data/test/reopening.rb +3 -3
- data/test/supplement.rb +20 -0
- data/test/wildcard.rb +15 -0
- data/test/with_self.rb +8 -3
- metadata +12 -35
- data/doc/README +0 -71
- data/doc/rdoc/classes/Functor.html +0 -207
- data/doc/rdoc/classes/Functor.src/M000001.html +0 -18
- data/doc/rdoc/classes/Functor.src/M000002.html +0 -19
- data/doc/rdoc/classes/Functor.src/M000003.html +0 -18
- data/doc/rdoc/classes/Functor.src/M000004.html +0 -19
- data/doc/rdoc/classes/Functor.src/M000005.html +0 -16
- data/doc/rdoc/classes/Functor/Method.html +0 -131
- data/doc/rdoc/classes/Functor/Method.src/M000006.html +0 -25
- data/doc/rdoc/classes/Object.html +0 -147
- data/doc/rdoc/classes/Object.src/M000007.html +0 -25
- data/doc/rdoc/created.rid +0 -1
- data/doc/rdoc/files/doc/HISTORY.html +0 -111
- data/doc/rdoc/files/doc/README.html +0 -210
- data/doc/rdoc/files/lib/functor_rb.html +0 -108
- data/doc/rdoc/files/lib/object_rb.html +0 -101
- data/doc/rdoc/fr_class_index.html +0 -29
- data/doc/rdoc/fr_file_index.html +0 -30
- data/doc/rdoc/fr_method_index.html +0 -33
- data/doc/rdoc/index.html +0 -24
- data/doc/rdoc/rdoc-style.css +0 -208
- data/lib/object.rb +0 -17
@@ -1,210 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
-
<!DOCTYPE html
|
3
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
-
|
6
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
-
<head>
|
8
|
-
<title>File: README</title>
|
9
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
-
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
-
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
-
<script type="text/javascript">
|
13
|
-
// <![CDATA[
|
14
|
-
|
15
|
-
function popupCode( url ) {
|
16
|
-
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
-
}
|
18
|
-
|
19
|
-
function toggleCode( id ) {
|
20
|
-
if ( document.getElementById )
|
21
|
-
elem = document.getElementById( id );
|
22
|
-
else if ( document.all )
|
23
|
-
elem = eval( "document.all." + id );
|
24
|
-
else
|
25
|
-
return false;
|
26
|
-
|
27
|
-
elemStyle = elem.style;
|
28
|
-
|
29
|
-
if ( elemStyle.display != "block" ) {
|
30
|
-
elemStyle.display = "block"
|
31
|
-
} else {
|
32
|
-
elemStyle.display = "none"
|
33
|
-
}
|
34
|
-
|
35
|
-
return true;
|
36
|
-
}
|
37
|
-
|
38
|
-
// Make codeblocks hidden by default
|
39
|
-
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
-
|
41
|
-
// ]]>
|
42
|
-
</script>
|
43
|
-
|
44
|
-
</head>
|
45
|
-
<body>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<div id="fileHeader">
|
50
|
-
<h1>README</h1>
|
51
|
-
<table class="header-table">
|
52
|
-
<tr class="top-aligned-row">
|
53
|
-
<td><strong>Path:</strong></td>
|
54
|
-
<td>doc/README
|
55
|
-
</td>
|
56
|
-
</tr>
|
57
|
-
<tr class="top-aligned-row">
|
58
|
-
<td><strong>Last Update:</strong></td>
|
59
|
-
<td>Mon Jun 16 10:19:05 -0700 2008</td>
|
60
|
-
</tr>
|
61
|
-
</table>
|
62
|
-
</div>
|
63
|
-
<!-- banner header -->
|
64
|
-
|
65
|
-
<div id="bodyContent">
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
<div id="contextContent">
|
70
|
-
|
71
|
-
<div id="description">
|
72
|
-
<p>
|
73
|
-
<a href="../../classes/Functor.html">Functor</a> provides pattern-based
|
74
|
-
function and method dispatch for Ruby, originally inspired by Topher
|
75
|
-
Cyll‘s multi gem.
|
76
|
-
</p>
|
77
|
-
<h1>Method Functors</h1>
|
78
|
-
<p>
|
79
|
-
To use it in a class:
|
80
|
-
</p>
|
81
|
-
<pre>
|
82
|
-
class Repeater
|
83
|
-
attr_accessor :times
|
84
|
-
include Functor::Method
|
85
|
-
functor( :repeat, Integer ) { |x| x * @times }
|
86
|
-
functor( :repeat, String ) { |s| [].fill( s, 0..@times ).join(' ') }
|
87
|
-
end
|
88
|
-
|
89
|
-
r = Repeater.new
|
90
|
-
r.times = 5
|
91
|
-
r.repeat( 5 ) # => 25
|
92
|
-
r.repeat( "-" ) # => "- - - - -"
|
93
|
-
r.repeat( 7.3 ) # => ArgumentError!
|
94
|
-
</pre>
|
95
|
-
<p>
|
96
|
-
Warning: This defines a class instance variable <tt>@__functors</tt> behind
|
97
|
-
the scenes as a side-effect. Also, although inheritance works within a
|
98
|
-
functor method, super does not. To call the parent method, you need to call
|
99
|
-
it explicitly using the <tt>functors</tt> class method, like this:
|
100
|
-
</p>
|
101
|
-
<pre>
|
102
|
-
A.functors[ :foo ].apply( self, 'bar' )
|
103
|
-
</pre>
|
104
|
-
<h1>Stand-Alone Functors</h1>
|
105
|
-
<p>
|
106
|
-
You can also define <a href="../../classes/Functor.html">Functor</a>
|
107
|
-
objects directly:
|
108
|
-
</p>
|
109
|
-
<pre>
|
110
|
-
fib = Functor.new do
|
111
|
-
given( 0 ) { 0 }
|
112
|
-
given( 1 ) { 1 }
|
113
|
-
given( Integer ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
|
114
|
-
end
|
115
|
-
</pre>
|
116
|
-
<p>
|
117
|
-
You can use functors directly with functions taking a block like this:
|
118
|
-
</p>
|
119
|
-
<pre>
|
120
|
-
[ *0..10 ].map( &fib ) # => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
121
|
-
</pre>
|
122
|
-
<p>
|
123
|
-
You can call a functor as a method using apply:
|
124
|
-
</p>
|
125
|
-
<pre>
|
126
|
-
fun.apply( obj, 7 )
|
127
|
-
</pre>
|
128
|
-
<p>
|
129
|
-
which is actually how the method functors are implemented.
|
130
|
-
</p>
|
131
|
-
<h1>Pattern Matching</h1>
|
132
|
-
<p>
|
133
|
-
Arguments are matched first using === and then ==, so anything that
|
134
|
-
supports these methods can be matched against. In addition, you may pass
|
135
|
-
"guards," any object that responds to call and which take and
|
136
|
-
object (the argument) and return true or false. This allows you to do
|
137
|
-
things like this:
|
138
|
-
</p>
|
139
|
-
<pre>
|
140
|
-
stripe ||= Functor.new do
|
141
|
-
given( lambda { |x| x % 2 == 0 } ) { 'white' }
|
142
|
-
given( lambda { |x| x % 2 == 1 } ) { 'silver' }
|
143
|
-
end
|
144
|
-
</pre>
|
145
|
-
<p>
|
146
|
-
which will return "white" and "silver" alternately for
|
147
|
-
a sequence of numbers.
|
148
|
-
</p>
|
149
|
-
<h1>Precedence</h1>
|
150
|
-
<p>
|
151
|
-
Precedence is defined in order of declaration: first-come, first-serve, aka
|
152
|
-
FIFO. Thus, you need to be careful in how you define your functor. The
|
153
|
-
Fibonacci example above would not work properly if the Integer pattern was
|
154
|
-
given first. That said, it is possible to redefine earlier cases, which, in
|
155
|
-
effect, "demotes" it, as if it had not been declared before. So
|
156
|
-
the following will work properly:
|
157
|
-
</p>
|
158
|
-
<pre>
|
159
|
-
fib = Functor.new do
|
160
|
-
given( Integer ) { |n| raise "this would start an infinite loop ..." }
|
161
|
-
given( 0 ) { 0 }
|
162
|
-
given( 1 ) { 1 }
|
163
|
-
# but this will "demote" the Integer pattern and now it will work ...
|
164
|
-
given( Integer ) { |n| self.call( n - 1 ) + self.call( n - 2 ) }
|
165
|
-
end
|
166
|
-
</pre>
|
167
|
-
<p>
|
168
|
-
This isn‘t perfect, but it is very easy to predict, simple to
|
169
|
-
implement, and reasonably fast, which other approaches (such as
|
170
|
-
implementing a precedence scheme) are not.
|
171
|
-
</p>
|
172
|
-
<h1>Credits And Support</h1>
|
173
|
-
<p>
|
174
|
-
<a href="../../classes/Functor.html">Functor</a> was written by Dan Yoder,
|
175
|
-
Matthew King, and Lawrence Pit. Send email to dan at zeraweb.com for
|
176
|
-
support or questions.
|
177
|
-
</p>
|
178
|
-
|
179
|
-
</div>
|
180
|
-
|
181
|
-
|
182
|
-
</div>
|
183
|
-
|
184
|
-
|
185
|
-
</div>
|
186
|
-
|
187
|
-
|
188
|
-
<!-- if includes -->
|
189
|
-
|
190
|
-
<div id="section">
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
<!-- if method_list -->
|
200
|
-
|
201
|
-
|
202
|
-
</div>
|
203
|
-
|
204
|
-
|
205
|
-
<div id="validator-badges">
|
206
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
207
|
-
</div>
|
208
|
-
|
209
|
-
</body>
|
210
|
-
</html>
|
@@ -1,108 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
-
<!DOCTYPE html
|
3
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
-
|
6
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
-
<head>
|
8
|
-
<title>File: functor.rb</title>
|
9
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
-
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
-
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
-
<script type="text/javascript">
|
13
|
-
// <![CDATA[
|
14
|
-
|
15
|
-
function popupCode( url ) {
|
16
|
-
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
-
}
|
18
|
-
|
19
|
-
function toggleCode( id ) {
|
20
|
-
if ( document.getElementById )
|
21
|
-
elem = document.getElementById( id );
|
22
|
-
else if ( document.all )
|
23
|
-
elem = eval( "document.all." + id );
|
24
|
-
else
|
25
|
-
return false;
|
26
|
-
|
27
|
-
elemStyle = elem.style;
|
28
|
-
|
29
|
-
if ( elemStyle.display != "block" ) {
|
30
|
-
elemStyle.display = "block"
|
31
|
-
} else {
|
32
|
-
elemStyle.display = "none"
|
33
|
-
}
|
34
|
-
|
35
|
-
return true;
|
36
|
-
}
|
37
|
-
|
38
|
-
// Make codeblocks hidden by default
|
39
|
-
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
-
|
41
|
-
// ]]>
|
42
|
-
</script>
|
43
|
-
|
44
|
-
</head>
|
45
|
-
<body>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<div id="fileHeader">
|
50
|
-
<h1>functor.rb</h1>
|
51
|
-
<table class="header-table">
|
52
|
-
<tr class="top-aligned-row">
|
53
|
-
<td><strong>Path:</strong></td>
|
54
|
-
<td>lib/functor.rb
|
55
|
-
</td>
|
56
|
-
</tr>
|
57
|
-
<tr class="top-aligned-row">
|
58
|
-
<td><strong>Last Update:</strong></td>
|
59
|
-
<td>Sun Jun 15 17:52:06 -0700 2008</td>
|
60
|
-
</tr>
|
61
|
-
</table>
|
62
|
-
</div>
|
63
|
-
<!-- banner header -->
|
64
|
-
|
65
|
-
<div id="bodyContent">
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
<div id="contextContent">
|
70
|
-
|
71
|
-
|
72
|
-
<div id="requires-list">
|
73
|
-
<h3 class="section-bar">Required files</h3>
|
74
|
-
|
75
|
-
<div class="name-list">
|
76
|
-
<a href="object_rb.html">lib/object</a>
|
77
|
-
</div>
|
78
|
-
</div>
|
79
|
-
|
80
|
-
</div>
|
81
|
-
|
82
|
-
|
83
|
-
</div>
|
84
|
-
|
85
|
-
|
86
|
-
<!-- if includes -->
|
87
|
-
|
88
|
-
<div id="section">
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
<!-- if method_list -->
|
98
|
-
|
99
|
-
|
100
|
-
</div>
|
101
|
-
|
102
|
-
|
103
|
-
<div id="validator-badges">
|
104
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
105
|
-
</div>
|
106
|
-
|
107
|
-
</body>
|
108
|
-
</html>
|
@@ -1,101 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
-
<!DOCTYPE html
|
3
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
-
|
6
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
-
<head>
|
8
|
-
<title>File: object.rb</title>
|
9
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
-
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
-
<link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
-
<script type="text/javascript">
|
13
|
-
// <![CDATA[
|
14
|
-
|
15
|
-
function popupCode( url ) {
|
16
|
-
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
-
}
|
18
|
-
|
19
|
-
function toggleCode( id ) {
|
20
|
-
if ( document.getElementById )
|
21
|
-
elem = document.getElementById( id );
|
22
|
-
else if ( document.all )
|
23
|
-
elem = eval( "document.all." + id );
|
24
|
-
else
|
25
|
-
return false;
|
26
|
-
|
27
|
-
elemStyle = elem.style;
|
28
|
-
|
29
|
-
if ( elemStyle.display != "block" ) {
|
30
|
-
elemStyle.display = "block"
|
31
|
-
} else {
|
32
|
-
elemStyle.display = "none"
|
33
|
-
}
|
34
|
-
|
35
|
-
return true;
|
36
|
-
}
|
37
|
-
|
38
|
-
// Make codeblocks hidden by default
|
39
|
-
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
-
|
41
|
-
// ]]>
|
42
|
-
</script>
|
43
|
-
|
44
|
-
</head>
|
45
|
-
<body>
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
<div id="fileHeader">
|
50
|
-
<h1>object.rb</h1>
|
51
|
-
<table class="header-table">
|
52
|
-
<tr class="top-aligned-row">
|
53
|
-
<td><strong>Path:</strong></td>
|
54
|
-
<td>lib/object.rb
|
55
|
-
</td>
|
56
|
-
</tr>
|
57
|
-
<tr class="top-aligned-row">
|
58
|
-
<td><strong>Last Update:</strong></td>
|
59
|
-
<td>Sat Jun 14 13:44:12 -0700 2008</td>
|
60
|
-
</tr>
|
61
|
-
</table>
|
62
|
-
</div>
|
63
|
-
<!-- banner header -->
|
64
|
-
|
65
|
-
<div id="bodyContent">
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
<div id="contextContent">
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
</div>
|
74
|
-
|
75
|
-
|
76
|
-
</div>
|
77
|
-
|
78
|
-
|
79
|
-
<!-- if includes -->
|
80
|
-
|
81
|
-
<div id="section">
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
<!-- if method_list -->
|
91
|
-
|
92
|
-
|
93
|
-
</div>
|
94
|
-
|
95
|
-
|
96
|
-
<div id="validator-badges">
|
97
|
-
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
98
|
-
</div>
|
99
|
-
|
100
|
-
</body>
|
101
|
-
</html>
|
@@ -1,29 +0,0 @@
|
|
1
|
-
|
2
|
-
<?xml version="1.0" encoding="iso-8859-1"?>
|
3
|
-
<!DOCTYPE html
|
4
|
-
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
5
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
6
|
-
|
7
|
-
<!--
|
8
|
-
|
9
|
-
Classes
|
10
|
-
|
11
|
-
-->
|
12
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
13
|
-
<head>
|
14
|
-
<title>Classes</title>
|
15
|
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
16
|
-
<link rel="stylesheet" href="rdoc-style.css" type="text/css" />
|
17
|
-
<base target="docwin" />
|
18
|
-
</head>
|
19
|
-
<body>
|
20
|
-
<div id="index">
|
21
|
-
<h1 class="section-bar">Classes</h1>
|
22
|
-
<div id="index-entries">
|
23
|
-
<a href="classes/Functor.html">Functor</a><br />
|
24
|
-
<a href="classes/Functor/Method.html">Functor::Method</a><br />
|
25
|
-
<a href="classes/Object.html">Object</a><br />
|
26
|
-
</div>
|
27
|
-
</div>
|
28
|
-
</body>
|
29
|
-
</html>
|