lambda_driver 1.2.4 → 1.3.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.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/.travis.yml +6 -1
- data/README.md +20 -8
- data/Rakefile +3 -0
- data/lambda_driver.gemspec +1 -0
- data/lib/lambda_driver/composable.rb +50 -21
- data/lib/lambda_driver/core_ext/proc.rb +27 -1
- data/lib/lambda_driver/flipable.rb +1 -1
- data/lib/lambda_driver/version.rb +1 -1
- data/lib/lambda_driver/with_args.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- metadata +26 -23
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4c3d081ade29ea00bff0663128d511b54b8426a0120abd9547d3311272e5377c
|
4
|
+
data.tar.gz: ff7e3634ebad3dc746922706aff0cd4976f654143cb4fbfbf56b849d101370f5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 22051e3c480ffbe09b9d8e851ced3b204ab44819c56ded9b005b4fa72da4c87bb871da48ecfef3d01f5899e313bd27ddca84dd58581dd9d7a7b21b4504c01ad6
|
7
|
+
data.tar.gz: 0e5e892f72c98e9e5d6e774008ee15fac81df42ff114437be0d2b4096884966c1f8f66923dda8022cd027cc6b3f5c032ecfa9b31ae168111bbb2efea78912e4b
|
data/.rspec
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -85,12 +85,12 @@ This method is aliased as `<<`.
|
|
85
85
|
#### Proc#lift
|
86
86
|
|
87
87
|
Lift this function to the given context-function.
|
88
|
-
The lifted
|
88
|
+
The lifted function can compose other function with context-function.
|
89
89
|
|
90
90
|
The given context-fuction used by `compose_with_lifting`
|
91
|
-
to compose other
|
91
|
+
to compose other function.
|
92
92
|
|
93
|
-
The context-
|
93
|
+
The context-function should recieve 2 arguments.
|
94
94
|
|
95
95
|
- first one is a function that reciver function of `compose_with_lifting` method.
|
96
96
|
- second arg is a result of g(x)
|
@@ -115,9 +115,9 @@ see -> LambdaDriver::Context
|
|
115
115
|
#### Proc#compose_with_lifting
|
116
116
|
|
117
117
|
Compose self and given function on the context-function.
|
118
|
-
The context-
|
118
|
+
The context-function is passed by `lift` method.
|
119
119
|
|
120
|
-
This method returns composed
|
120
|
+
This method returns composed function like bellow.
|
121
121
|
|
122
122
|
```ruby
|
123
123
|
lambda{|args| context(self, g(*args)) }
|
@@ -147,7 +147,7 @@ For example, set context-function that logging the result.
|
|
147
147
|
```
|
148
148
|
|
149
149
|
if context-function does not given,
|
150
|
-
default behaivior is compose function with checking g(x) is
|
150
|
+
default behaivior is compose function with checking g(x) is mzero
|
151
151
|
|
152
152
|
if g(x) is mzero, it does not call self and return g(x),
|
153
153
|
otherwise returns f(g(x)).
|
@@ -169,6 +169,18 @@ This method is aliased as `<=`.
|
|
169
169
|
f <= g # => f.compose_with_lifting(g)
|
170
170
|
```
|
171
171
|
|
172
|
+
##### Example : try-chains
|
173
|
+
|
174
|
+
An annoying try chain like `arr.try(:first).try(:upcase).try(:to_sym)` is rewritten by following
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
arr = ["foo", "bar"]
|
178
|
+
arr.try(&:first >= :upcase >= :to_sym) # => :Foo
|
179
|
+
|
180
|
+
arr = [nil]
|
181
|
+
arr.try(&:first >= :upcase >= :to_sym) # => nil
|
182
|
+
```
|
183
|
+
|
172
184
|
#### Proc#with_args
|
173
185
|
|
174
186
|
Returns partially applied function that has 2nd and more parameters are
|
@@ -289,7 +301,7 @@ This method is aliased as `&`.
|
|
289
301
|
```ruby
|
290
302
|
f = lambda{|x| x * 2 }
|
291
303
|
|
292
|
-
"foo".revapply(f) # => "
|
304
|
+
"foo".revapply(f) # => "foofoo" (== f.call("foo") )
|
293
305
|
```
|
294
306
|
|
295
307
|
#### Object#_
|
@@ -312,7 +324,7 @@ if f(self) is nil, returns self, otherwise return f(self).
|
|
312
324
|
```ruby
|
313
325
|
f = lambda{|x| x % 2 == 0 ? nil : x * 2}
|
314
326
|
|
315
|
-
2.disjunction(f) # => 2 (disjunction returns
|
327
|
+
2.disjunction(f) # => 2 (disjunction returns receiver object)
|
316
328
|
3.disjunction(f) # => 6 (disjunction returns f(3) )
|
317
329
|
```
|
318
330
|
|
data/Rakefile
CHANGED
data/lambda_driver.gemspec
CHANGED
@@ -1,27 +1,56 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
module LambdaDriver::Composable
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
3
|
+
if RUBY_VERSION < "2.6.0"
|
4
|
+
# Returns new lambda which composed self and given function.
|
5
|
+
# A composed proc called with args, executes `self.(g(*args)).
|
6
|
+
#
|
7
|
+
# f = lambda{|x| x.to_s }
|
8
|
+
# g = lambda{|y| y.length }
|
9
|
+
# h = f compose g
|
10
|
+
# h.(:hoge) # => 4
|
11
|
+
#
|
12
|
+
# This method is aliased as `<<`.
|
13
|
+
#
|
14
|
+
# f << g # => f.compose(g)
|
15
|
+
#
|
16
|
+
def compose(g)
|
17
|
+
lambda{|*args|
|
18
|
+
self.to_proc.call(g.to_proc.call(*args))
|
19
|
+
}
|
20
|
+
end
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
# g compose self
|
23
|
+
def >>(g)
|
24
|
+
g.to_proc << self
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.included(klass)
|
28
|
+
klass.send(:alias_method, :<<, :compose)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
# Returns new lambda which composed self and given function.
|
32
|
+
# A composed proc called with args, executes `self.(g(*args)).
|
33
|
+
#
|
34
|
+
# f = lambda{|x| x.to_s }
|
35
|
+
# g = lambda{|y| y.length }
|
36
|
+
# h = f compose g
|
37
|
+
# h.(:hoge) # => 4
|
38
|
+
#
|
39
|
+
# This method is aliased as `<<`.
|
40
|
+
#
|
41
|
+
# f << g # => f.compose(g)
|
42
|
+
#
|
43
|
+
def compose(g)
|
44
|
+
self.to_proc << g.to_proc
|
45
|
+
end
|
46
|
+
|
47
|
+
# g compose self
|
48
|
+
def >>(g)
|
49
|
+
g.to_proc << self
|
50
|
+
end
|
23
51
|
|
24
|
-
|
25
|
-
|
52
|
+
def self.included(klass)
|
53
|
+
klass.send(:alias_method, :<<, :compose)
|
54
|
+
end
|
26
55
|
end
|
27
56
|
end
|
@@ -1,10 +1,36 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
# Override Proc#>> and << these are implemented from Ruby 2.6
|
4
|
+
# to ensure passed argument is callable.
|
5
|
+
# Convert argument to proc obj by calling :to_proc before call super implementaion
|
6
|
+
module ProcOverride
|
7
|
+
def compose(g)
|
8
|
+
self << g
|
9
|
+
end
|
10
|
+
|
11
|
+
# g compose self
|
12
|
+
def <<(g)
|
13
|
+
super(g.to_proc)
|
14
|
+
end
|
15
|
+
|
16
|
+
# g compose self
|
17
|
+
def >>(g)
|
18
|
+
g.to_proc << self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
2
22
|
class Proc
|
3
23
|
include LambdaDriver::Callable
|
4
|
-
include LambdaDriver::Composable
|
5
24
|
include LambdaDriver::WithArgs
|
6
25
|
include LambdaDriver::Flipable
|
7
26
|
include LambdaDriver::ProcConvertable
|
8
27
|
include LambdaDriver::Currying
|
9
28
|
include LambdaDriver::Liftable
|
29
|
+
|
30
|
+
if RUBY_VERSION < "2.6.0"
|
31
|
+
include LambdaDriver::Composable
|
32
|
+
else
|
33
|
+
prepend ProcOverride
|
34
|
+
end
|
10
35
|
end
|
36
|
+
|
@@ -3,7 +3,7 @@ module LambdaDriver::Flipable
|
|
3
3
|
# Returns function whose parameter order spawed 1st for 2nd.
|
4
4
|
# A result of filped fuction is curried by Proc#curry.
|
5
5
|
#
|
6
|
-
# f =
|
6
|
+
# f = lambda{|x, y, z| [x, y, z]}
|
7
7
|
# h = f.flip
|
8
8
|
# h.(:a).(:b).(:c) # => [:b, :a, :c]
|
9
9
|
#
|
data/spec/spec_helper.rb
CHANGED
@@ -12,7 +12,7 @@ require 'lambda_driver'
|
|
12
12
|
Dir["#{File.dirname(__FILE__)}/shared/**/*.rb"].each {|f| require f}
|
13
13
|
|
14
14
|
RSpec.configure do |config|
|
15
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
16
15
|
config.run_all_when_everything_filtered = true
|
17
16
|
config.filter_run :focus
|
17
|
+
config.expect_with(:rspec) { |c| c.syntax = :should }
|
18
18
|
end
|
metadata
CHANGED
@@ -1,30 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lambda_driver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Tomohito Ozaki
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2019-01-11 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
14
27
|
- !ruby/object:Gem::Dependency
|
15
28
|
name: rspec
|
16
29
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
30
|
requirements:
|
19
|
-
- -
|
31
|
+
- - ">="
|
20
32
|
- !ruby/object:Gem::Version
|
21
33
|
version: '0'
|
22
34
|
type: :development
|
23
35
|
prerelease: false
|
24
36
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
37
|
requirements:
|
27
|
-
- -
|
38
|
+
- - ">="
|
28
39
|
- !ruby/object:Gem::Version
|
29
40
|
version: '0'
|
30
41
|
description: Drives your code more functioal!
|
@@ -34,9 +45,9 @@ executables: []
|
|
34
45
|
extensions: []
|
35
46
|
extra_rdoc_files: []
|
36
47
|
files:
|
37
|
-
- .gitignore
|
38
|
-
- .rspec
|
39
|
-
- .travis.yml
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
40
51
|
- Gemfile
|
41
52
|
- LICENSE.txt
|
42
53
|
- README.md
|
@@ -79,33 +90,25 @@ files:
|
|
79
90
|
- spec/unbound_method_spec.rb
|
80
91
|
homepage: http://yuroyoro.github.com/lambda_driver/
|
81
92
|
licenses: []
|
93
|
+
metadata: {}
|
82
94
|
post_install_message:
|
83
95
|
rdoc_options: []
|
84
96
|
require_paths:
|
85
97
|
- lib
|
86
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
-
none: false
|
88
99
|
requirements:
|
89
|
-
- -
|
100
|
+
- - ">="
|
90
101
|
- !ruby/object:Gem::Version
|
91
102
|
version: '0'
|
92
|
-
segments:
|
93
|
-
- 0
|
94
|
-
hash: -3867802259008164465
|
95
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
-
none: false
|
97
104
|
requirements:
|
98
|
-
- -
|
105
|
+
- - ">="
|
99
106
|
- !ruby/object:Gem::Version
|
100
107
|
version: '0'
|
101
|
-
segments:
|
102
|
-
- 0
|
103
|
-
hash: -3867802259008164465
|
104
108
|
requirements: []
|
105
|
-
|
106
|
-
rubygems_version: 1.8.23
|
109
|
+
rubygems_version: 3.0.1
|
107
110
|
signing_key:
|
108
|
-
specification_version:
|
111
|
+
specification_version: 4
|
109
112
|
summary: Drives your code more functioal!
|
110
113
|
test_files:
|
111
114
|
- spec/class_spec.rb
|