arrows 0.0.2 → 0.0.3
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 +4 -4
- data/lib/arrows.rb +28 -0
- data/lib/arrows/version.rb +1 -1
- data/spec/arrows/proc_spec.rb +17 -0
- 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: 3e42f3d0e4c8cecd3652dad40c68dceb12ae87b3
|
4
|
+
data.tar.gz: 60bfe5bc1948c543a13e421fce7fadae1fb96fe6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 747c5dec094e34b35286359c41477f8cca1bc655c4378798f49f66e41610d5339e642cc7ebef41e510e8349d7dd42c39d9d7e009ff12affffd622d13645df83a
|
7
|
+
data.tar.gz: cc30a24f86c1831b0f48febe9bfed33cce5a16d7fd1a95e74a88dfab9d9b6e9e31535578ca333ed17831a1f347ec9c97fdbe7f75b19839b9a9405d80f1e3d8f4
|
data/lib/arrows.rb
CHANGED
@@ -1,7 +1,22 @@
|
|
1
1
|
require "arrows/version"
|
2
2
|
|
3
3
|
module Arrows
|
4
|
+
class Either
|
5
|
+
attr_accessor :payload
|
6
|
+
def initialize(good_or_evil, payload)
|
7
|
+
@good = !!good_or_evil
|
8
|
+
@payload = payload
|
9
|
+
end
|
10
|
+
def good?
|
11
|
+
@good
|
12
|
+
end
|
13
|
+
end
|
4
14
|
class << self
|
15
|
+
def fork(f,g)
|
16
|
+
Arrows::Proc.new do |either|
|
17
|
+
either.good? ? f[*either.payload] : g[*either.payload]
|
18
|
+
end
|
19
|
+
end
|
5
20
|
def concurrent(f,g)
|
6
21
|
Arrows::Proc.new do |*args|
|
7
22
|
[f[*args.first], g[*args.last]]
|
@@ -16,6 +31,14 @@ module Arrows
|
|
16
31
|
def fmap(xs, f)
|
17
32
|
Arrows::Proc.new { |*args| xs[*args].map { |*x| f[*x] } }
|
18
33
|
end
|
34
|
+
def good(x)
|
35
|
+
return x if x.respond_to?(:good?) && x.respond_to?(:payload)
|
36
|
+
Arrows::Either.new true, x
|
37
|
+
end
|
38
|
+
def evil(x)
|
39
|
+
return x if x.respond_to?(:good?) && x.respond_to?(:payload)
|
40
|
+
Arrows::Either.new false, x
|
41
|
+
end
|
19
42
|
def lift(x)
|
20
43
|
return x if arrow_like? x
|
21
44
|
return wrap_proc x if proc_like? x
|
@@ -53,5 +76,10 @@ module Arrows
|
|
53
76
|
def %(f)
|
54
77
|
Arrows.concurrent self, Arrows.lift(f)
|
55
78
|
end
|
79
|
+
|
80
|
+
# fork composition
|
81
|
+
def ^(f)
|
82
|
+
Arrows.fork self, f
|
83
|
+
end
|
56
84
|
end
|
57
85
|
end
|
data/lib/arrows/version.rb
CHANGED
data/spec/arrows/proc_spec.rb
CHANGED
@@ -48,6 +48,23 @@ RSpec.describe Arrows::Proc do
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
+
context '^ fork' do
|
52
|
+
let(:times2) { Arrows.lift -> (x) { x * 2 } }
|
53
|
+
let(:plus3) { Arrows.lift -> (x) { x + 3 } }
|
54
|
+
let(:four) { Arrows.lift Arrows.good 4 }
|
55
|
+
let(:eight) { Arrows.lift Arrows.evil 8 }
|
56
|
+
let(:fork_four) { four >> (times2 ^ plus3) }
|
57
|
+
let(:fork_eight) { eight >> (plus3 ^ times2) }
|
58
|
+
context 'good' do
|
59
|
+
subject { fork_four.call }
|
60
|
+
specify { should eq 8 }
|
61
|
+
end
|
62
|
+
context 'evil' do
|
63
|
+
subject { fork_eight.call }
|
64
|
+
specify { should eq 16 }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
51
68
|
context '%/ fanout into concurrent' do
|
52
69
|
let(:add1) { Arrows.lift -> (x) { x + 1 } }
|
53
70
|
let(:add4) { Arrows.lift -> (x) { x + 4 } }
|
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.3
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|