totally_lazy 0.1.28 → 0.1.29
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 +8 -8
- data/lib/totally_lazy/either.rb +21 -0
- data/spec/totally_lazy/either_spec.rb +17 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTY1MTJjY2YzODk1NWFlZjI0Y2VjYzYyNzEyZWI0NTFlMjU5ZmI2NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NGE3YmJlNTg4NGRiY2I3YmFkYmUxODU3N2U2ODBiNzc3ZTlhN2Q2YQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Njg1ZTY4MjM5ZTcwNzc0YTI4NzMzZDNkYWQ5MjY2NmJiMDZiZTRiYjZjOGUy
|
10
|
+
ZjQzMGM2YWY4NDZmN2ZhNWJmMGI2YzU2OGNhMTc4Njg1MTFmZDFjYjFmYWU0
|
11
|
+
NjEyMWVjMmI3NTRmNDliMzgxMTI4NmQyOTg1YzcyYjY0NDNjNTc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTU2OTllZjkzZmRhZmI5NDZkNjg3ZmY5NWExYWVlMzk3MDJlNTdlODZlZDFh
|
14
|
+
M2Y5MGZmYTRjZGY4OGFkMDg2ODFlNGY5MWE0ZmExZDljMDQ1NGYyZjBhYTgy
|
15
|
+
MGQyOGFmNGRiNGJlYWZmMjZiMGY5MzkyOWNmOWI2ZWJhNzhlODY=
|
data/lib/totally_lazy/either.rb
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
require_relative 'lambda_block'
|
2
2
|
|
3
|
+
class Proc
|
4
|
+
def or_exception
|
5
|
+
-> (value) {
|
6
|
+
begin
|
7
|
+
right(self.(value))
|
8
|
+
rescue Exception => e
|
9
|
+
left(e)
|
10
|
+
end
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
module Eithers
|
4
16
|
private
|
5
17
|
def left(value)
|
@@ -54,6 +66,10 @@ class Left < Either
|
|
54
66
|
left(block_given? ? block.call(@value) : fn.(@value))
|
55
67
|
end
|
56
68
|
|
69
|
+
def flat_map(fn=nil, &block) # a function which returns an either
|
70
|
+
assert_funcs(fn, block_given?)
|
71
|
+
self
|
72
|
+
end
|
57
73
|
def <=>(other)
|
58
74
|
@value <=> other.left_value
|
59
75
|
end
|
@@ -94,6 +110,11 @@ class Right < Either
|
|
94
110
|
self
|
95
111
|
end
|
96
112
|
|
113
|
+
def flat_map(fn=nil, &block) # a function which returns an either
|
114
|
+
assert_funcs(fn, block_given?)
|
115
|
+
block_given? ? block.call(@value) : fn.(@value)
|
116
|
+
end
|
117
|
+
|
97
118
|
def <=>(other)
|
98
119
|
@value <=> other.right_value
|
99
120
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
require_relative '../spec_helper'
|
2
4
|
|
3
5
|
describe 'Either' do
|
@@ -21,9 +23,24 @@ describe 'Either' do
|
|
21
23
|
expect(left(3).map_left { |a| a+2 }).to eq(left(5))
|
22
24
|
end
|
23
25
|
|
26
|
+
it 'should support flat_map' do
|
27
|
+
expect(right(4).flat_map(divide(2).or_exception)).to eq(right(2))
|
28
|
+
expect(right(4).flat_map { right(2) }).to eq(right(2))
|
29
|
+
|
30
|
+
result = right(4).flat_map(divide(0).or_exception)
|
31
|
+
expect(result.is_left?).to eq(true)
|
32
|
+
expect(result.left_value.class).to eq(ZeroDivisionError)
|
33
|
+
|
34
|
+
expect(left(2).flat_map(divide(2).or_exception)).to eq(left(2))
|
35
|
+
expect(left(2).flat_map { right(5) }).to eq(left(2))
|
36
|
+
end
|
37
|
+
|
24
38
|
it 'should raise exception if you try to use both lambda and block' do
|
25
39
|
expect { right(1).map(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
|
26
40
|
expect { right(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
|
27
41
|
expect { left(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
|
42
|
+
expect { right(10).flat_map(divide(2)) { |a| a/2 } }.to raise_error(RuntimeError)
|
43
|
+
expect { left(10).flat_map(divide(2)) { |a| a/2 } }.to raise_error(RuntimeError)
|
28
44
|
end
|
45
|
+
|
29
46
|
end
|