totally_lazy 0.1.14 → 0.1.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/numbers.rb +4 -0
- data/lib/option.rb +19 -4
- data/spec/option_spec.rb +17 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
M2FmZDUwYjhlMzkyMjQ5ZjQ0ZTc4NGVlZWI3MzhjMWE5MjMxNjRhYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NTJkODZiODkxZDQ3YWY5MTExMmRlODM4NGIyYzZkZTIyMmFmOGYwZg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OTk0ZDNlMjFkYWQzYzhmZWVjMjhmOGZkMTNlYTdkM2M0Y2M0NGQzNWEyMDQ1
|
10
|
+
ZDc2OTI2M2ExMDM3MzZiMzk2ZjU0NGMwOTAzNjgxMmIyNzYzMjdlMDRhMTVi
|
11
|
+
ODc1ZjUxNGI4NWNmN2M2Mzk4NzVmZjEyNzQxN2ZmNGFiNjQxOGU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZGMyMzg4NTg0YWIyMDZjMjE2MGY3NjliNjg0OWRiNTFmMjMzNGFkZjJkNzll
|
14
|
+
YWNlZDhiZTQ4ODg4MDg5ZmE2NGZkODczNWE0NjFkM2JlMTBiMjUyMDdiNTQ2
|
15
|
+
YWJhMDY0YmVkNDcwM2I0MDhmMzJjMTUyODZkODhmYTM4ODg5NTU=
|
data/lib/numbers.rb
CHANGED
data/lib/option.rb
CHANGED
@@ -47,6 +47,17 @@ module Option
|
|
47
47
|
some(fn.(value))
|
48
48
|
end
|
49
49
|
|
50
|
+
def fold(seed, fn=nil, &block)
|
51
|
+
assert_funcs(fn, block_given?)
|
52
|
+
block_given? ? block.call(seed, @value) : fn.(seed, @value)
|
53
|
+
end
|
54
|
+
|
55
|
+
alias fold_left fold
|
56
|
+
|
57
|
+
def is_empty?
|
58
|
+
false
|
59
|
+
end
|
60
|
+
|
50
61
|
def enumerator
|
51
62
|
Enumerator.new { |y|
|
52
63
|
y << @value
|
@@ -54,10 +65,6 @@ module Option
|
|
54
65
|
}
|
55
66
|
end
|
56
67
|
|
57
|
-
def is_empty?
|
58
|
-
false
|
59
|
-
end
|
60
|
-
|
61
68
|
def <=>(other)
|
62
69
|
@value <=> other.value
|
63
70
|
end
|
@@ -77,6 +84,7 @@ module Option
|
|
77
84
|
end
|
78
85
|
|
79
86
|
def exists?(fn_pred=nil, &block_pred)
|
87
|
+
assert_funcs(fn_pred, block_given?)
|
80
88
|
false
|
81
89
|
end
|
82
90
|
|
@@ -84,6 +92,13 @@ module Option
|
|
84
92
|
none
|
85
93
|
end
|
86
94
|
|
95
|
+
def fold(seed, fn=nil &block)
|
96
|
+
assert_funcs(fn, block_given?)
|
97
|
+
seed
|
98
|
+
end
|
99
|
+
|
100
|
+
alias fold_left fold
|
101
|
+
|
87
102
|
def enumerator
|
88
103
|
Enumerator.new { |y|
|
89
104
|
raise StopIteration.new
|
data/spec/option_spec.rb
CHANGED
@@ -28,8 +28,23 @@ describe 'Option' do
|
|
28
28
|
expect(none.is?(greater_than(0))).to eq(false)
|
29
29
|
end
|
30
30
|
|
31
|
+
it 'should support fold (aka fold_left)' do
|
32
|
+
expect(option(1).fold(1, add)).to eq(2)
|
33
|
+
expect(option(1).fold_left(1, add)).to eq(2)
|
34
|
+
expect(option(1).fold(1) { |a, b| a + b }).to eq(2)
|
35
|
+
expect(some(1).fold(1, add)).to eq(2)
|
36
|
+
expect(none.fold(1, add)).to eq(1)
|
37
|
+
end
|
38
|
+
|
31
39
|
it 'should raise exception if you try to use both lambda and block' do
|
32
|
-
expect {
|
33
|
-
expect {
|
40
|
+
expect { some(1).exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
|
41
|
+
expect { none.exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
|
42
|
+
expect { some(1).is?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
|
43
|
+
expect { none.is?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
|
44
|
+
expect { some(1).fold(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
|
45
|
+
expect { none.fold(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
|
46
|
+
expect { some(1).fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
|
47
|
+
expect { none.fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
|
34
48
|
end
|
49
|
+
|
35
50
|
end
|