ribimaybe 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/ribimaybe.rb +9 -3
- data/lib/version.rb +1 -1
- data/spec/applicative_functor_spec.rb +55 -53
- data/spec/monad_spec.rb +35 -33
- 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: c4f36f13ff02dcf449662d3d89acdce59f781329
|
4
|
+
data.tar.gz: 09a060eebd6164be01a68f33d6820660a2afb7eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e99de225be69caf575dabfb961167d1b642dd2e497371ef2a301276d72529f70e7e63abe21f55c3f77ba14d523c2b2b15f5bb4a8e34107d9fdc1510bb6c0e479
|
7
|
+
data.tar.gz: a57acc14c7af6109c870efd22a31a9e2eec688aa2b9e038ef11f8466d2cfa8ee9a7028ae3b9ac520d0f785e2dba5812528f0ccbf2479f7552c13b0046a705a9d
|
data/README.md
CHANGED
@@ -91,7 +91,15 @@ end # => Just(42)
|
|
91
91
|
Nothing.bind do |x|
|
92
92
|
rturn(x * x)
|
93
93
|
end # => Nothing
|
94
|
+
|
95
|
+
# We even have >= but you need to pass a Proc or a lambda.
|
96
|
+
Just(42) >= -> (x) do
|
97
|
+
rturn(x - 21) >= -> (y) do
|
98
|
+
if x * x > 100 then rturn(y) else rturn(x) end
|
99
|
+
end
|
100
|
+
end
|
94
101
|
```
|
102
|
+
|
95
103
|
## Contributing
|
96
104
|
|
97
105
|
1. Fork it
|
data/lib/ribimaybe.rb
CHANGED
@@ -59,9 +59,13 @@ module Ribimaybe
|
|
59
59
|
# No operation. Always returns Nothing.
|
60
60
|
#
|
61
61
|
Contract Proc => Nothing
|
62
|
-
def self.bind(&_)
|
62
|
+
def self.bind(fn = nil, &_)
|
63
63
|
self
|
64
64
|
end
|
65
|
+
|
66
|
+
class << self
|
67
|
+
alias_method :>=, :bind
|
68
|
+
end
|
65
69
|
end
|
66
70
|
|
67
71
|
class Just
|
@@ -165,9 +169,11 @@ module Ribimaybe
|
|
165
169
|
# end # => Just(2)
|
166
170
|
#
|
167
171
|
Contract Proc => Or[Nothing, Just]
|
168
|
-
def bind(&
|
169
|
-
fn.curry.(@value)
|
172
|
+
def bind(fn = nil, &block)
|
173
|
+
(fn || block).curry.(@value)
|
170
174
|
end
|
175
|
+
|
176
|
+
alias_method :>=, :bind
|
171
177
|
end
|
172
178
|
|
173
179
|
# Converts nil to Nothing or lifts value into a Just. Accepts a optional
|
data/lib/version.rb
CHANGED
@@ -21,81 +21,83 @@ describe "Applicative Instance" do
|
|
21
21
|
->(x){ ->(y) { x } }.(SecureRandom.base64(1000))
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
[:apply, :>>].each do |m|
|
25
|
+
# pure id <*> v = v
|
26
|
+
describe "identity" do
|
27
|
+
context "when i have nothing" do
|
28
|
+
it do
|
29
|
+
expect(pure(&id).public_send(m, Nothing)).to eq(Nothing)
|
30
|
+
end
|
29
31
|
end
|
30
|
-
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
context "when i have just :x" do
|
34
|
+
it do
|
35
|
+
expect(pure(&id).public_send(m, pure(:x))).to eq(pure(:x))
|
36
|
+
end
|
35
37
|
end
|
36
38
|
end
|
37
|
-
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
40
|
+
# pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
|
41
|
+
describe "composition" do
|
42
|
+
context "when i have nothing" do
|
43
|
+
it do
|
44
|
+
lhs = pure(&dot).public_send(m, pure(&f)).public_send(m, pure(&g)).public_send(m, Nothing)
|
45
|
+
rhs = pure(&f).public_send(m, pure(&g).public_send(m, Nothing))
|
46
|
+
expect(lhs).to eq(rhs)
|
47
|
+
end
|
46
48
|
end
|
47
|
-
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
context "when i have just :x" do
|
51
|
+
it do
|
52
|
+
lhs = pure(&dot).public_send(m, pure(&f)).public_send(m, pure(&g)).public_send(m, pure(:x))
|
53
|
+
rhs = pure(&f).public_send(m, pure(&g).public_send(m, pure(:x)))
|
54
|
+
expect(lhs).to eq(rhs)
|
55
|
+
end
|
54
56
|
end
|
55
57
|
end
|
56
|
-
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
# pure f <*> pure x = pure (f x)
|
60
|
+
describe "homomorphism" do
|
61
|
+
context "when i have nothing" do
|
62
|
+
it do
|
63
|
+
expect(pure(&f).public_send(m, Nothing)).to eq(Nothing)
|
64
|
+
end
|
63
65
|
end
|
64
|
-
end
|
65
66
|
|
66
|
-
|
67
|
-
|
68
|
-
|
67
|
+
context "when i have just :x" do
|
68
|
+
it do
|
69
|
+
expect(pure(&f).public_send(m, pure(:x))).to eq(pure(f.(:x)))
|
70
|
+
end
|
69
71
|
end
|
70
72
|
end
|
71
|
-
end
|
72
73
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
74
|
+
# u <*> pure y = pure ($ y) <*> u
|
75
|
+
describe "interchange" do
|
76
|
+
context "when i have nothing" do
|
77
|
+
it do
|
78
|
+
expect(pure(&f).public_send(m, Nothing)).to eq(pure(&ap.(f)).public_send(m, Nothing))
|
79
|
+
end
|
78
80
|
end
|
79
|
-
end
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
|
82
|
+
context "when i have just :x" do
|
83
|
+
it do
|
84
|
+
expect(pure(&f).public_send(m, pure(:x))).to eq(pure(&ap.(f)).public_send(m, pure(:x)))
|
85
|
+
end
|
84
86
|
end
|
85
87
|
end
|
86
|
-
end
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
# fmap f x = pure f <*> x
|
90
|
+
describe "map" do
|
91
|
+
context "when i have nothing" do
|
92
|
+
it do
|
93
|
+
expect(Nothing.map(&f)).to eq(pure(&f).public_send(m, Nothing))
|
94
|
+
end
|
93
95
|
end
|
94
|
-
end
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
context "when i have just :x" do
|
98
|
+
it do
|
99
|
+
expect(Just(:x).map(&f)).to eq(pure(&f).public_send(m, Just(:x)))
|
100
|
+
end
|
99
101
|
end
|
100
102
|
end
|
101
103
|
end
|
data/spec/monad_spec.rb
CHANGED
@@ -17,51 +17,53 @@ describe "Monad Instance" do
|
|
17
17
|
->(x){ ->(y) { rturn(x) } }.(SecureRandom.base64(1000))
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
[:bind, :>=].each do |m|
|
21
|
+
# return a >>= f = f a
|
22
|
+
describe "left identity" do
|
23
|
+
context "when i have nothing" do
|
24
|
+
it do
|
25
|
+
expect(Nothing.public_send(m, &lifted_id)).to eq(Nothing)
|
26
|
+
end
|
25
27
|
end
|
26
|
-
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
context "when i have just :x" do
|
30
|
+
it do
|
31
|
+
expect(rturn(:x).public_send(m, &lifted_id)).to eq(lifted_id.(:x))
|
32
|
+
end
|
31
33
|
end
|
32
34
|
end
|
33
|
-
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
# m >>= return = m
|
37
|
+
describe "right identity" do
|
38
|
+
context "when i have nothing" do
|
39
|
+
it do
|
40
|
+
expect(Nothing.public_send(m, &lifted_id)).to eq(Nothing)
|
41
|
+
end
|
40
42
|
end
|
41
|
-
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
context "when i have just :x" do
|
45
|
+
it do
|
46
|
+
expect(Just(:x).public_send(m, &lifted_id)).to eq(Just(:x))
|
47
|
+
end
|
46
48
|
end
|
47
49
|
end
|
48
|
-
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
51
|
+
# (m >>= f) >>= g = m >>= (\x -> f x >>= g)
|
52
|
+
describe "associativity" do
|
53
|
+
context "when i have nothing" do
|
54
|
+
it do
|
55
|
+
lhs = Nothing.public_send(m, &f).public_send(m, &g)
|
56
|
+
rhs = Nothing.bind { |x| f.(x).public_send(m, &g) }
|
57
|
+
expect(lhs).to eq(rhs)
|
58
|
+
end
|
57
59
|
end
|
58
|
-
end
|
59
60
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
context "when i have just :x" do
|
62
|
+
it do
|
63
|
+
lhs = Just(:x).public_send(m, &f).public_send(m, &g)
|
64
|
+
rhs = Just(:x).bind { |x| f.(x).public_send(m, &g) }
|
65
|
+
expect(lhs).to eq(rhs)
|
66
|
+
end
|
65
67
|
end
|
66
68
|
end
|
67
69
|
end
|