lambda_driver 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +70 -26
- data/lib/lambda_driver/core_ext/object.rb +2 -1
- data/lib/lambda_driver/core_ext/symbol.rb +6 -0
- data/lib/lambda_driver/disjunction.rb +9 -0
- data/lib/lambda_driver/revapply.rb +9 -0
- data/lib/lambda_driver/version.rb +1 -1
- data/spec/disjunction_spec.rb +80 -0
- data/spec/revapply_spec.rb +41 -0
- data/spec/symbol_spec.rb +26 -3
- metadata +10 -7
- data/lib/lambda_driver/ap.rb +0 -13
- data/spec/ap_spec.rb +0 -41
data/README.md
CHANGED
@@ -43,61 +43,79 @@ Or install it yourself as:
|
|
43
43
|
#### alias :< to Proc#call
|
44
44
|
|
45
45
|
```ruby
|
46
|
-
f =
|
46
|
+
f = lambda{|x| x.to_s }
|
47
47
|
f < :foo # => "foo"
|
48
48
|
```
|
49
49
|
|
50
|
+
#### alias :+@ to Proc#to_proc
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
+:to_s # => #<Proc:0x007ff78aadaa78>
|
54
|
+
+:to_s < :foo # => "foo"
|
55
|
+
```
|
56
|
+
|
57
|
+
|
50
58
|
#### Proc#compose
|
51
59
|
|
52
60
|
Returns new lambda which composed self and given function.
|
53
61
|
A composed proc called with args, executes `self.(g(*args)).
|
54
62
|
|
55
63
|
```ruby
|
56
|
-
f =
|
64
|
+
f = lambda{|x| x.to_s * 2 }
|
57
65
|
g = lambda{|y| y.length }
|
58
|
-
|
59
|
-
h.
|
66
|
+
|
67
|
+
h = f.compose g # => #<Proc:0x007ff78aa2ab2>
|
68
|
+
h.(:hoge) # => "44" ( == f.call(g.call(:hoge)) )
|
60
69
|
```
|
61
70
|
|
62
71
|
This method is aliased as `<<`.
|
63
72
|
|
64
73
|
```ruby
|
65
|
-
f << g
|
74
|
+
f << g # => f.compose(g)
|
75
|
+
f << g < :hoge # => "44" ( == f.call(g.call(:hoge)) )
|
66
76
|
```
|
67
77
|
|
68
78
|
#### Proc#with_args
|
69
79
|
|
70
|
-
Returns partially applied function that has 2nd and more parameters
|
80
|
+
Returns partially applied function that has 2nd and more parameters are
|
71
81
|
fixed by given *args.
|
72
82
|
|
73
83
|
```ruby
|
74
|
-
f =
|
75
|
-
|
76
|
-
h.(:
|
84
|
+
f = lambda{|x, y, z| [x, y, z]}
|
85
|
+
|
86
|
+
h = f.with_args(:a, :b) # => #<Proc:0x007ff78a9c5ca0>
|
87
|
+
h.(:c) # => [:c, :a, :b] ( == f.call(:c, :a, :b) )
|
77
88
|
```
|
78
89
|
|
79
90
|
This method is aliased as `*`.
|
80
91
|
|
81
92
|
```ruby
|
82
|
-
f
|
93
|
+
f = lambda{|x, y| [x, y]}
|
94
|
+
|
95
|
+
f * :foo # => #<Proc:0x007ff78a987540> (== f.with_args(:foo) )
|
96
|
+
f * :foo < :bar # => [:bar, :foo] ( == f.with_args(:foo).call(:bar) )
|
83
97
|
```
|
84
98
|
|
85
99
|
|
86
100
|
#### Proc#flip
|
87
101
|
|
88
|
-
Returns function whose parameter order
|
102
|
+
Returns function whose parameter order swaped 1st for 2nd.
|
89
103
|
A result of filped fuction is curried by Proc#curry.
|
90
104
|
|
91
105
|
```ruby
|
92
|
-
f =
|
93
|
-
|
94
|
-
h.
|
106
|
+
f = lambda{|x, y, z| [x, y, z]}
|
107
|
+
|
108
|
+
h = f.flip # => #<Proc:0x007ff78a942fa>
|
109
|
+
h.call(:a).call(:b).call(:c) # => [:b, :a, :c] (== f.curry.call(:b).call(:a).call(:b))
|
110
|
+
h < :a < :b < :c # => [:b, :a, :c] (== f.curry.call(:b).call(:a).call(:b))
|
95
111
|
```
|
96
112
|
|
97
113
|
If arguments is var-args, pass explicitly arity to curring.
|
98
114
|
|
99
115
|
```ruby
|
100
116
|
p = Proc.new{|*args| args.inspect }
|
117
|
+
|
118
|
+
p.arity # => -1
|
101
119
|
p.flip(3).call(:a).(:b).(:c) # => "[:b, :a, :c]"
|
102
120
|
p.flip(4).call(:a).(:b).(:c).(:d) # => "[:b, :a, :c, :d]"
|
103
121
|
```
|
@@ -107,11 +125,26 @@ If arity is 0 or 1, flip returns itself.
|
|
107
125
|
This method is aliased as `~@`.
|
108
126
|
|
109
127
|
```ruby
|
110
|
-
~f # => f.filp
|
128
|
+
~f # => #<Proc:0x007ff78a8e22c> (== f.filp)
|
129
|
+
~f < :a < :b < :c # => [:b, :a, :c] (== f.filp.call(:b).call(:a).call(:b))
|
130
|
+
|
111
131
|
```
|
112
132
|
|
113
|
-
|
114
|
-
|
133
|
+
### Symbol extensions
|
134
|
+
- to_method
|
135
|
+
|
136
|
+
#### Symbol#to_method
|
137
|
+
|
138
|
+
Symbol#to_method generates a function that extract Method object from given arguments.
|
139
|
+
This method is aliased as `-@`.
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
(-:index).call("foobarbaz") # => #<Method: String#index>
|
143
|
+
(-:index).call("foobarbaz").call("bar") # => 3 (== "foobarbaz".index(3) )
|
144
|
+
|
145
|
+
-:index < "foobarbaz" # => #<Method: String#index>
|
146
|
+
-:index < "foobarbaz" < "bar" # => 3 (== "foobarbaz".index(3) )
|
147
|
+
```
|
115
148
|
|
116
149
|
### Class extensions
|
117
150
|
- alias instance_method, :/
|
@@ -134,32 +167,43 @@ This method is aliased as `~@`.
|
|
134
167
|
- ski combinator
|
135
168
|
|
136
169
|
### Object extensions
|
137
|
-
-
|
138
|
-
- obj._
|
139
|
-
|
170
|
+
- obj.ap(|>)
|
171
|
+
- obj._
|
172
|
+
- obj.disjunction(f)
|
140
173
|
|
141
|
-
#### Object#
|
174
|
+
#### Object#revapply
|
142
175
|
|
143
|
-
`Object#
|
176
|
+
`Object#revapply` is applies self to given proc/block.
|
144
177
|
|
145
178
|
```ruby
|
146
179
|
f = lambda{|x| x * 2 }
|
147
|
-
|
180
|
+
|
181
|
+
"foo".revapply(f) # => "fooffoo" (== f.call("foo") )
|
148
182
|
```
|
149
183
|
|
150
184
|
#### Object#_
|
151
185
|
|
152
|
-
Object#
|
186
|
+
Object#_ is shortcut to quickly extract Method object.
|
153
187
|
|
154
188
|
```ruby
|
155
|
-
"foobarbaz"._.index
|
189
|
+
"foobarbaz"._.index # => #<Method: String#index>
|
156
190
|
"foobarbaz"._.index < "bar" # => 3 (== "foobarbaz".index(3) )
|
157
191
|
|
158
|
-
2._(:>=)
|
192
|
+
2._(:>=) # => #<Method: Fixnum#>=>
|
159
193
|
[1, 2, 3].select(&2._(:>=)) # => [1, 2]( = [1, 2].select{|n| 2 >= n})
|
160
194
|
```
|
161
195
|
|
196
|
+
#### Object#disjunction
|
197
|
+
|
198
|
+
`Object#disjunction` select self or result of applied self to given function.
|
199
|
+
if f(self) is nil, returns self, otherwise return f(self).
|
162
200
|
|
201
|
+
```ruby
|
202
|
+
f = lambda{|x| x % 2 == 0 ? nil : x * 2}
|
203
|
+
|
204
|
+
2.disjunction(f) # => 2 (disjunction returns reciever object)
|
205
|
+
3.disjunction(f) # => 6 (disjunction returns f(3) )
|
206
|
+
```
|
163
207
|
|
164
208
|
## Contributing
|
165
209
|
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LambdaDriver::Disjunction do
|
4
|
+
let(:obj) { "foobarbaz" }
|
5
|
+
let(:f) { lambda{|x| x * 2 } }
|
6
|
+
|
7
|
+
describe '#disjunction'do
|
8
|
+
context 'f(obj) returns nil' do
|
9
|
+
context 'given none' do
|
10
|
+
subject { obj.disjunction }
|
11
|
+
|
12
|
+
it { should be_a_kind_of Proc }
|
13
|
+
|
14
|
+
it('obj.disjunction.call(f) should be obj'){
|
15
|
+
subject.call(lambda{|x| nil}).should == obj
|
16
|
+
}
|
17
|
+
it('obj.disjunction.call{block} should be obj'){
|
18
|
+
subject.call{nil}.should == obj
|
19
|
+
}
|
20
|
+
it('obj.disjunction.call(f){block} should obj'){
|
21
|
+
subject.call(f){nil}.should == obj
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'block given' do
|
26
|
+
it('obj.disjunction{block} should be result of execute block with obj'){
|
27
|
+
obj.disjunction{nil}.should == obj
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'proc object given' do
|
32
|
+
it('obj.ap(f) should be f.call(obj)'){
|
33
|
+
obj.disjunction(lambda{|x| nil}).should == obj
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'block given and proc object given' do
|
38
|
+
it('obj.ap.call(f){block} should be result of execute block with obj'){
|
39
|
+
obj.disjunction(f){nil}.should == obj
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'f(obj) does not returns nil' do
|
45
|
+
context 'given none' do
|
46
|
+
subject { obj.disjunction }
|
47
|
+
|
48
|
+
it { should be_a_kind_of Proc }
|
49
|
+
|
50
|
+
it('obj.disjunction.call(f) should be obj'){
|
51
|
+
subject.call(f).should == f.call(obj)
|
52
|
+
}
|
53
|
+
it('obj.disjunction.call{block} should be result of execute block with obj'){
|
54
|
+
subject.call{|x| x * 3 }.should == obj * 3
|
55
|
+
}
|
56
|
+
it('obj.disjunction.call(f){block} should be result of execute block with obj'){
|
57
|
+
subject.call(f){|x| x * 3 }.should == obj * 3
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'block given' do
|
62
|
+
it('obj.disjunction{block} should be result of execute block with obj'){
|
63
|
+
obj.disjunction{|x| x * 3 }.should == (obj * 3)
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'proc object given' do
|
68
|
+
it('obj.ap(f) should be f.call(obj)'){
|
69
|
+
obj.disjunction(f).should == f.call(obj)
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'block given and proc object given' do
|
74
|
+
it('obj.ap.call(f){block} should be result of execute block with obj'){
|
75
|
+
obj.disjunction(f){|x| x * 3 }.should == (obj * 3)
|
76
|
+
}
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LambdaDriver::Revapply do
|
4
|
+
let(:object) { "foobarbaz" }
|
5
|
+
let(:f) { lambda{|x| x * 2 } }
|
6
|
+
|
7
|
+
describe '#revapply'do
|
8
|
+
context 'given none' do
|
9
|
+
subject { object.revapply }
|
10
|
+
|
11
|
+
it { should be_a_kind_of Proc }
|
12
|
+
it('obj.revapply.call(f) should be f.call(obj)'){
|
13
|
+
subject.call(f).should == f.call(object)
|
14
|
+
}
|
15
|
+
it('obj.revapply.call{block} should be result of execute block with obj'){
|
16
|
+
subject.call{|x| x * 3 }.should == (object * 3)
|
17
|
+
}
|
18
|
+
it('obj.revapply.call(f){block} should be result of execute block with obj'){
|
19
|
+
subject.call(f){|x| x * 3 }.should == (object * 3)
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'block given' do
|
24
|
+
it('obj.revapply{block} should be result of execute block with obj'){
|
25
|
+
object.revapply{|x| x * 3 }.should == (object * 3)
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'proc object given' do
|
30
|
+
it('obj.revapply(f) should be f.call(obj)'){
|
31
|
+
object.revapply(f).should == f.call(object)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'block given and proc object given' do
|
36
|
+
it('obj.revapply.call(f){block} should be result of execute block with obj'){
|
37
|
+
object.revapply(f){|x| x * 3 }.should == (object * 3)
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/symbol_spec.rb
CHANGED
@@ -55,9 +55,32 @@ describe Symbol do
|
|
55
55
|
it_should_behave_like 'aliases'
|
56
56
|
|
57
57
|
it_should_behave_like 'aliases(varargs)' do
|
58
|
-
subject { :
|
59
|
-
let(:x) {
|
60
|
-
let(:y) {
|
58
|
+
subject { :delete}
|
59
|
+
let(:x) { :bar }
|
60
|
+
let(:y) { 'a' }
|
61
61
|
end
|
62
62
|
end
|
63
|
+
|
64
|
+
describe '#to_method' do
|
65
|
+
subject { :index.to_method }
|
66
|
+
|
67
|
+
let(:obj) { "foobarbaz" }
|
68
|
+
|
69
|
+
it { should be_a_kind_of Proc }
|
70
|
+
it('symbol.to_method.call(obj) should returns Method'){
|
71
|
+
subject.call(obj).should be_a_kind_of Method
|
72
|
+
}
|
73
|
+
|
74
|
+
it('symbol.to_method.call(obj).call(x) should be obj.method(symbol).call(x)'){
|
75
|
+
subject.call(obj).call("bar").should == obj.method(:index).call("bar")
|
76
|
+
}
|
77
|
+
|
78
|
+
it('-:symbol.to_method.call(obj) should returns Method'){
|
79
|
+
(-:index).call(obj).should be_a_kind_of Method
|
80
|
+
}
|
81
|
+
|
82
|
+
it('-:symbol.to_method.call(obj).call(x) should be obj.method(symbol).call(x)'){
|
83
|
+
(-:index).call(obj).call("bar").should == obj.method(:index).call("bar")
|
84
|
+
}
|
85
|
+
end
|
63
86
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lambda_driver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-27 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70294430507480 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70294430507480
|
25
25
|
description: Drives your code more functioal!
|
26
26
|
email:
|
27
27
|
- ozaki@yuroyoro.com
|
@@ -37,7 +37,6 @@ files:
|
|
37
37
|
- Rakefile
|
38
38
|
- lambda_driver.gemspec
|
39
39
|
- lib/lambda_driver.rb
|
40
|
-
- lib/lambda_driver/ap.rb
|
41
40
|
- lib/lambda_driver/callable.rb
|
42
41
|
- lib/lambda_driver/core_ext.rb
|
43
42
|
- lib/lambda_driver/core_ext/class.rb
|
@@ -47,14 +46,17 @@ files:
|
|
47
46
|
- lib/lambda_driver/core_ext/symbol.rb
|
48
47
|
- lib/lambda_driver/core_ext/unbound_method.rb
|
49
48
|
- lib/lambda_driver/currying.rb
|
49
|
+
- lib/lambda_driver/disjunction.rb
|
50
50
|
- lib/lambda_driver/op.rb
|
51
|
+
- lib/lambda_driver/revapply.rb
|
51
52
|
- lib/lambda_driver/version.rb
|
52
|
-
- spec/ap_spec.rb
|
53
53
|
- spec/class_spec.rb
|
54
|
+
- spec/disjunction_spec.rb
|
54
55
|
- spec/lambda_spec.rb
|
55
56
|
- spec/method_spec.rb
|
56
57
|
- spec/op_spec.rb
|
57
58
|
- spec/proc_spec.rb
|
59
|
+
- spec/revapply_spec.rb
|
58
60
|
- spec/shared/composable_spec.rb
|
59
61
|
- spec/spec_helper.rb
|
60
62
|
- spec/symbol_spec.rb
|
@@ -84,12 +86,13 @@ signing_key:
|
|
84
86
|
specification_version: 3
|
85
87
|
summary: Drives your code more functioal!
|
86
88
|
test_files:
|
87
|
-
- spec/ap_spec.rb
|
88
89
|
- spec/class_spec.rb
|
90
|
+
- spec/disjunction_spec.rb
|
89
91
|
- spec/lambda_spec.rb
|
90
92
|
- spec/method_spec.rb
|
91
93
|
- spec/op_spec.rb
|
92
94
|
- spec/proc_spec.rb
|
95
|
+
- spec/revapply_spec.rb
|
93
96
|
- spec/shared/composable_spec.rb
|
94
97
|
- spec/spec_helper.rb
|
95
98
|
- spec/symbol_spec.rb
|
data/lib/lambda_driver/ap.rb
DELETED
data/spec/ap_spec.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe LambdaDriver::Ap do
|
4
|
-
let(:object) { "foobarbaz" }
|
5
|
-
let(:f) { lambda{|x| x * 2 } }
|
6
|
-
|
7
|
-
describe '#ap'do
|
8
|
-
context 'given none' do
|
9
|
-
subject { object.ap }
|
10
|
-
|
11
|
-
it { should be_a_kind_of Proc }
|
12
|
-
it('obj.ap.call(f) should be f.call(obj)'){
|
13
|
-
subject.call(f).should == f.call(object)
|
14
|
-
}
|
15
|
-
it('obj.ap.call{block} should be result of execute block with obj'){
|
16
|
-
subject.call{|x| x * 3 }.should == (object * 3)
|
17
|
-
}
|
18
|
-
it('obj.ap.call(f){block} should be result of execute block with obj'){
|
19
|
-
subject.call(f){|x| x * 3 }.should == (object * 3)
|
20
|
-
}
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'block given' do
|
24
|
-
it('obj.ap{block} should be result of execute block with obj'){
|
25
|
-
object.ap{|x| x * 3 }.should == (object * 3)
|
26
|
-
}
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'proc object given' do
|
30
|
-
it('obj.ap(f) should be f.call(obj)'){
|
31
|
-
object.ap(f).should == f.call(object)
|
32
|
-
}
|
33
|
-
end
|
34
|
-
|
35
|
-
context 'block given and proc object given' do
|
36
|
-
it('obj.ap.call(f){block} should be result of execute block with obj'){
|
37
|
-
object.ap(f){|x| x * 3 }.should == (object * 3)
|
38
|
-
}
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|