ruby-maybe 0.1.0 → 0.1.1
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/README.md +43 -0
- data/lib/ruby-maybe.rb +8 -19
- data/ruby-maybe.gemspec +1 -1
- data/spec/ruby-maybe_spec.rb +2 -2
- 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: 5c18c0a9b1364479ee30af4cd0dd677e6d0d2834
|
4
|
+
data.tar.gz: 645ef3b0d3b468777dc58488c846c1bb1d869958
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c62cb16a1ca0177720af7c3c0e646c5ed80f73612522d9e862616c0282be813a639d6d1e2a1bb17b8f283c141625f331f1a84c8f56f20cc9c0e617c8a58e8b70
|
7
|
+
data.tar.gz: b176635e49c8e5b7e173f636245ccbe939eff8a4add5c3e794d74d777c744d6cb1769085e71c1f727db08e50b393be0504ae2a8e01be93040bb48df01f9b3ea6
|
data/README.md
CHANGED
@@ -12,6 +12,49 @@ 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
|
13
13
|
deal with values that may or may not be undefined.
|
14
14
|
|
15
|
+
Imagine we are access accessing an array via a method:
|
16
|
+
|
17
|
+
array = [1,2,3]
|
18
|
+
|
19
|
+
def access(n)
|
20
|
+
array[n]
|
21
|
+
end
|
22
|
+
|
23
|
+
access(1) # => 2
|
24
|
+
access(5) # => nil
|
25
|
+
|
26
|
+
Now what if we want to take some index and add `5` to the array value
|
27
|
+
stored there?
|
28
|
+
|
29
|
+
access(5) + 5 # => NoMethodError: undefined method `+' for nil:NilClass
|
30
|
+
|
31
|
+
Hmmm, that fails pretty miserably. We can solve this with `Maybe`. Lets
|
32
|
+
rewrite `access`:
|
33
|
+
|
34
|
+
def access(n)
|
35
|
+
if array[n]
|
36
|
+
Just.new(array[n])
|
37
|
+
else
|
38
|
+
Nothing
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
`Nothing` and `Just` used here are both instances of `Maybe`. This means
|
43
|
+
that they will both respond to the `bind` method:
|
44
|
+
|
45
|
+
access(1).bind { |val| Just.new(val + 5) } # => Just.new(7)
|
46
|
+
access(5).bind { |val| Just.new(val + 5) } # => Nothing.new
|
47
|
+
|
48
|
+
For instances of `Just`, `bind` will execute the passed block with
|
49
|
+
respect to its contained value and for `Nothing` it will skip the block
|
50
|
+
and simply return another instance of `Nothing`. This allows a neat
|
51
|
+
mechanism for dealing with non determinitic methods such as `access`
|
52
|
+
without having them throw exceptions or simply return `nil`.
|
53
|
+
|
54
|
+
`Maybe` is a very basic monad and at first might not seem that powerful
|
55
|
+
but after using it instead of the more verbose control flow it replaces
|
56
|
+
I think you might learn to love it.
|
57
|
+
|
15
58
|
## Installation
|
16
59
|
|
17
60
|
Either include in your Gemfile:
|
data/lib/ruby-maybe.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
class Maybe
|
2
2
|
def method_missing(method_name, *args, &block)
|
3
|
-
|
4
|
-
super
|
5
|
-
else
|
6
|
-
Maybe.new
|
7
|
-
end
|
3
|
+
Maybe.new
|
8
4
|
end
|
9
5
|
|
10
6
|
def bind(&block)
|
@@ -14,15 +10,12 @@ end
|
|
14
10
|
|
15
11
|
class Just < Maybe
|
16
12
|
def method_missing(method_name, *args, &block)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
}
|
24
|
-
Just.new(@value.public_send(method_name, *values, &block))
|
25
|
-
end
|
13
|
+
values = args.map { |arg|
|
14
|
+
return Nothing.new if arg == Nothing.new
|
15
|
+
arg.kind_of?(Just) ? arg.value : arg
|
16
|
+
}
|
17
|
+
|
18
|
+
Just.new(@value.public_send(method_name, *values, &block))
|
26
19
|
end
|
27
20
|
|
28
21
|
def initialize(value)
|
@@ -52,11 +45,7 @@ end
|
|
52
45
|
|
53
46
|
class Nothing < Maybe
|
54
47
|
def method_missing(method_name, *args, &block)
|
55
|
-
|
56
|
-
super
|
57
|
-
else
|
58
|
-
Nothing.new
|
59
|
-
end
|
48
|
+
Nothing.new
|
60
49
|
end
|
61
50
|
|
62
51
|
def bind
|
data/ruby-maybe.gemspec
CHANGED
data/spec/ruby-maybe_spec.rb
CHANGED
@@ -87,7 +87,7 @@ describe "Maybe" do
|
|
87
87
|
end
|
88
88
|
|
89
89
|
describe "method lifting" do
|
90
|
-
it "returns
|
90
|
+
it "returns Maybe for any method call" do
|
91
91
|
Maybe.new.missing.kind_of?(Maybe).must_equal(true)
|
92
92
|
end
|
93
93
|
end
|
@@ -101,7 +101,7 @@ describe "Integration Specs" do
|
|
101
101
|
if (val > 10)
|
102
102
|
Nothing.new
|
103
103
|
else
|
104
|
-
Just.new(val)
|
104
|
+
Just.new(val) + 1
|
105
105
|
end
|
106
106
|
}.must_equal(Nothing.new)
|
107
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-maybe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Callum Stott
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
11
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|