ribimaybe 0.0.13 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -8
- data/lib/ribimaybe.rb +5 -19
- data/lib/version.rb +1 -1
- data/spec/monad_spec.rb +4 -4
- 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: d563a588e4c633fb8ac5b30eac7fe2a4396fa2f6
|
4
|
+
data.tar.gz: 2fc3a0eb32ae37c5cc5f7d2d7d60d05bc38b9768
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b30e7b8d646f98db81cf48b7347a9e912628be4c420b53dcaff2b8a547746af077cb21c3bdeb713d7560f9bb9d1d2e2ca3ac07f97deffa04b2efd81f63635578
|
7
|
+
data.tar.gz: 822a93c669fcede5b74d323431571ccc5fcfe24dc51063276aacbcd8e5def8d8bc1b1615ea134234c50710b0351fe8d22602ec8f7265c5b1ee96436120106196
|
data/README.md
CHANGED
@@ -82,22 +82,23 @@ include Ribimaybe::Maybe
|
|
82
82
|
|
83
83
|
# Chain together computations and pretend you're a Haskeller.
|
84
84
|
Just(42).bind do |x|
|
85
|
-
|
86
|
-
|
87
|
-
|
85
|
+
unit(x - 21).bind do |y|
|
86
|
+
if x * x > 100 then unit(x) else unit(y) end
|
87
|
+
end
|
88
88
|
end # => Just(42)
|
89
89
|
|
90
90
|
# You guessed it! If have Nothing, you get Nothing.
|
91
91
|
Nothing.bind do |x|
|
92
|
-
|
92
|
+
unit(x * x)
|
93
93
|
end # => Nothing
|
94
94
|
|
95
|
-
# We even have
|
95
|
+
# We even have >>= but it's called >= and you you need to pass a Proc or a
|
96
|
+
# lambda.
|
96
97
|
Just(42) >= -> (x) do
|
97
|
-
|
98
|
-
if x * x > 100 then
|
98
|
+
unit(x - 21) >= -> (y) do
|
99
|
+
if x * x > 100 then unit(x) else unit(y) end
|
99
100
|
end
|
100
|
-
end
|
101
|
+
end # => Just(42)
|
101
102
|
```
|
102
103
|
|
103
104
|
## Contributing
|
data/lib/ribimaybe.rb
CHANGED
@@ -4,8 +4,7 @@ module Ribimaybe
|
|
4
4
|
module Maybe
|
5
5
|
include Contracts
|
6
6
|
|
7
|
-
#
|
8
|
-
#
|
7
|
+
# Ensure constants are available to Ruby contracts.
|
9
8
|
module Nothing; end
|
10
9
|
class Just; end
|
11
10
|
|
@@ -13,7 +12,6 @@ module Ribimaybe
|
|
13
12
|
include Contracts
|
14
13
|
|
15
14
|
# Nothing string representation.
|
16
|
-
#
|
17
15
|
def self.to_s
|
18
16
|
"Nothing"
|
19
17
|
end
|
@@ -25,28 +23,24 @@ module Ribimaybe
|
|
25
23
|
# ==== Attributes
|
26
24
|
#
|
27
25
|
# * +other+ - The other Maybe value.
|
28
|
-
#
|
29
26
|
Contract Or[Nothing, Just] => Bool
|
30
27
|
def self.===(other)
|
31
28
|
self == other
|
32
29
|
end
|
33
30
|
|
34
31
|
# No operation. Always returns the default value.
|
35
|
-
#
|
36
32
|
Contract Any, Proc => Any
|
37
33
|
def self.maybe(default, &_)
|
38
34
|
default
|
39
35
|
end
|
40
36
|
|
41
37
|
# No operation. Always returns Nothing.
|
42
|
-
#
|
43
38
|
Contract Proc => Nothing
|
44
39
|
def self.map(&_)
|
45
40
|
self
|
46
41
|
end
|
47
42
|
|
48
43
|
# No operation. Always returns Nothing.
|
49
|
-
#
|
50
44
|
Contract Any => Nothing
|
51
45
|
def self.apply(_)
|
52
46
|
self
|
@@ -57,7 +51,6 @@ module Ribimaybe
|
|
57
51
|
end
|
58
52
|
|
59
53
|
# No operation. Always returns Nothing.
|
60
|
-
#
|
61
54
|
Contract Proc => Nothing
|
62
55
|
def self.bind(fn = nil, &_)
|
63
56
|
self
|
@@ -76,7 +69,6 @@ module Ribimaybe
|
|
76
69
|
end
|
77
70
|
|
78
71
|
# Just string representation.
|
79
|
-
#
|
80
72
|
def to_s
|
81
73
|
"Just(#{@value.inspect})"
|
82
74
|
end
|
@@ -94,7 +86,6 @@ module Ribimaybe
|
|
94
86
|
# Just(1) == Just(1) # => true
|
95
87
|
# Just(1) == Just(2) # => false
|
96
88
|
# Just(1) == Nothing # => false
|
97
|
-
#
|
98
89
|
Contract Or[Nothing, Just] => Bool
|
99
90
|
def ==(other)
|
100
91
|
other.maybe(false) do |value|
|
@@ -114,7 +105,6 @@ module Ribimaybe
|
|
114
105
|
#
|
115
106
|
# Just(1).maybe(false) { |x| x == 1 } # => true
|
116
107
|
# Just(1).maybe(42) { |x| x } # => 1
|
117
|
-
#
|
118
108
|
Contract Any, Proc => Any
|
119
109
|
def maybe(_, &fn)
|
120
110
|
fn.curry.(@value)
|
@@ -130,7 +120,6 @@ module Ribimaybe
|
|
130
120
|
#
|
131
121
|
# Just(1).map { |x| x + 1 } # => Just(2)
|
132
122
|
# Just { |x, y| x + y }.map { |f| f.(1) } # => Just(#<Proc:...>)
|
133
|
-
#
|
134
123
|
Contract Proc => Just
|
135
124
|
def map(&fn)
|
136
125
|
Just.new(fn.curry.(@value))
|
@@ -148,7 +137,6 @@ module Ribimaybe
|
|
148
137
|
# Just do |x|
|
149
138
|
# x + x
|
150
139
|
# end.apply(Just(1)) # => Just(2)
|
151
|
-
#
|
152
140
|
Contract Or[Nothing, Just] => Or[Nothing, Just]
|
153
141
|
def apply(value)
|
154
142
|
value.map { |v| @value.curry.(v) }
|
@@ -165,9 +153,8 @@ module Ribimaybe
|
|
165
153
|
# ==== Examples
|
166
154
|
#
|
167
155
|
# Just(1).bind do |x|
|
168
|
-
#
|
156
|
+
# unit(x + x)
|
169
157
|
# end # => Just(2)
|
170
|
-
#
|
171
158
|
Contract Proc => Or[Nothing, Just]
|
172
159
|
def bind(fn = nil, &block)
|
173
160
|
(fn || block).curry.(@value)
|
@@ -189,14 +176,13 @@ module Ribimaybe
|
|
189
176
|
# Maybe(nil) # => Nothing
|
190
177
|
# Maybe(1) # => Just(1)
|
191
178
|
# Maybe { |x| x } # => Just(#<Proc:0x007fdecc03a478@(irb):6>)
|
192
|
-
#
|
193
179
|
Contract Any, Or[nil, Proc] => Or[Nothing, Just]
|
194
180
|
def Maybe(value = nil, &fn)
|
195
181
|
(value || fn) ? Just.new(value || fn.curry) : Nothing
|
196
182
|
end
|
197
183
|
|
198
|
-
alias_method :Just,
|
199
|
-
alias_method :pure,
|
200
|
-
alias_method :
|
184
|
+
alias_method :Just, :Maybe
|
185
|
+
alias_method :pure, :Maybe
|
186
|
+
alias_method :unit, :Maybe
|
201
187
|
end
|
202
188
|
end
|
data/lib/version.rb
CHANGED
data/spec/monad_spec.rb
CHANGED
@@ -6,15 +6,15 @@ describe "Monad Instance" do
|
|
6
6
|
end
|
7
7
|
|
8
8
|
let(:lifted_id) do
|
9
|
-
->(x) { id.(
|
9
|
+
->(x) { id.(unit(x)) }
|
10
10
|
end
|
11
11
|
|
12
12
|
let(:f) do
|
13
|
-
->(x){ ->(y) {
|
13
|
+
->(x){ ->(y) { unit(x) } }.(SecureRandom.base64(1000))
|
14
14
|
end
|
15
15
|
|
16
16
|
let(:g) do
|
17
|
-
->(x){ ->(y) {
|
17
|
+
->(x){ ->(y) { unit(x) } }.(SecureRandom.base64(1000))
|
18
18
|
end
|
19
19
|
|
20
20
|
[:bind, :>=].each do |m|
|
@@ -28,7 +28,7 @@ describe "Monad Instance" do
|
|
28
28
|
|
29
29
|
context "when i have just :x" do
|
30
30
|
it do
|
31
|
-
expect(
|
31
|
+
expect(unit(:x).public_send(m, &lifted_id)).to eq(lifted_id.(:x))
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ribimaybe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- unsymbol
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|