ruby-maybe 0.0.2 → 0.1.0
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 +7 -0
- data/README.md +1 -1
- data/lib/ruby-maybe.rb +28 -0
- data/ruby-maybe.gemspec +1 -1
- data/spec/ruby-maybe_spec.rb +47 -11
- metadata +6 -8
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cadba4571e8ddf9c546222f3ca6824af468fe7d1
|
4
|
+
data.tar.gz: ddfe8a59d92210e65eab97a25d343affeac30a48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e0a29cdbbc478a7f0f1ba59febf81577cffc27803a2d4585c422c3b93bf64475217f339a8da1c8b1847146fd0693a254cef5110b19222c0ee8983eeb5db125e
|
7
|
+
data.tar.gz: 783603c6b507eb8afd188e5ff92c23ff9b2f8dfa31660b8a2d8a9fb2cb090ab715e19968136ca470c724572644cfe37a8a7839b3737bd6f59c73f5469c560e47
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
## Description
|
8
8
|
|
9
|
-
'Maybe' not 'Maeby'. This is an implementation of the Maybe monad used
|
9
|
+
'Maybe' not 'Maeby'. This is an implementation of the 'Maybe' or 'Option' monad similar to that used
|
10
10
|
[Haskell](http://www.haskell.org/haskellwiki/Maybe). Monads provide a
|
11
11
|
safe way to create non deterministic programs (for example, when not all
|
12
12
|
values are known at compile time). The Maybe monad allows programmers to
|
data/lib/ruby-maybe.rb
CHANGED
@@ -1,10 +1,30 @@
|
|
1
1
|
class Maybe
|
2
|
+
def method_missing(method_name, *args, &block)
|
3
|
+
if Maybe.method_defined?(method_name)
|
4
|
+
super
|
5
|
+
else
|
6
|
+
Maybe.new
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
2
10
|
def bind(&block)
|
3
11
|
Maybe.new
|
4
12
|
end
|
5
13
|
end
|
6
14
|
|
7
15
|
class Just < Maybe
|
16
|
+
def method_missing(method_name, *args, &block)
|
17
|
+
if Just.method_defined?(method_name)
|
18
|
+
super
|
19
|
+
else
|
20
|
+
values = args.map { |arg|
|
21
|
+
return Nothing.new if arg == Nothing.new
|
22
|
+
arg.kind_of?(Just) ? arg.value : arg
|
23
|
+
}
|
24
|
+
Just.new(@value.public_send(method_name, *values, &block))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
8
28
|
def initialize(value)
|
9
29
|
@value = value
|
10
30
|
end
|
@@ -31,6 +51,14 @@ class Just < Maybe
|
|
31
51
|
end
|
32
52
|
|
33
53
|
class Nothing < Maybe
|
54
|
+
def method_missing(method_name, *args, &block)
|
55
|
+
if Nothing.method_defined?(method_name)
|
56
|
+
super
|
57
|
+
else
|
58
|
+
Nothing.new
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
34
62
|
def bind
|
35
63
|
Nothing.new
|
36
64
|
end
|
data/ruby-maybe.gemspec
CHANGED
data/spec/ruby-maybe_spec.rb
CHANGED
@@ -7,6 +7,40 @@ describe "Just" do
|
|
7
7
|
Just.new(5).bind { |val| Just.new(val * 2) }.must_equal Just.new(10)
|
8
8
|
end
|
9
9
|
end
|
10
|
+
|
11
|
+
describe "#==" do
|
12
|
+
it "returns false if the passed object is not a Just" do
|
13
|
+
(Just.new(5) == 5).must_equal false
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns true if the passed object is a Just with the same value" do
|
17
|
+
(Just.new(5) == Just.new(5)).must_equal true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "method lifting" do
|
22
|
+
it "allows using a method for the contained type" do
|
23
|
+
just = Just.new(5)
|
24
|
+
just.+(5).must_equal(Just.new(10))
|
25
|
+
end
|
26
|
+
|
27
|
+
it "allows passed arguments to be Maybe instances" do
|
28
|
+
just = Just.new(5)
|
29
|
+
just.+(Just.new(5)).must_equal(Just.new(10))
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns Nothing if any argument is Nothing" do
|
33
|
+
just = Just.new("Hello")
|
34
|
+
just.slice(Just.new(0), Nothing.new).must_equal(Nothing.new)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "passes blocks correctly" do
|
38
|
+
count = 0
|
39
|
+
just = Just.new(5)
|
40
|
+
just.times { |i| count += i }
|
41
|
+
count.must_equal(10)
|
42
|
+
end
|
43
|
+
end
|
10
44
|
end
|
11
45
|
|
12
46
|
describe "Nothing" do
|
@@ -23,12 +57,18 @@ describe "Nothing" do
|
|
23
57
|
end
|
24
58
|
|
25
59
|
describe "#==" do
|
26
|
-
it "returns false if the passed object is not a
|
27
|
-
(
|
60
|
+
it "returns false if the passed object is not a Nothing" do
|
61
|
+
(Nothing.new == 5).must_equal false
|
28
62
|
end
|
29
63
|
|
30
|
-
it "returns true if the passed object is a
|
31
|
-
(
|
64
|
+
it "returns true if the passed object is a Nothing" do
|
65
|
+
(Nothing.new == Nothing.new).must_equal true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "method lifting" do
|
70
|
+
it "returns Nothing for any method call" do
|
71
|
+
Nothing.new.missing.must_equal(Nothing.new)
|
32
72
|
end
|
33
73
|
end
|
34
74
|
end
|
@@ -46,13 +86,9 @@ describe "Maybe" do
|
|
46
86
|
end
|
47
87
|
end
|
48
88
|
|
49
|
-
describe "
|
50
|
-
it "returns
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
it "returns true if the passed object is a Nothing" do
|
55
|
-
(Nothing.new == Nothing.new).must_equal true
|
89
|
+
describe "method lifting" do
|
90
|
+
it "returns Nothing for any method call" do
|
91
|
+
Maybe.new.missing.kind_of?(Maybe).must_equal(true)
|
56
92
|
end
|
57
93
|
end
|
58
94
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-maybe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Callum Stott
|
@@ -28,26 +27,25 @@ files:
|
|
28
27
|
- spec/ruby-maybe_spec.rb
|
29
28
|
homepage:
|
30
29
|
licenses: []
|
30
|
+
metadata: {}
|
31
31
|
post_install_message:
|
32
32
|
rdoc_options: []
|
33
33
|
require_paths:
|
34
34
|
- lib
|
35
35
|
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
-
none: false
|
37
36
|
requirements:
|
38
|
-
- -
|
37
|
+
- - '>='
|
39
38
|
- !ruby/object:Gem::Version
|
40
39
|
version: '0'
|
41
40
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
-
none: false
|
43
41
|
requirements:
|
44
|
-
- -
|
42
|
+
- - '>='
|
45
43
|
- !ruby/object:Gem::Version
|
46
44
|
version: '0'
|
47
45
|
requirements: []
|
48
46
|
rubyforge_project:
|
49
|
-
rubygems_version:
|
47
|
+
rubygems_version: 2.0.0
|
50
48
|
signing_key:
|
51
|
-
specification_version:
|
49
|
+
specification_version: 4
|
52
50
|
summary: Maybe monad implementation for Ruby
|
53
51
|
test_files: []
|