monad-maybe 0.9.1 → 0.9.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 115ca1c42c927a7d5ae489333dce07dbf1090e18
4
- data.tar.gz: 881609187366be6aa31e0c2555ccbf92f9fe8cdc
3
+ metadata.gz: f4ff513fcc26d6a2a1034ca7628d787994f1f4c4
4
+ data.tar.gz: a44413edb8240cea79f755c6374981424ca97d86
5
5
  SHA512:
6
- metadata.gz: 75a253437966cda5b6c41ae4f34c85b13fd84d66bc9deba9bd7ca11aaed69a73a72b50761d991efb17ad31719624043ac52152df834b54ea8a25c3decfcc90ba
7
- data.tar.gz: 5ea2adcb6c3ee400772cb9feef134bd4693697c99f2a453443eb2caa7257b90649bebe9a19c617ebc2f2de812acf24c8ab9757244a9e01789087f864bbda52b3
6
+ metadata.gz: c71be1ce7e5f7e6f487cbaefaac244128215218d83a5375954dab890e378e35836a38483dbd37629f624335d2ac803a13a02b1c75fa06d436ae9cffda302118d
7
+ data.tar.gz: 6efd96a3d2581d1204c6dc0c0dd8268d305ff460515cf75228ad5c2c110c43b27128368fa93bf9e9481717d235e5cb3bb5729db2d172a2b3dd51c6cfab7f0487
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ Jeweler::Tasks.new do |gem|
3
3
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
4
4
  gem.name = "monad-maybe"
5
5
  gem.summary = %Q{A Ruby implementation of Haskell's Maybe Monad}
6
- gem.description = %Q{This is an attempt to implement Haskell's Maybe monad in a Ruby-ish way with as little monkey patching as possible.}
6
+ gem.description = %Q{This is an attempt to implement Haskell's Maybe monad in a Ruby-ish way.}
7
7
  gem.email = "delon.newman@gmail.com"
8
8
  gem.homepage = "https://github.com/delonnewman/monad-maybe"
9
9
  gem.authors = ["Delon Newman"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.1
1
+ 0.9.2
data/lib/monad/maybe.rb CHANGED
@@ -21,13 +21,17 @@ module Monad
21
21
  class ::Range; include Enumerable end
22
22
 
23
23
  class ::Object
24
- def maybe(&blk)
25
- if blk
26
- Just.new(blk.call(self))
24
+ def maybe(obj=self, &blk)
25
+ if obj && blk
26
+ blk.call(obj).to_maybe
27
27
  else
28
- Just.new(self)
28
+ obj.to_maybe
29
29
  end
30
30
  end
31
+
32
+ def to_maybe
33
+ Just.new(self)
34
+ end
31
35
 
32
36
  def maybe?
33
37
  false
@@ -47,19 +51,19 @@ module Monad
47
51
  end
48
52
 
49
53
  class ::NilClass
50
- def maybe(&blk)
54
+ def to_maybe
51
55
  Nothing.instance.freeze
52
56
  end
53
57
 
58
+ def maybe(&blk)
59
+ to_maybe
60
+ end
61
+
54
62
  def something?
55
63
  false
56
64
  end
57
65
  end
58
66
 
59
- def maybe(o, &blk)
60
- o.maybe(&blk)
61
- end
62
-
63
67
  def just(o)
64
68
  Just.new(o)
65
69
  end
@@ -11,10 +11,13 @@ module Monad
11
11
  true
12
12
  end
13
13
 
14
-
15
14
  def to_list
16
15
  List.new(to_a)
17
16
  end
17
+
18
+ def to_maybe
19
+ self
20
+ end
18
21
 
19
22
  private
20
23
  def initialize; end
@@ -11,7 +11,7 @@ module Monad
11
11
  end
12
12
 
13
13
  def method_missing(method, *args)
14
- Just.new(value.send(method, *args))
14
+ value.send(method, *args).maybe
15
15
  end
16
16
 
17
17
  def unwrap(val)
@@ -30,9 +30,10 @@ module Monad
30
30
  true
31
31
  end
32
32
 
33
+ # NOTE: This being able to return Nothings maybe dangerous
33
34
  def maybe(&blk)
34
35
  if blk
35
- Just.new(blk.call(self.value))
36
+ blk.call(self.value).to_maybe
36
37
  else
37
38
  self
38
39
  end
@@ -25,7 +25,7 @@ module Monad
25
25
  end
26
26
 
27
27
  def to_maybe
28
- first.maybe
28
+ first.to_maybe
29
29
  end
30
30
 
31
31
  def each
data/monad-maybe.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "monad-maybe"
8
- s.version = "0.9.1"
8
+ s.version = "0.9.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Delon Newman"]
12
- s.date = "2013-06-17"
13
- s.description = "This is an attempt to implement Haskell's Maybe monad in a Ruby-ish way with as little monkey patching as possible."
12
+ s.date = "2013-06-18"
13
+ s.description = "This is an attempt to implement Haskell's Maybe monad in a Ruby-ish way."
14
14
  s.email = "delon.newman@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "README.md"
data/test/maybe.rb CHANGED
@@ -52,11 +52,15 @@ class MaybeTest < Test::Unit::TestCase
52
52
  nil.maybe do
53
53
  raise Exception, "This should not be called"
54
54
  end
55
+ end
55
56
 
57
+ assert_nothing_raised do
56
58
  nothing.maybe do
57
59
  raise Exception, "This should not be called"
58
60
  end
61
+ end
59
62
 
63
+ assert_nothing_raised do
60
64
  maybe(nil) do
61
65
  raise Exception, "This should not be called"
62
66
  end
@@ -75,6 +79,18 @@ class MaybeTest < Test::Unit::TestCase
75
79
  end
76
80
  end
77
81
 
82
+ def test_maybe_block_return_value_and_type
83
+ m = 1.maybe { |n| n + 1 }
84
+ assert_equal 2, m.value
85
+ assert m.maybe?
86
+ assert m.just?
87
+
88
+ o = 2.maybe { nil }
89
+ assert_equal nil, o.value
90
+ assert o.maybe?
91
+ assert o.nothing?
92
+ end
93
+
78
94
  def test_something?
79
95
  assert_equal false, nil.something?
80
96
  assert_equal false, nothing.something?
@@ -82,4 +98,13 @@ class MaybeTest < Test::Unit::TestCase
82
98
  assert 1.maybe.something?
83
99
  assert (0..10).maybe_map { |n| n }.something?
84
100
  end
101
+
102
+ def test_to_maybe
103
+ assert_equal just(1), 1.to_maybe
104
+ assert_equal just(1), just(1).to_maybe
105
+ assert_equal nothing, nil.to_maybe
106
+ assert_equal nothing, nothing.to_maybe
107
+ assert_equal 1.to_maybe, [1].to_maybe
108
+ assert_equal 0.to_maybe, (0..10).maybe_map { |n| n }.to_maybe
109
+ end
85
110
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monad-maybe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delon Newman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-17 00:00:00.000000000 Z
11
+ date: 2013-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jeweler
@@ -52,8 +52,7 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: This is an attempt to implement Haskell's Maybe monad in a Ruby-ish way
56
- with as little monkey patching as possible.
55
+ description: This is an attempt to implement Haskell's Maybe monad in a Ruby-ish way.
57
56
  email: delon.newman@gmail.com
58
57
  executables: []
59
58
  extensions: []