arrows 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/arrows.rb +13 -1
- data/lib/arrows/version.rb +1 -1
- data/spec/arrows/proc_spec.rb +49 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b8e5f6e4eb4a43c2f35518b07bf4b9f0a8a48f4
|
4
|
+
data.tar.gz: b28cecf3536eba6249380a4b1b95ccebe70f31c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dca8f5f4ff089919b40f3aad4ea9bc074c54be226dd39cefd4f52376a9dcb85946472cce5cbd2b04a39904adaf7f91759967c13fdb9708ab49958df18e2f70f2
|
7
|
+
data.tar.gz: 4da8524c8d276ee903bdc14176733ad99b9157ec2621b59fd35d5b984140b0e56badfb292633793aa29e7b9feb550dc263f45635a97d419365ccad96f4eb0e72
|
data/README.md
CHANGED
@@ -7,6 +7,7 @@ actually useful set of functional tools for day-to-day Ruby programming
|
|
7
7
|
|
8
8
|
Features:
|
9
9
|
|
10
|
+
*note, see spec/arrows/proc_spec.rb to get an idea how to use this junk
|
10
11
|
### Function composition
|
11
12
|
|
12
13
|
If given
|
@@ -17,6 +18,7 @@ x -> H -> z
|
|
17
18
|
|
18
19
|
As in we pipe what F poops out into the mouth of G a la Human Centipede
|
19
20
|
|
21
|
+
|
20
22
|
### Applicative composition
|
21
23
|
Calls map (in Haskell, they generalize it to fmap) on the data passed in
|
22
24
|
|
data/lib/arrows.rb
CHANGED
@@ -2,6 +2,11 @@ require "arrows/version"
|
|
2
2
|
|
3
3
|
module Arrows
|
4
4
|
class << self
|
5
|
+
def concurrent(f,g)
|
6
|
+
Arrows::Proc.new do |*args|
|
7
|
+
[f[*args.first], g[*args.last]]
|
8
|
+
end
|
9
|
+
end
|
5
10
|
def fanout(f,g)
|
6
11
|
Arrows::Proc.new { |*args| [f[*args], g[*args]] }
|
7
12
|
end
|
@@ -23,7 +28,9 @@ module Arrows
|
|
23
28
|
x.respond_to?(:call) && x.respond_to?(:arity)
|
24
29
|
end
|
25
30
|
def wrap_proc(f)
|
26
|
-
Arrows::Proc.new
|
31
|
+
Arrows::Proc.new do |*args|
|
32
|
+
f[*args]
|
33
|
+
end
|
27
34
|
end
|
28
35
|
end
|
29
36
|
class Proc < ::Proc
|
@@ -41,5 +48,10 @@ module Arrows
|
|
41
48
|
def /(f)
|
42
49
|
Arrows.fanout self, Arrows.lift(f)
|
43
50
|
end
|
51
|
+
|
52
|
+
# concurrent composition
|
53
|
+
def %(f)
|
54
|
+
Arrows.concurrent self, Arrows.lift(f)
|
55
|
+
end
|
44
56
|
end
|
45
57
|
end
|
data/lib/arrows/version.rb
CHANGED
data/spec/arrows/proc_spec.rb
CHANGED
@@ -19,7 +19,7 @@ RSpec.describe Arrows::Proc do
|
|
19
19
|
specify { should eq [10, 12, 14] }
|
20
20
|
end
|
21
21
|
|
22
|
-
context '
|
22
|
+
context '/ fanout' do
|
23
23
|
let(:times2) { Arrows.lift -> (x) { x * 2 } }
|
24
24
|
let(:plus3) { Arrows.lift -> (x) { x + 3 } }
|
25
25
|
let(:four) { Arrows.lift 4 }
|
@@ -27,4 +27,52 @@ RSpec.describe Arrows::Proc do
|
|
27
27
|
subject { six_eight.call }
|
28
28
|
specify { should eq [8, 7] }
|
29
29
|
end
|
30
|
+
|
31
|
+
context '% concurrent' do
|
32
|
+
let(:times2) { Arrows.lift -> (x) { x * 2 } }
|
33
|
+
let(:plus3) { Arrows.lift -> (x) { x + 3 } }
|
34
|
+
let(:four) { Arrows.lift [4,6] }
|
35
|
+
context 'validity' do
|
36
|
+
subject { times2.call 2 }
|
37
|
+
specify { should eq 4 }
|
38
|
+
end
|
39
|
+
context 'arity' do
|
40
|
+
let(:par) { times2 % plus3 }
|
41
|
+
subject { par.call 1,2 }
|
42
|
+
specify { should eq [2, 5] }
|
43
|
+
end
|
44
|
+
context 'result' do
|
45
|
+
let(:eight_nine) { four >> times2 % plus3 }
|
46
|
+
subject { eight_nine.call }
|
47
|
+
specify { should eq [8,9] }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context '%/ fanout into concurrent' do
|
52
|
+
let(:add1) { Arrows.lift -> (x) { x + 1 } }
|
53
|
+
let(:add4) { Arrows.lift -> (x) { x + 4 } }
|
54
|
+
let(:two) { Arrows.lift 2 }
|
55
|
+
let(:result) { two >> add1 / add4 >> add1 % add4 }
|
56
|
+
context 'result' do
|
57
|
+
subject { result.call }
|
58
|
+
specify { should eq [4, 10] }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context '>=%/ applicative fanout into concurrent' do
|
63
|
+
let(:add1) { Arrows.lift -> (x) { x + 1 } }
|
64
|
+
let(:add4) { Arrows.lift -> (x) { x + 4 } }
|
65
|
+
let(:twos) { Arrows.lift [2,2,2] }
|
66
|
+
let(:transform) { add1 / add4 >> add1 % add4 }
|
67
|
+
let(:result) { twos >= add1 / add4 >> add1 % add4 }
|
68
|
+
context 'result' do
|
69
|
+
subject { result.call }
|
70
|
+
specify { should eq [[4,10], [4,10], [4,10]] }
|
71
|
+
end
|
72
|
+
context 'similarity' do
|
73
|
+
let(:actual) { twos >= transform }
|
74
|
+
subject { result.call }
|
75
|
+
specify { should eq actual.call }
|
76
|
+
end
|
77
|
+
end
|
30
78
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arrows
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|