totally_lazy 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/option.rb +105 -8
- data/lib/sequence.rb +15 -0
- data/spec/option_spec.rb +50 -0
- data/spec/sequence_spec.rb +8 -0
- data/totally_lazy.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaad07a76c011ba5573c34c69bf011a0a6c83dcf
|
4
|
+
data.tar.gz: 0d6623c562462a5e936b60bcee8d43ea919fa4bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9000dde503c5754b186847d11e300de5cc53ea8c48f5a0806ecd3847b3f565870cce50e3f7a330df184a0e2af4fd534bc06dfce1abbcfc5d38571128b87e122d
|
7
|
+
data.tar.gz: 8dc151abd744dbb82190bc7774d431f868cabd9b856a62e275475601e7416cefe053f9e4fb71e50c4f7f6a87c32587a5091481632d244a038b63da84cfea97a4
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.8
|
data/lib/option.rb
CHANGED
@@ -22,6 +22,8 @@ module Option
|
|
22
22
|
raise(Exception, 'some cannot be nil') if @content.nil?
|
23
23
|
end
|
24
24
|
|
25
|
+
|
26
|
+
|
25
27
|
def <=>(object)
|
26
28
|
self.state <=> object.state
|
27
29
|
end
|
@@ -58,8 +60,8 @@ module Option
|
|
58
60
|
blank? ? nil : @content
|
59
61
|
end
|
60
62
|
|
61
|
-
def get_or_throw(exception,message='')
|
62
|
-
blank? ? raise(exception,message) : @content
|
63
|
+
def get_or_throw(exception, message='')
|
64
|
+
blank? ? raise(exception, message) : @content
|
63
65
|
end
|
64
66
|
|
65
67
|
def to_seq
|
@@ -71,13 +73,63 @@ module Option
|
|
71
73
|
end
|
72
74
|
|
73
75
|
def exists?(predicate)
|
74
|
-
|
76
|
+
value
|
75
77
|
end
|
76
78
|
|
77
79
|
def join(target_sequence)
|
78
80
|
sequence(value) << target_sequence
|
79
81
|
end
|
80
82
|
|
83
|
+
def map(predicate=nil, &block)
|
84
|
+
sequence(@content).map(predicate, &block).head_option
|
85
|
+
end
|
86
|
+
|
87
|
+
alias collect map
|
88
|
+
|
89
|
+
def select(predicate=nil, &block)
|
90
|
+
sequence(@content).select(predicate, &block).head_option
|
91
|
+
end
|
92
|
+
|
93
|
+
alias find_all select
|
94
|
+
alias filter select
|
95
|
+
|
96
|
+
def reject(predicate=nil, &block)
|
97
|
+
sequence(@content).reject(predicate, &block).head_option
|
98
|
+
end
|
99
|
+
|
100
|
+
alias unfilter reject
|
101
|
+
|
102
|
+
def grep(pattern)
|
103
|
+
sequence(@content).grep(pattern).head_option
|
104
|
+
end
|
105
|
+
|
106
|
+
def drop(n)
|
107
|
+
sequence(@content).drop(n).head_option
|
108
|
+
end
|
109
|
+
|
110
|
+
def drop_while(&block)
|
111
|
+
sequence(@content).drop_while(&block).head_option
|
112
|
+
end
|
113
|
+
|
114
|
+
def take(n)
|
115
|
+
sequence(@content).take(n).head_option
|
116
|
+
end
|
117
|
+
|
118
|
+
def take_while(&block)
|
119
|
+
sequence(@content).take_while(&block).head_option
|
120
|
+
end
|
121
|
+
|
122
|
+
def flat_map(&block)
|
123
|
+
sequence(@content).flat_map(&block).head_option
|
124
|
+
end
|
125
|
+
|
126
|
+
alias collect_concat flat_map
|
127
|
+
|
128
|
+
# TODO - fix me
|
129
|
+
# def zip(*args, &block)
|
130
|
+
# sequence(@content).zip(*args,&block).head_option
|
131
|
+
# end
|
132
|
+
|
81
133
|
alias + join
|
82
134
|
alias << join
|
83
135
|
|
@@ -127,19 +179,19 @@ module Option
|
|
127
179
|
end
|
128
180
|
|
129
181
|
def defined?
|
130
|
-
|
182
|
+
false
|
131
183
|
end
|
132
184
|
|
133
185
|
def get_or_else(item)
|
134
|
-
|
186
|
+
item
|
135
187
|
end
|
136
188
|
|
137
189
|
def get_or_nil
|
138
190
|
nil
|
139
191
|
end
|
140
192
|
|
141
|
-
def get_or_throw(exception,message='')
|
142
|
-
raise(exception,message)
|
193
|
+
def get_or_throw(exception, message='')
|
194
|
+
raise(exception, message)
|
143
195
|
end
|
144
196
|
|
145
197
|
def to_seq
|
@@ -147,7 +199,7 @@ module Option
|
|
147
199
|
end
|
148
200
|
|
149
201
|
def contains(item)
|
150
|
-
|
202
|
+
false
|
151
203
|
end
|
152
204
|
|
153
205
|
def exists?(predicate)
|
@@ -161,6 +213,51 @@ module Option
|
|
161
213
|
alias + join
|
162
214
|
alias << join
|
163
215
|
|
216
|
+
def map(predicate=nil, &block)
|
217
|
+
sequence(@content).map(predicate, &block).head_option
|
218
|
+
end
|
219
|
+
|
220
|
+
alias collect map
|
221
|
+
|
222
|
+
def select(predicate=nil, &block)
|
223
|
+
none
|
224
|
+
end
|
225
|
+
|
226
|
+
alias find_all select
|
227
|
+
alias filter select
|
228
|
+
|
229
|
+
def reject(predicate=nil, &block)
|
230
|
+
none
|
231
|
+
end
|
232
|
+
|
233
|
+
alias unfilter reject
|
234
|
+
|
235
|
+
def grep(pattern)
|
236
|
+
none
|
237
|
+
end
|
238
|
+
|
239
|
+
def drop(n)
|
240
|
+
none
|
241
|
+
end
|
242
|
+
|
243
|
+
def drop_while(&block)
|
244
|
+
none
|
245
|
+
end
|
246
|
+
|
247
|
+
def take(n)
|
248
|
+
none
|
249
|
+
end
|
250
|
+
|
251
|
+
def take_while(&block)
|
252
|
+
none
|
253
|
+
end
|
254
|
+
|
255
|
+
def flat_map(&block)
|
256
|
+
none
|
257
|
+
end
|
258
|
+
|
259
|
+
alias collect_concat flat_map
|
260
|
+
|
164
261
|
protected
|
165
262
|
|
166
263
|
def state
|
data/lib/sequence.rb
CHANGED
@@ -349,6 +349,10 @@ module Sequences
|
|
349
349
|
end)
|
350
350
|
end
|
351
351
|
|
352
|
+
def to_map(symbolize=true)
|
353
|
+
Maps.merge(to_maps(symbolize))
|
354
|
+
end
|
355
|
+
|
352
356
|
def from_pairs
|
353
357
|
Sequence.new(Sequence::Generator.new do |g|
|
354
358
|
self.entries.map { |e| Type.check(e, Pair::Pair); [e.key, e.value] }.flatten.each { |i| g.yield i }
|
@@ -460,6 +464,17 @@ module Sequences
|
|
460
464
|
blank?(sequence[index]) ? raise(exception, message) : sequence[index]
|
461
465
|
end
|
462
466
|
|
467
|
+
|
468
|
+
def get_by(pair_key)
|
469
|
+
item = self.filter{|e| e.first == pair_key}.head_option
|
470
|
+
item.is_some? ? item.get.value : none
|
471
|
+
end
|
472
|
+
|
473
|
+
def into_hash
|
474
|
+
raise(Exception.new, 'The sequence must contain pairs') unless self.head.kind_of?(Pair::Pair)
|
475
|
+
Maps.merge(self.map{|p| {p.key => p.value}})
|
476
|
+
end
|
477
|
+
|
463
478
|
def drop_nil
|
464
479
|
Sequence.new(Sequence::Generator.new do |g|
|
465
480
|
self.reject { |e| e.nil? }.each { |i| g.yield i }
|
data/spec/option_spec.rb
CHANGED
@@ -115,6 +115,56 @@ describe 'Option' do
|
|
115
115
|
expect(option(1).is_some?).to be(true)
|
116
116
|
end
|
117
117
|
|
118
|
+
it 'should support select' do
|
119
|
+
expect(option(2).filter(even)).to eq(some(2))
|
120
|
+
expect(option(2).find_all(even)).to eq(some(2))
|
121
|
+
expect(option(2).select(even)).to eq(some(2))
|
122
|
+
expect(option(2).filter(&:even?)).to eq(some(2))
|
123
|
+
expect(option(2).filter{|v| v == 2}).to eq(some(2))
|
124
|
+
expect(option(nil).filter(even)).to eq(none)
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should support map' do
|
128
|
+
expect(option(1).map(as_string)).to eq(some('1'))
|
129
|
+
expect(option(1).collect(as_string)).to eq(some('1'))
|
130
|
+
expect(option(sequence(1, 2, 3)).map { |s| s.entries }).to eq(some([1, 2, 3]))
|
131
|
+
expect(option(nil).map(as_string)).to eq(none)
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should support reject' do
|
135
|
+
expect(option(2).reject(odd)).to eq(some(2))
|
136
|
+
expect(option(2).unfilter(odd)).to eq(some(2))
|
137
|
+
expect(option(2).reject(&:odd?)).to eq(some(2))
|
138
|
+
expect(option(2).reject{|v| v == 1}).to eq(some(2))
|
139
|
+
expect(option(nil).reject(odd)).to eq(none)
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should support grep' do
|
143
|
+
expect(option('apple').grep(/p/)).to eq(some('apple'))
|
144
|
+
expect(option(nil).grep(/p/)).to eq(none)
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'should support drop' do
|
148
|
+
expect(option(1).drop(1)).to eq(none)
|
149
|
+
expect(option(nil).drop(1)).to eq(none)
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should support drop_while' do
|
153
|
+
expect(option(1).drop_while { |n| n < 5 }).to eq(none)
|
154
|
+
expect(option(nil).drop_while { |n| n < 5 }).to eq(none)
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'should support take' do
|
158
|
+
expect(option(1).take(2)).to eq(some(1))
|
159
|
+
expect(option(nil).take(2)).to eq(none)
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'should support take_while' do
|
163
|
+
expect(option(nil).take_while { |n| n < 5 }).to eq(none)
|
164
|
+
end
|
118
165
|
|
166
|
+
it 'should support flat_map' do
|
167
|
+
expect(option(nil).flat_map{|v| v.first}).to eq(none)
|
168
|
+
end
|
119
169
|
|
120
170
|
end
|
data/spec/sequence_spec.rb
CHANGED
@@ -212,6 +212,14 @@ describe 'Sequence' do
|
|
212
212
|
expect(sequence(5,2,3,1,4).sorting).to eq(sequence(1,2,3,4,5))
|
213
213
|
end
|
214
214
|
|
215
|
+
it 'should convert sequence to a map' do
|
216
|
+
expect(sequence(oops('apple',1,'pear',2)).to_map).to eq({apple:1,pear:2})
|
217
|
+
end
|
218
|
+
|
219
|
+
def oops(*values)
|
220
|
+
values
|
221
|
+
end
|
222
|
+
|
215
223
|
|
216
224
|
|
217
225
|
end
|
data/totally_lazy.gemspec
CHANGED