or_else 0.0.4 → 0.0.5

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: e403ed565733f3d923aee1ec9353e495e95a2536
4
- data.tar.gz: f4ffee374ca8a749251e93d9f3e67b7afbba39bf
3
+ metadata.gz: eecae4c515e30eb7444310ddad11f7decc47d06e
4
+ data.tar.gz: 8f8c822cdd62cb58b7fceff8b9b5b5734f02fcaf
5
5
  SHA512:
6
- metadata.gz: 23987290677e25336fedb1c16aaff6e6a38c807c216c16cb82842990c964e6619f61359618e0d792d0370814c743c7fb28173ef4734f4e6fcd800fbb00c40228
7
- data.tar.gz: 48821e7cbdb95382f28cf04dc70f2a8a8c632d427809eb83a0dc6f0a3517af8023474dd6ce58606945fa941033ba4f4ef72d87c12616fc30a530f499ab538226
6
+ metadata.gz: 4add6df6ad601b2290bb7dad6e259031a5d22ddef709cb28726df6b16344cdeae0bb7423d874aed45c98fe261b470657e693426e32c9632aa74bff16a5e5a04a
7
+ data.tar.gz: 00aad4d57d23adf625c2e804ad33c2e4d11eddc90097af1c9a39cb62a630b9c263b948783fb4153a24331903811b8c11dff4515b729f841da73f9e19882635c1
data/README.md CHANGED
@@ -19,29 +19,33 @@ Or install it yourself as:
19
19
  ## Usage
20
20
 
21
21
  ```ruby
22
- value = Maybe(nil) # => Nothing
23
- value.empty? # => true
24
- value.nil? # => true
25
- value.exists? # => false
22
+ # Nothings
23
+ value = Maybe(nil) # => Nothing
24
+ Maybe(nil).empty? # => true
25
+ Maybe(nil).nil? # => true
26
+ Maybe(nil).exists? # => false
26
27
  # Block is ignored
27
- value.map { |v| v } # => Nothing
28
+ Maybe(nil).map { |v| v } # => Nothing
28
29
  # Block is ignored
29
- value.flat_map { |v| Maybe(v) } # => Nothing
30
+ Maybe(nil).flat_map { |v| v } # => Nothing
30
31
  # Returns the result of the block
31
- value.or_else { 'foo' } # => 'foo'
32
-
33
- value = Maybe('value') # => <OrElse::Just>
34
- value.empty? # => false
35
- value.nil? # => false
36
- value.exists? # => true
37
- # Block is yielded the bare value and the result of the block is wrapped in a Maybe
38
- value.map { |v| v } # => <OrElse::Just>
39
- # Block is yielded the bare value, and the block exists with a Maybe so it is left as is
40
- value.flat_map { |v| Maybe(v) } # => <OrElse::Just> (Block is yielded the bare value)
41
- # Block is yielded the bare value, but did not exit with a Maybe so it is wrapped
42
- value.flat_map { |v| v } # => <OrElse::Just>
32
+ Maybe(nil).or_else { 'foo' } # => 'foo'
33
+
34
+ # Justs
35
+ Maybe('value') # => Just('value')
36
+ Maybe('value').empty? # => false
37
+ Maybe('value').nil? # => false
38
+ Maybe('value').exists? # => true
39
+ Maybe('value').map { |v| v } # => Just('value')
40
+ Maybe('value').flat_map { |v| v } # => Maybe('value')
43
41
  # Block is ignored
44
- value.or_else { 'foo' } # => 'value'
42
+ Maybe('value').or_else { 'foo' } # => 'value'
43
+
44
+ #more examples
45
+ Maybe(1).map { |v| nil } # => Just(nil)
46
+ Maybe(1).map { |v| Maybe(v) } # => Just(Maybe(1))
47
+ Maybe(1).flat_map { |v| nil } # => Nothing
48
+ Maybe(1).flat_map { |v| Maybe(v) } # => Maybe(1)
45
49
  ```
46
50
 
47
51
  Real life example:
@@ -8,12 +8,11 @@ module OrElse
8
8
  end
9
9
 
10
10
  def map
11
- Maybe(yield value)
11
+ Just(yield value)
12
12
  end
13
13
 
14
14
  def flat_map
15
- t = yield value
16
- t.is_a?(Maybe) ? t : Maybe(t)
15
+ Maybe(yield value)
17
16
  end
18
17
 
19
18
  def empty?
@@ -3,6 +3,7 @@ require 'or_else/nothing_class'
3
3
 
4
4
  def Maybe(val)
5
5
  return Nothing if val.nil?
6
+ return val if val.is_a?(OrElse::Maybe)
6
7
  OrElse::Just.new(val)
7
8
  end
8
9
 
@@ -1,3 +1,3 @@
1
1
  module OrElse
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -28,12 +28,12 @@ module OrElse
28
28
 
29
29
  specify { expect { |b| just.map(&b) }.to yield_with_args(val) }
30
30
 
31
- it 'yields the value 1 and wraps the block in a Maybe' do
31
+ it 'yields the value 1 and wraps the block in a Just' do
32
32
  expect(just.map { |j| j }.value).to eq val
33
33
  end
34
34
 
35
- it 'yields the value 1 and is Nothing when the block evaluates to nil' do
36
- expect(just.map { |j| nil }).to eq Nothing
35
+ it 'yields the value 1 and is Just(nil) when the block evaluates to nil' do
36
+ expect(just.map { |j| nil }.value).to eq nil
37
37
  end
38
38
  end
39
39
 
@@ -61,6 +61,12 @@ module OrElse
61
61
  it 'wraps the result in a Maybe' do
62
62
  expect(just.flat_map { |j| j }.value).to eq val
63
63
  end
64
+
65
+ context 'when the block exists with a nil' do
66
+ it 'returns Nothing' do
67
+ expect(just.flat_map { |j| nil }).to eq Nothing
68
+ end
69
+ end
64
70
  end
65
71
  end
66
72
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: or_else
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - alexpeachey