totally_lazy 0.1.16 → 0.1.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/functions.rb +12 -0
- data/lib/numbers.rb +4 -0
- data/lib/option.rb +10 -0
- data/spec/option_spec.rb +9 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZGUyNzc3NTNjMDYzY2UyOWU4NjFjNzhmZDZlZTJlZDM0NDJkMmU0NA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzI3YjVkZGU4YmNiMmM0Y2QxZjVlNTU2OTBkMGQyNjVhOGZmOTc1NA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZTUxOGRjMDVlNDM2NThhOWJhNDBiZjJlNzI1YzkxZjA4NGI4NWQ3ZTM1YmM1
|
10
|
+
YjgxYmUwNmU5ZTQ2ZTQ2NmEwYWY3ODJjYzVkNDExYmUyNzllODM1MjVkY2I4
|
11
|
+
NThkN2EyMWEyMDIwODRhNDhlYjBhODI2MjY2MTU2MWY1Zjk3M2U=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NTMxYzE0YTQzNzhhM2M4ZGQ5OTk0MGEyZWNmOThlNjI0YThhZDRkMDQyMGZk
|
14
|
+
NDU2NWY1YmFhYWIzMTQ0YTk0YjEzYTQ4OTZlYzBjYzBjNTczNmNhNWU1NmM2
|
15
|
+
NTYyZWMyYzhlNDI0NTFkNzU2M2FkNDkwODU1ZDlkMTkyMGZmNGE=
|
data/lib/functions.rb
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
require 'concurrent/executors'
|
2
2
|
require 'concurrent/promise'
|
3
3
|
|
4
|
+
class Proc
|
5
|
+
def to_optional
|
6
|
+
->(value) {
|
7
|
+
begin
|
8
|
+
option(self.(value))
|
9
|
+
rescue
|
10
|
+
none
|
11
|
+
end
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
4
16
|
module Functions
|
5
17
|
def monoid(fn, id)
|
6
18
|
fn.define_singleton_method(:identity) do
|
data/lib/numbers.rb
CHANGED
data/lib/option.rb
CHANGED
@@ -48,6 +48,11 @@ module Option
|
|
48
48
|
some(block_given? ? block.call(@value) : fn.(@value))
|
49
49
|
end
|
50
50
|
|
51
|
+
def flat_map(fn=nil, &block) # function should return an option
|
52
|
+
assert_funcs(fn, block_given?)
|
53
|
+
block_given? ? block.call(@value) : fn.(@value)
|
54
|
+
end
|
55
|
+
|
51
56
|
def fold(seed, fn=nil, &block)
|
52
57
|
assert_funcs(fn, block_given?)
|
53
58
|
block_given? ? block.call(seed, @value) : fn.(seed, @value)
|
@@ -94,6 +99,11 @@ module Option
|
|
94
99
|
none
|
95
100
|
end
|
96
101
|
|
102
|
+
def flat_map(fn=nil, &block) # function should return an option
|
103
|
+
assert_funcs(fn, block_given?)
|
104
|
+
none
|
105
|
+
end
|
106
|
+
|
97
107
|
def fold(seed, fn=nil &block)
|
98
108
|
assert_funcs(fn, block_given?)
|
99
109
|
seed
|
data/spec/option_spec.rb
CHANGED
@@ -44,6 +44,14 @@ describe 'Option' do
|
|
44
44
|
expect(some(2).map(ignore_and_return(nil))).to eq(some(nil)) # differs from Java TL which reutrns none
|
45
45
|
end
|
46
46
|
|
47
|
+
it 'should support flat_map' do
|
48
|
+
expect(some(4).flat_map(divide(2).to_optional)).to eq(some(2))
|
49
|
+
expect(some(4).flat_map{ |v| divide(2).to_optional.(v) }).to eq(some(2))
|
50
|
+
expect(some(4).flat_map(divide(0).to_optional)).to eq(none)
|
51
|
+
expect(none.flat_map(constant(none))).to eq(none)
|
52
|
+
expect(none.flat_map(some(4))).to eq(none)
|
53
|
+
end
|
54
|
+
|
47
55
|
it 'should raise exception if you try to use both lambda and block' do
|
48
56
|
expect { some(1).exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
|
49
57
|
expect { none.exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
|
@@ -55,6 +63,7 @@ describe 'Option' do
|
|
55
63
|
expect { none.fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
|
56
64
|
expect { some(1).map(->(v) { v.to_s }) { |v| v.to_s } }.to raise_error(RuntimeError)
|
57
65
|
expect { none.map(->(v) { v.to_s }) { |v| v.to_s } }.to raise_error(RuntimeError)
|
66
|
+
expect { some(4).flat_map(divide(2).to_optional){ |v| divide(2).to_optional.(v) } }.to raise_error(RuntimeError)
|
58
67
|
end
|
59
68
|
|
60
69
|
end
|