totally_lazy 0.1.25 → 0.1.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/totally_lazy/either.rb +75 -0
- data/lib/totally_lazy/functions.rb +8 -0
- data/lib/totally_lazy/option.rb +6 -1
- data/lib/totally_lazy/predicates.rb +8 -0
- data/lib/totally_lazy.rb +2 -0
- data/spec/totally_lazy/either_spec.rb +9 -0
- data/spec/{option_spec.rb → totally_lazy/option_spec.rb} +3 -5
- data/spec/{sequence_spec.rb → totally_lazy/sequence_spec.rb} +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MjAwMDFhNmJiNmFlNTZmMDA3ODg5ZTUxNGNjMDZhNzA1MmFkYTRmOA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDgwNjk1NTU3NDk5ZDk1OThlMTVlYWRhYmY2ZGUzNDQxOTIzNDBmNw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZTExNjEzN2FhOWJjN2MzMDI2N2RmOTZlZmZlYzUxY2U4Mzc1NGZiMmZjYzJh
|
10
|
+
ZjViMjdlYzQzMmRkNDdkZjhlNDAxYjYwZDczZDRlOTA0MTY4NWYzYjlmMWNj
|
11
|
+
ZjJiODM4MDY1OWZkNmJhNjA5NTc5NDZkOGVmNTFmYTkzZGQwNmQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YWIxNzc0N2EwYTRkNDA1Y2RmM2Q0MGFkMTRlZjk4Njk5ODQ5YWQxMWM2OWNj
|
14
|
+
ZGM4ZTRiYWYwN2JlZGQ4MDNjZTQ1OGYyMDlhZWIyYTVjZmQxNzgyN2ViZDgw
|
15
|
+
ZjExODFmYzkwYjk3YTZlMGZjNWFjYmI0ODljMjhiMjJkY2JmMmI=
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Eithers
|
2
|
+
private
|
3
|
+
def left(value)
|
4
|
+
Either.left(value)
|
5
|
+
end
|
6
|
+
|
7
|
+
def right(value)
|
8
|
+
Either.right(value)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Either
|
13
|
+
include Comparable
|
14
|
+
|
15
|
+
def self.left(value)
|
16
|
+
Left.new(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.right(value)
|
20
|
+
Right.new(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class Left < Either
|
26
|
+
def initialize(value)
|
27
|
+
@value = value
|
28
|
+
end
|
29
|
+
|
30
|
+
def is_left?
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def is_right?
|
35
|
+
false
|
36
|
+
end
|
37
|
+
|
38
|
+
def left
|
39
|
+
@value
|
40
|
+
end
|
41
|
+
|
42
|
+
def right
|
43
|
+
raise NoSuchElementException.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def <=>(other)
|
47
|
+
@value <=> other.left
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Right < Either
|
52
|
+
def initialize(value)
|
53
|
+
@value = value
|
54
|
+
end
|
55
|
+
|
56
|
+
def is_left?
|
57
|
+
false
|
58
|
+
end
|
59
|
+
|
60
|
+
def is_right?
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
def left
|
65
|
+
raise NoSuchElementException.new
|
66
|
+
end
|
67
|
+
|
68
|
+
def right
|
69
|
+
@value
|
70
|
+
end
|
71
|
+
|
72
|
+
def <=>(other)
|
73
|
+
@value <=> other.right
|
74
|
+
end
|
75
|
+
end
|
data/lib/totally_lazy/option.rb
CHANGED
@@ -28,6 +28,8 @@ module Options
|
|
28
28
|
end
|
29
29
|
|
30
30
|
class Option
|
31
|
+
include Comparable
|
32
|
+
|
31
33
|
def self.option(value)
|
32
34
|
value.nil? ? none : some(value)
|
33
35
|
end
|
@@ -55,7 +57,6 @@ class Option
|
|
55
57
|
end
|
56
58
|
|
57
59
|
class Some < Option
|
58
|
-
include Comparable
|
59
60
|
include LambdaBlock
|
60
61
|
|
61
62
|
attr_reader :value
|
@@ -202,6 +203,10 @@ class None < Option
|
|
202
203
|
}
|
203
204
|
end
|
204
205
|
|
206
|
+
def <=>(other)
|
207
|
+
other == NONE
|
208
|
+
end
|
209
|
+
|
205
210
|
def to_s
|
206
211
|
'None'
|
207
212
|
end
|
data/lib/totally_lazy.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require_relative 'totally_lazy/comparators'
|
2
|
+
require_relative 'totally_lazy/either'
|
2
3
|
require_relative 'totally_lazy/enumerators'
|
3
4
|
require_relative 'totally_lazy/functions'
|
4
5
|
require_relative 'totally_lazy/numbers'
|
@@ -9,6 +10,7 @@ require_relative 'totally_lazy/sequence'
|
|
9
10
|
require_relative 'totally_lazy/strings'
|
10
11
|
|
11
12
|
include Comparators
|
13
|
+
include Eithers
|
12
14
|
include Enumerators
|
13
15
|
include Functions
|
14
16
|
include Numbers
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe 'Either' do
|
4
|
+
it 'should support filter and map' do
|
5
|
+
eithers = sequence(left('error'), right(3))
|
6
|
+
expect(eithers.filter(is_left?).map(get_left)).to eq(sequence('error'))
|
7
|
+
expect(eithers.filter(is_right?).map(get_right)).to eq(sequence(3))
|
8
|
+
end
|
9
|
+
end
|
@@ -1,7 +1,6 @@
|
|
1
|
-
require_relative 'spec_helper'
|
1
|
+
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe 'Option' do
|
4
|
-
|
5
4
|
it 'should support is_empty? & is_defined?' do
|
6
5
|
expect(some(1).is_empty?).to eq(false)
|
7
6
|
expect(some(1).is_defined?).to eq(true)
|
@@ -94,7 +93,7 @@ describe 'Option' do
|
|
94
93
|
end
|
95
94
|
|
96
95
|
it 'should support get_or_raise' do
|
97
|
-
expect(some(
|
96
|
+
expect(some('bob').get_or_raise(RuntimeError.new)).to eq('bob')
|
98
97
|
expect{none.get_or_raise(RuntimeError.new)}.to raise_error(RuntimeError)
|
99
98
|
end
|
100
99
|
|
@@ -112,5 +111,4 @@ describe 'Option' do
|
|
112
111
|
expect { some(4).flat_map(divide(2).optional) { |v| divide(2).optional.(v) } }.to raise_error(RuntimeError)
|
113
112
|
expect { some(1).get_or_else(returns(2)) { |value| 3 } }.to raise_error(RuntimeError)
|
114
113
|
end
|
115
|
-
|
116
|
-
end
|
114
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: totally_lazy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.26
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Raymond Barlow
|
@@ -156,6 +156,7 @@ files:
|
|
156
156
|
- contributors.txt
|
157
157
|
- lib/totally_lazy.rb
|
158
158
|
- lib/totally_lazy/comparators.rb
|
159
|
+
- lib/totally_lazy/either.rb
|
159
160
|
- lib/totally_lazy/enumerators.rb
|
160
161
|
- lib/totally_lazy/functions.rb
|
161
162
|
- lib/totally_lazy/lambda_block.rb
|
@@ -166,9 +167,10 @@ files:
|
|
166
167
|
- lib/totally_lazy/sequence.rb
|
167
168
|
- lib/totally_lazy/strings.rb
|
168
169
|
- readme.md
|
169
|
-
- spec/option_spec.rb
|
170
|
-
- spec/sequence_spec.rb
|
171
170
|
- spec/spec_helper.rb
|
171
|
+
- spec/totally_lazy/either_spec.rb
|
172
|
+
- spec/totally_lazy/option_spec.rb
|
173
|
+
- spec/totally_lazy/sequence_spec.rb
|
172
174
|
- totally_lazy.iml
|
173
175
|
homepage: http://github.com/raymanoz/totally_lazy
|
174
176
|
licenses:
|