ribimaybe 0.0.8 → 0.0.9
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 +5 -13
- data/lib/ribimaybe.rb +6 -0
- data/lib/version.rb +1 -1
- data/spec/applicative_functor_spec.rb +82 -25
- data/spec/functor_spec.rb +34 -9
- data/spec/maybe_spec.rb +18 -5
- data/spec/monad_spec.rb +60 -0
- data/spec/spec_helper.rb +10 -0
- metadata +13 -13
- data/spec/monad_instance_spec.rb +0 -40
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
YTBjMmYwM2FkNjUwNDc0MjhiN2Y4MjM4YjMzNGQ0YjQ3MmQ5OTc3Zg==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6db212d9c3464928b07c3bb761205ed07313e566
|
4
|
+
data.tar.gz: ca60246d186c728d0f4da756826523f26b3ba814
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MzdmYjYzOThhZTZlOTY3ZDc1NDFhMzE3MDkyODJjMTc1Njc0NjhkMmJiNDM2
|
11
|
-
MTgzMWY3ZTVmOTVjMTE5NjM1Zjc3YzQ0ZDg3MTk2YjEwYmJkYWE=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
ZWQzMWIzMTk1ZGVkMDQ0ZTE0OWU1ZjExNGQyZDk2OTBkZmM1YzJhNTYwZGVl
|
14
|
-
NjUwMDJiNTUxYzExYzhiOWY0ZWRmOTBkOGQ2ZjE0Nzg2ZDU2ZjBhNzM4ZTM0
|
15
|
-
ODhhYzhiMGZmNTMzZGFhNzAxMTJjNTIyZDEwNjZlMThmM2ZmNDk=
|
6
|
+
metadata.gz: 84aa2de634c9d305c7d27fed5a267ce2a275bd87bed2f73aed3b47e2170e609da37742bacda67af1324d0d5337311a3ae9ea40abb06bfc58a15dd5d8986dc7df
|
7
|
+
data.tar.gz: 6bd80d6844bd70110234b5e75686d26a7ab7a4d05f022e6181064c7f5b5f1fe2110119b4365ee8a63897710f0ee272b7047ee1bed261d71898b08000224036d8
|
data/lib/ribimaybe.rb
CHANGED
@@ -13,6 +13,8 @@ module Ribimaybe
|
|
13
13
|
"Nothing"
|
14
14
|
end
|
15
15
|
|
16
|
+
alias_method :inspect, :to_s
|
17
|
+
|
16
18
|
# No operation. Always returns the default value.
|
17
19
|
#
|
18
20
|
Contract Any, Proc => Any
|
@@ -49,10 +51,14 @@ module Ribimaybe
|
|
49
51
|
@value = value
|
50
52
|
end
|
51
53
|
|
54
|
+
# Just string representation.
|
55
|
+
#
|
52
56
|
def to_s
|
53
57
|
"Just(#{@value.inspect})"
|
54
58
|
end
|
55
59
|
|
60
|
+
alias_method :inspect, :to_s
|
61
|
+
|
56
62
|
# Compares a Just to another Maybe.
|
57
63
|
#
|
58
64
|
# ==== Attributes
|
data/lib/version.rb
CHANGED
@@ -1,44 +1,101 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
include Ribimaybe::Maybe
|
3
|
-
|
4
3
|
describe "Applicative Instance" do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
let(:ap) do
|
5
|
+
->(f, a){ f.(a) }.curry
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:id) do
|
9
|
+
->(x){ x }
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:dot) do
|
13
|
+
->(f, g){ ->(x){ f.(g.(x)) } }
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:f) do
|
17
|
+
->(x){ ->(y) { x } }.(SecureRandom.base64(1000))
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:g) do
|
21
|
+
->(x){ ->(y) { x } }.(SecureRandom.base64(1000))
|
22
|
+
end
|
23
|
+
|
24
|
+
# pure id <*> v = v
|
25
|
+
describe "identity" do
|
26
|
+
context "when i have nothing" do
|
27
|
+
it do
|
28
|
+
expect(pure(&id).apply(Nothing)).to eq(Nothing)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when i have just :x" do
|
33
|
+
it do
|
34
|
+
expect(pure(&id).apply(pure(:x))).to eq(pure(:x))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
|
40
|
+
describe "composition" do
|
41
|
+
context "when i have nothing" do
|
42
|
+
it do
|
43
|
+
lhs = pure(&dot).apply(pure(&f)).apply(pure(&g)).apply(Nothing)
|
44
|
+
rhs = pure(&f).apply(pure(&g).apply(Nothing))
|
45
|
+
expect(lhs).to eq(rhs)
|
10
46
|
end
|
11
47
|
end
|
12
48
|
|
13
|
-
context "when i
|
14
|
-
it
|
15
|
-
|
16
|
-
|
49
|
+
context "when i have just :x" do
|
50
|
+
it do
|
51
|
+
lhs = pure(&dot).apply(pure(&f)).apply(pure(&g)).apply(pure(:x))
|
52
|
+
rhs = pure(&f).apply(pure(&g).apply(pure(:x)))
|
53
|
+
expect(lhs).to eq(rhs)
|
17
54
|
end
|
18
55
|
end
|
19
56
|
end
|
20
57
|
|
21
|
-
|
58
|
+
# pure f <*> pure x = pure (f x)
|
59
|
+
describe "homomorphism" do
|
22
60
|
context "when i have nothing" do
|
23
|
-
it
|
24
|
-
|
25
|
-
expect(result).to eq Nothing
|
61
|
+
it do
|
62
|
+
expect(pure(&f).apply(Nothing)).to eq(Nothing)
|
26
63
|
end
|
27
64
|
end
|
28
65
|
|
29
|
-
context "when i
|
30
|
-
it
|
31
|
-
|
32
|
-
x + x
|
33
|
-
end.apply Just(21)
|
34
|
-
expect(result).to eq Just(42)
|
66
|
+
context "when i have just :x" do
|
67
|
+
it do
|
68
|
+
expect(pure(&f).apply(pure(:x))).to eq(pure(f.(:x)))
|
35
69
|
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# u <*> pure y = pure ($ y) <*> u
|
74
|
+
describe "interchange" do
|
75
|
+
context "when i have nothing" do
|
76
|
+
it do
|
77
|
+
expect(pure(&f).apply(Nothing)).to eq(pure(&ap.(f)).apply(Nothing))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when i have just :x" do
|
82
|
+
it do
|
83
|
+
expect(pure(&f).apply(pure(:x))).to eq(pure(&ap.(f)).apply(pure(:x)))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# fmap f x = pure f <*> x
|
89
|
+
describe "map" do
|
90
|
+
context "when i have nothing" do
|
91
|
+
it do
|
92
|
+
expect(Nothing.map(&f)).to eq(pure(&f).apply(Nothing))
|
93
|
+
end
|
94
|
+
end
|
36
95
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end.apply(Just(21)).apply(Just(21))
|
41
|
-
expect(result).to eq Just(42)
|
96
|
+
context "when i have just :x" do
|
97
|
+
it do
|
98
|
+
expect(Just(:x).map(&f)).to eq(pure(&f).apply(Just(:x)))
|
42
99
|
end
|
43
100
|
end
|
44
101
|
end
|
data/spec/functor_spec.rb
CHANGED
@@ -1,19 +1,44 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
include Ribimaybe::Maybe
|
3
|
-
|
4
3
|
describe "Functor Instance" do
|
5
|
-
|
4
|
+
let(:id) do
|
5
|
+
->(x){ x }
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:f) do
|
9
|
+
->(x){ ->(y) { x } }.(SecureRandom.base64(1000)).extend(Composable)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:g) do
|
13
|
+
->(x){ ->(y) { x } }.(SecureRandom.base64(1000)).extend(Composable)
|
14
|
+
end
|
15
|
+
|
16
|
+
# fmap id = id
|
17
|
+
describe "identity" do
|
18
|
+
context "when i have nothing" do
|
19
|
+
it do
|
20
|
+
expect(Nothing.map(&id)).to eq(Nothing)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when i have just :x" do
|
25
|
+
it do
|
26
|
+
expect(Just(:x).map(&id)).to eq(Just(:x))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# fmap (f . g) = fmap f . fmap g
|
32
|
+
describe "composition" do
|
6
33
|
context "when i have nothing" do
|
7
|
-
it
|
8
|
-
|
9
|
-
expect(result).to eq Nothing
|
34
|
+
it do
|
35
|
+
expect(Nothing.map(&(f * g))).to eq(Nothing.map(&g).map(&f))
|
10
36
|
end
|
11
37
|
end
|
12
38
|
|
13
|
-
context "when i have
|
14
|
-
it
|
15
|
-
|
16
|
-
expect(result).to eq Just(42)
|
39
|
+
context "when i have just :x" do
|
40
|
+
it do
|
41
|
+
expect(Just(:x).map(&(f * g))).to eq(Just(:x).map(&g).map(&f))
|
17
42
|
end
|
18
43
|
end
|
19
44
|
end
|
data/spec/maybe_spec.rb
CHANGED
@@ -1,19 +1,32 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
include Ribimaybe::Maybe
|
3
|
-
|
4
3
|
describe Ribimaybe::Maybe do
|
5
4
|
describe ".maybe" do
|
6
5
|
context "when i have nothing" do
|
7
6
|
it "should give me back a default" do
|
8
|
-
|
9
|
-
expect(result).to eq 42
|
7
|
+
expect(Nothing.maybe(false) { |_| true }).to eq(false)
|
10
8
|
end
|
11
9
|
end
|
10
|
+
end
|
12
11
|
|
12
|
+
describe "#maybe" do
|
13
13
|
context "when i have something" do
|
14
14
|
it "should give me back something" do
|
15
|
-
|
16
|
-
|
15
|
+
expect(Just(:x).maybe(:y) { |x| x }).to eq(:x)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#Maybe()" do
|
21
|
+
context "when i have nil" do
|
22
|
+
it "should give me back nothing" do
|
23
|
+
expect(Maybe(nil)).to eq(Nothing)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when i have :x" do
|
28
|
+
it "should give me back just :x" do
|
29
|
+
expect(Maybe(:x)).to eq(Just(:x))
|
17
30
|
end
|
18
31
|
end
|
19
32
|
end
|
data/spec/monad_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
include Ribimaybe::Maybe
|
3
|
+
describe "Monad Instance" do
|
4
|
+
let(:f) do
|
5
|
+
->(x){ ->(y) { rturn(x) } }.(SecureRandom.base64(1000))
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:g) do
|
9
|
+
->(x){ ->(y) { rturn(x) } }.(SecureRandom.base64(1000))
|
10
|
+
end
|
11
|
+
|
12
|
+
# return a >>= f = f a
|
13
|
+
describe "left identity" do
|
14
|
+
context "when i have nothing" do
|
15
|
+
it do
|
16
|
+
expect(Nothing.bind(&f)).to eq(Nothing)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when i have just :x" do
|
21
|
+
it do
|
22
|
+
expect(rturn(:x).bind(&f)).to eq(f.(:a))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# m >>= return = m
|
28
|
+
describe "right identity" do
|
29
|
+
context "when i have nothing" do
|
30
|
+
it do
|
31
|
+
expect(Nothing.bind { |x| rturn(x) }).to eq(Nothing)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when i have just :x" do
|
36
|
+
it do
|
37
|
+
expect(Just(:x).bind { |x| rturn(x) }).to eq(Just(:x))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# (m >>= f) >>= g = m >>= (\x -> f x >>= g)
|
43
|
+
describe "associativity" do
|
44
|
+
context "when i have nothing" do
|
45
|
+
it do
|
46
|
+
lhs = Nothing.bind(&f).bind(&g)
|
47
|
+
rhs = Nothing.bind { |x| f.(x).bind(&g) }
|
48
|
+
expect(lhs).to eq(rhs)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when i have just :x" do
|
53
|
+
it do
|
54
|
+
lhs = Just(:x).bind(&f).bind(&g)
|
55
|
+
rhs = Just(:x).bind { |x| f.(x).bind(&g) }
|
56
|
+
expect(lhs).to eq(rhs)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- unsymbol
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,42 +28,42 @@ dependencies:
|
|
28
28
|
name: pry
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
@@ -100,7 +100,7 @@ files:
|
|
100
100
|
- spec/applicative_functor_spec.rb
|
101
101
|
- spec/functor_spec.rb
|
102
102
|
- spec/maybe_spec.rb
|
103
|
-
- spec/
|
103
|
+
- spec/monad_spec.rb
|
104
104
|
- spec/spec_helper.rb
|
105
105
|
homepage: https://github.com/unsymbol/ribimaybe
|
106
106
|
licenses:
|
@@ -112,17 +112,17 @@ require_paths:
|
|
112
112
|
- lib
|
113
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - '>='
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: 1.9.3
|
118
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
119
|
requirements:
|
120
|
-
- -
|
120
|
+
- - '>='
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.0.2
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: A tiny Ruby library that provides a Maybe datatype which is a Functor, Applicative
|
@@ -131,5 +131,5 @@ test_files:
|
|
131
131
|
- spec/applicative_functor_spec.rb
|
132
132
|
- spec/functor_spec.rb
|
133
133
|
- spec/maybe_spec.rb
|
134
|
-
- spec/
|
134
|
+
- spec/monad_spec.rb
|
135
135
|
- spec/spec_helper.rb
|
data/spec/monad_instance_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
include Ribimaybe::Maybe
|
3
|
-
|
4
|
-
describe "Monad Instance" do
|
5
|
-
describe "#rturn" do
|
6
|
-
context "when i provide a nil" do
|
7
|
-
it "should give me back nothing" do
|
8
|
-
result = rturn(nil)
|
9
|
-
expect(result).to eq Nothing
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
context "when i provide a value" do
|
14
|
-
it "should wrap the value" do
|
15
|
-
result = rturn(42)
|
16
|
-
expect(result).to eq Just(42)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
describe "#bind" do
|
22
|
-
context "when i have nothing" do
|
23
|
-
it "should give me back nothing" do
|
24
|
-
result = Nothing.bind do |x|
|
25
|
-
rturn x + x
|
26
|
-
end
|
27
|
-
expect(result).to eq Nothing
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "when i something containing a fn" do
|
32
|
-
it "should be apply to apply that fn to something" do
|
33
|
-
result = Just(21).bind do |x|
|
34
|
-
rturn x + x
|
35
|
-
end
|
36
|
-
expect(result).to eq Just(42)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|