ruby-maybe 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cadba4571e8ddf9c546222f3ca6824af468fe7d1
4
- data.tar.gz: ddfe8a59d92210e65eab97a25d343affeac30a48
3
+ metadata.gz: 5c18c0a9b1364479ee30af4cd0dd677e6d0d2834
4
+ data.tar.gz: 645ef3b0d3b468777dc58488c846c1bb1d869958
5
5
  SHA512:
6
- metadata.gz: 0e0a29cdbbc478a7f0f1ba59febf81577cffc27803a2d4585c422c3b93bf64475217f339a8da1c8b1847146fd0693a254cef5110b19222c0ee8983eeb5db125e
7
- data.tar.gz: 783603c6b507eb8afd188e5ff92c23ff9b2f8dfa31660b8a2d8a9fb2cb090ab715e19968136ca470c724572644cfe37a8a7839b3737bd6f59c73f5469c560e47
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
- if Maybe.method_defined?(method_name)
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
- 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
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
- if Nothing.method_defined?(method_name)
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
@@ -3,7 +3,7 @@ $:.unshift lib unless $:.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "ruby-maybe"
6
- s.version = "0.1.0"
6
+ s.version = "0.1.1"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Callum Stott"]
9
9
  s.email = ["callum.stott@me.com"]
@@ -87,7 +87,7 @@ describe "Maybe" do
87
87
  end
88
88
 
89
89
  describe "method lifting" do
90
- it "returns Nothing for any method call" do
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.0
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-03 00:00:00.000000000 Z
11
+ date: 2013-03-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: