monad-maybe 0.9.3 → 0.9.4
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/Rakefile +5 -0
- data/VERSION +1 -1
- data/lib/monad/maybe.rb +11 -22
- data/lib/monad/maybe/base.rb +13 -0
- data/lib/monad/maybe/just.rb +5 -5
- data/lib/monad/maybe/list.rb +1 -1
- data/lib/monad/maybe/nothing.rb +1 -1
- data/monad-maybe.gemspec +1 -1
- data/test/maybe.rb +62 -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: e61e65884a865a598567f36add94bbba7907ab3f
|
4
|
+
data.tar.gz: b2d2f6ec6625ccf1c61fbf8a888bd6cbd76342be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc6b56d0da004ab51a7d042cf7e8a8797de1716bd993a3eab98e42c1bb7d1dc8360119155802772f5f51ce53c87353cd060c19a4b1b3a79ade4df5d24fab2595
|
7
|
+
data.tar.gz: ad3ab76350f0d63b6a879d45e80b2e063aac9ad802092ae3530e1e4c3dbdde666949733de7c2a8f3fc07f99586aa4c6ae834f055de1a5716c1a3b549fe6ee548
|
data/Rakefile
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'rake/testtask'
|
1
2
|
require 'jeweler'
|
2
3
|
Jeweler::Tasks.new do |gem|
|
3
4
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
@@ -21,3 +22,7 @@ desc "Setup for development"
|
|
21
22
|
task :setup do
|
22
23
|
sh "bundle"
|
23
24
|
end
|
25
|
+
|
26
|
+
Rake::TestTask.new do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.4
|
data/lib/monad/maybe.rb
CHANGED
@@ -8,7 +8,7 @@ require_relative 'maybe/list'
|
|
8
8
|
#
|
9
9
|
module Enumerable
|
10
10
|
def to_maybe
|
11
|
-
first.
|
11
|
+
first.to_maybe
|
12
12
|
end
|
13
13
|
|
14
14
|
def maybe_map
|
@@ -16,18 +16,7 @@ module Enumerable
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
class Array; include Enumerable end
|
20
|
-
class Range; include Enumerable end
|
21
|
-
|
22
19
|
class Object
|
23
|
-
def maybe(obj=self, &blk)
|
24
|
-
if obj && blk
|
25
|
-
blk.call(obj).to_maybe
|
26
|
-
else
|
27
|
-
obj.to_maybe
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
20
|
def to_maybe
|
32
21
|
Monad::Maybe::Just.new(self)
|
33
22
|
end
|
@@ -43,24 +32,24 @@ class Object
|
|
43
32
|
def nothing?
|
44
33
|
false
|
45
34
|
end
|
46
|
-
|
47
|
-
def something?(&blk)
|
48
|
-
true
|
49
|
-
end
|
50
35
|
end
|
51
36
|
|
52
37
|
class NilClass
|
53
38
|
def to_maybe
|
54
|
-
Monad::Maybe::Nothing.instance
|
39
|
+
Monad::Maybe::Nothing.instance
|
55
40
|
end
|
41
|
+
end
|
56
42
|
|
57
|
-
|
58
|
-
|
43
|
+
module Monad
|
44
|
+
module Maybe
|
45
|
+
def self.return(obj)
|
46
|
+
obj.to_maybe
|
47
|
+
end
|
59
48
|
end
|
49
|
+
end
|
60
50
|
|
61
|
-
|
62
|
-
|
63
|
-
end
|
51
|
+
def maybe(obj)
|
52
|
+
Monad::Maybe.return(obj)
|
64
53
|
end
|
65
54
|
|
66
55
|
def just(o)
|
data/lib/monad/maybe/base.rb
CHANGED
data/lib/monad/maybe/just.rb
CHANGED
data/lib/monad/maybe/list.rb
CHANGED
data/lib/monad/maybe/nothing.rb
CHANGED
data/monad-maybe.gemspec
CHANGED
data/test/maybe.rb
CHANGED
@@ -4,16 +4,16 @@ require_relative '../lib/monad/maybe/json'
|
|
4
4
|
|
5
5
|
class MaybeTest < Test::Unit::TestCase
|
6
6
|
def test_nothing
|
7
|
-
assert nil.
|
8
|
-
assert nil.
|
9
|
-
assert_equal 'test', nil.
|
7
|
+
assert nil.to_maybe.nothing?
|
8
|
+
assert nil.to_maybe.value.nil?
|
9
|
+
assert_equal 'test', nil.to_maybe.unwrap('test')
|
10
10
|
assert_equal '', nothing.to_s
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_just
|
14
|
-
assert 1.
|
15
|
-
assert_equal 1, 1.
|
16
|
-
assert_equal 1, 1.
|
14
|
+
assert maybe(1).just?
|
15
|
+
assert_equal 1, 1.to_maybe.value
|
16
|
+
assert_equal 1, 1.to_maybe.unwrap('test')
|
17
17
|
assert_equal '1', just(1).to_s
|
18
18
|
end
|
19
19
|
|
@@ -26,25 +26,25 @@ class MaybeTest < Test::Unit::TestCase
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_to_a
|
29
|
-
assert_equal [1.
|
30
|
-
assert_equal [], nil.
|
29
|
+
assert_equal [1.to_maybe], 1.to_maybe.to_a
|
30
|
+
assert_equal [], nil.to_maybe.to_a
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_false
|
34
|
-
assert_equal false, false.
|
34
|
+
assert_equal false, false.to_maybe.value
|
35
35
|
end
|
36
36
|
|
37
37
|
def test_nil_conversions
|
38
|
-
assert_equal nil.nil?, nil.
|
38
|
+
assert_equal nil.nil?, nil.to_maybe.nil?
|
39
39
|
end
|
40
40
|
|
41
41
|
def test_class
|
42
|
-
assert_equal Monad::Maybe::Nothing, nil.
|
43
|
-
assert_equal Monad::Maybe::Just, 1.
|
42
|
+
assert_equal Monad::Maybe::Nothing, maybe(nil).class
|
43
|
+
assert_equal Monad::Maybe::Just, maybe(1).class
|
44
44
|
end
|
45
45
|
|
46
46
|
def test_list
|
47
|
-
xs = true.
|
47
|
+
xs = true.to_maybe << nil.to_maybe << 1.to_maybe << 3.to_maybe
|
48
48
|
assert_equal 3, xs.count
|
49
49
|
xs.each { |x| assert x.just? }
|
50
50
|
end
|
@@ -58,12 +58,6 @@ class MaybeTest < Test::Unit::TestCase
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_maybe_block
|
61
|
-
assert_nothing_raised do
|
62
|
-
nil.maybe do
|
63
|
-
raise Exception, "This should not be called"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
61
|
assert_nothing_raised do
|
68
62
|
nothing.maybe do
|
69
63
|
raise Exception, "This should not be called"
|
@@ -76,10 +70,6 @@ class MaybeTest < Test::Unit::TestCase
|
|
76
70
|
end
|
77
71
|
end
|
78
72
|
|
79
|
-
1.maybe do |n|
|
80
|
-
assert_equal 1, n
|
81
|
-
end
|
82
|
-
|
83
73
|
maybe(1) do |n|
|
84
74
|
assert_equal 1, n
|
85
75
|
end
|
@@ -90,25 +80,17 @@ class MaybeTest < Test::Unit::TestCase
|
|
90
80
|
end
|
91
81
|
|
92
82
|
def test_maybe_block_return_value_and_type
|
93
|
-
m = 1.maybe { |n| n + 1 }
|
83
|
+
m = maybe(1).maybe { |n| n + 1 }
|
94
84
|
assert_equal 2, m.value
|
95
85
|
assert m.maybe?
|
96
86
|
assert m.just?
|
97
87
|
|
98
|
-
o = 2.maybe { nil }
|
88
|
+
o = maybe(2).maybe { nil }
|
99
89
|
assert_equal nil, o.value
|
100
90
|
assert o.maybe?
|
101
91
|
assert o.nothing?
|
102
92
|
end
|
103
93
|
|
104
|
-
def test_something?
|
105
|
-
assert_equal false, nil.something?
|
106
|
-
assert_equal false, nothing.something?
|
107
|
-
assert 1.something?
|
108
|
-
assert 1.maybe.something?
|
109
|
-
assert (0..10).maybe_map { |n| n }.something?
|
110
|
-
end
|
111
|
-
|
112
94
|
def test_to_maybe
|
113
95
|
assert_equal just(1), 1.to_maybe
|
114
96
|
assert_equal just(1), just(1).to_maybe
|
@@ -117,4 +99,51 @@ class MaybeTest < Test::Unit::TestCase
|
|
117
99
|
assert_equal 1.to_maybe, [1].to_maybe
|
118
100
|
assert_equal 0.to_maybe, (0..10).maybe_map { |n| n }.to_maybe
|
119
101
|
end
|
102
|
+
|
103
|
+
def test_then_and_and
|
104
|
+
just(1).and do
|
105
|
+
assert true
|
106
|
+
end
|
107
|
+
|
108
|
+
nothing.and do
|
109
|
+
assert true
|
110
|
+
end
|
111
|
+
|
112
|
+
just(1).and{ assert true }.and{ assert true }
|
113
|
+
maybe(1).then(->(){ assert true }).then(->(){ assert true })
|
114
|
+
end
|
115
|
+
|
116
|
+
#
|
117
|
+
# Monad laws
|
118
|
+
#
|
119
|
+
|
120
|
+
#
|
121
|
+
# Right Unit:
|
122
|
+
# m >>= return = m
|
123
|
+
#
|
124
|
+
def test_right_unit
|
125
|
+
m = just(1)
|
126
|
+
assert_equal m, m.bind(->(x){ Monad::Maybe.return(x) })
|
127
|
+
end
|
128
|
+
|
129
|
+
#
|
130
|
+
# Left Unit:
|
131
|
+
# return x >>= f = f x
|
132
|
+
#
|
133
|
+
def test_left_unit
|
134
|
+
x = 2
|
135
|
+
f = ->(x) { x + 1 }
|
136
|
+
assert_equal Monad::Maybe.return(x).bind(f), f[x]
|
137
|
+
end
|
138
|
+
|
139
|
+
#
|
140
|
+
# Associativity:
|
141
|
+
# (m >>= f) >>= g = m >>= (\x -> f x >>= g)
|
142
|
+
#
|
143
|
+
def test_associativity
|
144
|
+
m = just(3.1459)
|
145
|
+
f = ->(x) { x ** 2 }
|
146
|
+
g = ->(x) { 2 * x }
|
147
|
+
assert_equal m.bind(f).bind(g), m.bind(->(x) { f[x] }).bind(g)
|
148
|
+
end
|
120
149
|
end
|