abstractivator 0.0.24 → 0.0.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/abstractivator/proc_ext.rb +14 -0
- data/lib/abstractivator/version.rb +1 -1
- data/spec/lib/abstractivator/proc_ext_spec.rb +19 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c7615cf2a1920441356f004c04e538266bcff8a
|
4
|
+
data.tar.gz: 3cbafbd8d4d96004a3bb71ad866559f84efc482e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 505a6935ad5889bcb7cfdfac6fb95cf16a043e08723f48bd21eac5a3a43274f4b99474bc054b177a45c9a8e55cea551ed49f720dacf9f8ab5343ff6999bd053b
|
7
|
+
data.tar.gz: 71b33fcc5c8399291aa465943cbbd112958c5b9a6db041c7f21148f2dacedd507132db0fc23614ea5f70214e75e99f6cf95eed06323fe3c66b61426dcb23606a
|
@@ -25,6 +25,20 @@ class Proc
|
|
25
25
|
procs.map(&:to_proc).inject_right(identity) { |inner, p| p.compose(inner) }
|
26
26
|
end
|
27
27
|
|
28
|
+
# composes procedures in reverse order.
|
29
|
+
# useful for applying a series of transformations.
|
30
|
+
# pipe(f, g, h) returns the procedure
|
31
|
+
# proc { |x| h.call(g.call(f.call(x))) }
|
32
|
+
def self.pipe(*procs)
|
33
|
+
Proc.compose(*procs.reverse)
|
34
|
+
end
|
35
|
+
|
36
|
+
# makes a pipeline transform as with Proc::pipe
|
37
|
+
# and applies it to the given value.
|
38
|
+
def self.pipe_value(value, *procs)
|
39
|
+
Proc.pipe(*procs).call(value)
|
40
|
+
end
|
41
|
+
|
28
42
|
# returns the identity function
|
29
43
|
def self.identity
|
30
44
|
proc {|x| x}
|
@@ -25,6 +25,25 @@ context 'in the world of functional programming' do
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
+
describe 'Proc::pipe' do
|
29
|
+
it 'composes procs in reverse order' do
|
30
|
+
expect(Proc.pipe.call(3)).to eql 3
|
31
|
+
expect(Proc.pipe(double).call(3)).to eql 6
|
32
|
+
expect(Proc.pipe(double, square).call(3)).to eql 36
|
33
|
+
expect(Proc.pipe(double, square, negate).call(3)).to eql -36
|
34
|
+
end
|
35
|
+
it 'coerces the args to a proc with to_proc' do
|
36
|
+
p = Proc.pipe(:first, :abs)
|
37
|
+
expect(p.call([-5, 6])).to eql 5
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'Proc::pipe_value' do
|
42
|
+
it 'makes a Proc::pipe pipeline and applies it to a value' do
|
43
|
+
expect(Proc.pipe_value(3, double, square, negate)).to eql -36
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
28
47
|
describe 'Proc#reverse_args' do
|
29
48
|
it 'reverse argument order' do
|
30
49
|
divide = proc {|a, b| a / b}
|