scaruby 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/scaruby/concurrent.rb +46 -46
- data/lib/scaruby/core_ext/enumerable.rb +1 -1
- data/lib/scaruby/core_ext/hash.rb +1 -1
- data/lib/scaruby/io.rb +62 -62
- data/lib/scaruby/map.rb +141 -141
- data/lib/scaruby/option.rb +46 -38
- data/lib/scaruby/seq.rb +395 -395
- data/lib/scaruby/version.rb +1 -1
- data/lib/scaruby.rb +59 -58
- data/scaruby.gemspec +23 -23
- data/spec/scaruby/converter_spec.rb +27 -27
- data/spec/scaruby/core_ext/enumerable_spec.rb +288 -288
- data/spec/scaruby/core_ext/hash_spec.rb +92 -92
- data/spec/scaruby/io_spec.rb +56 -56
- data/spec/scaruby/map_spec.rb +117 -117
- data/spec/scaruby/option_spec.rb +50 -43
- data/spec/scaruby/seq_spec.rb +301 -301
- data/spec/scaruby_spec.rb +32 -32
- metadata +19 -27
data/spec/scaruby/seq_spec.rb
CHANGED
@@ -1,301 +1,301 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
require 'scaruby'
|
4
|
-
|
5
|
-
describe Seq do
|
6
|
-
|
7
|
-
one_to_five = 1.upto 5
|
8
|
-
one_to_five_shuffled = one_to_five.sort_by {rand}
|
9
|
-
|
10
|
-
it 'does not accept invalid args' do
|
11
|
-
begin
|
12
|
-
Seq.new('aaaa').should eq(nil)
|
13
|
-
raise 'Expected exception did not be raised!'
|
14
|
-
rescue AssertionError
|
15
|
-
end
|
16
|
-
begin
|
17
|
-
Seq.new(12345).should eq(nil)
|
18
|
-
raise 'Expected exception did not be raised!'
|
19
|
-
rescue AssertionError
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# as a sub type of Enumerable
|
24
|
-
it 'has #each' do
|
25
|
-
expected = 0
|
26
|
-
returned = Seq.new([0,1,2,3]).each do |e|
|
27
|
-
e.should eq(expected)
|
28
|
-
expected += 1
|
29
|
-
end
|
30
|
-
returned.should eq(nil)
|
31
|
-
end
|
32
|
-
it 'has #all?' do
|
33
|
-
Seq.new([1,2,3]).all? {|e| e < 4 }.should eq(true)
|
34
|
-
Seq.new([1,2,3]).all? {|e| e > 2 }.should eq(false)
|
35
|
-
end
|
36
|
-
|
37
|
-
# defined
|
38
|
-
it 'has #to_a' do
|
39
|
-
Seq.new(one_to_five).to_a.should eq([1,2,3,4,5])
|
40
|
-
end
|
41
|
-
it 'has #corresponds' do
|
42
|
-
Seq.new([1,2,3]).corresponds([1,2,3]) {|a,b| a == b }.should eq(true)
|
43
|
-
Seq.new([1,2,3]).corresponds([3,1,2]) {|a,b| a == b }.should eq(false)
|
44
|
-
end
|
45
|
-
it 'has #count' do
|
46
|
-
Seq.new(one_to_five).count {|i| i > 2 }.should eq(3)
|
47
|
-
end
|
48
|
-
it 'has #diff' do
|
49
|
-
Seq.new([1,2,3]).diff([2,3,4]).to_a.should eq([1])
|
50
|
-
end
|
51
|
-
it 'has #distinct' do
|
52
|
-
Seq.new([1,2,4,1,3,3,3,2,4,1]).distinct.to_a.should eq([1,2,4,3])
|
53
|
-
end
|
54
|
-
it 'has #drop' do
|
55
|
-
Seq.new(one_to_five).drop(3).to_a.should eq([4,5])
|
56
|
-
Seq.new(one_to_five).drop(7).to_a.should eq([])
|
57
|
-
end
|
58
|
-
it 'has #drop_right' do
|
59
|
-
Seq.new(one_to_five).drop_right(3).to_a.should eq([1,2])
|
60
|
-
Seq.new(one_to_five).drop_right(7).to_a.should eq([])
|
61
|
-
end
|
62
|
-
it 'has #drop_while' do
|
63
|
-
Seq.new([5,3,2,4,1]).drop_while {|e| e > 2 }.to_a.should eq([2,4,1])
|
64
|
-
end
|
65
|
-
it 'has #ends_with' do
|
66
|
-
Seq.new([1,2,3]).ends_with([1,2]).should eq(false)
|
67
|
-
Seq.new([1,2,3]).ends_with([1,2,3]).should eq(true)
|
68
|
-
Seq.new([1,2,3]).ends_with([1,1,2,3]).should eq(false)
|
69
|
-
Seq.new([1,2,3]).ends_with([2,3]).should eq(true)
|
70
|
-
Seq.new([1,2,3]).ends_with([3]).should eq(true)
|
71
|
-
end
|
72
|
-
it 'has #exists' do
|
73
|
-
Seq.new([1,2,3]).exists {|i| i < 2 }.should eq(true)
|
74
|
-
Seq.new([1,2,3]).exists {|i| i < 4 }.should eq(true)
|
75
|
-
Seq.new([2,3,4]).exists {|i| i > 4 }.should eq(false)
|
76
|
-
end
|
77
|
-
it 'has #filter' do
|
78
|
-
Seq.new(one_to_five).filter {|i| i > 3 }.to_a.should eq([4,5])
|
79
|
-
Seq.new(one_to_five).filter {|i| i > 10 }.to_a.should eq([])
|
80
|
-
end
|
81
|
-
it 'has #filter_not' do
|
82
|
-
Seq.new(one_to_five).filter_not {|i| i > 3 }.to_a.should eq([1,2,3])
|
83
|
-
Seq.new(one_to_five).filter_not {|i| i > 10 }.to_a.should eq([1,2,3,4,5])
|
84
|
-
end
|
85
|
-
it 'has #find' do
|
86
|
-
some = Seq.new(one_to_five).find {|i| i < 3 }
|
87
|
-
some.get.should eq(1)
|
88
|
-
none = Seq.new(one_to_five).find {|i| i > 10 }
|
89
|
-
none.is_defined.should eq(false)
|
90
|
-
end
|
91
|
-
it 'has #flat_map and it works with nested arrays' do
|
92
|
-
Seq.new([[1,2],[3,4],[5]]).flat_map {|i| i }.to_a.should eq([1,2,3,4,5])
|
93
|
-
end
|
94
|
-
it 'has #flat_map and it works with Option elements' do
|
95
|
-
Seq.new([1,2,nil,3]).flat_map {|i| Option.new(i) }.to_a.should eq([1,2,3])
|
96
|
-
end
|
97
|
-
it 'has #fold_left' do
|
98
|
-
input = [1,2,3]
|
99
|
-
expected = [1,2,3]
|
100
|
-
idx = 0
|
101
|
-
Seq.new(input).fold_left(0) {|z,x|
|
102
|
-
x.should eq(expected[idx])
|
103
|
-
idx += 1
|
104
|
-
z + x
|
105
|
-
}.should eq(6)
|
106
|
-
end
|
107
|
-
it 'has #fold_right' do
|
108
|
-
input = [1,2,3]
|
109
|
-
expected = [3,2,1]
|
110
|
-
idx = 0
|
111
|
-
Seq.new(input).fold_right(0) {|z,x|
|
112
|
-
x.should eq(expected[idx])
|
113
|
-
idx += 1
|
114
|
-
z + x
|
115
|
-
}.should eq(6)
|
116
|
-
end
|
117
|
-
it 'has #flatten' do
|
118
|
-
Seq.new([[1,2],[3,4],[5]]).flatten.to_a.should eq([1,2,3,4,5])
|
119
|
-
Seq.new(
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
).flatten.to_a.should eq([1,2,3])
|
125
|
-
end
|
126
|
-
it 'has #forall' do
|
127
|
-
Seq.new([1,2,3]).forall {|i| i > 0 }.should eq(true)
|
128
|
-
Seq.new([1,2,3]).forall {|i| i > 1 }.should eq(false)
|
129
|
-
end
|
130
|
-
it 'has #foreach' do
|
131
|
-
count = 0
|
132
|
-
returned = Seq.new([1,2,3]).foreach do |i|
|
133
|
-
count += 1
|
134
|
-
end
|
135
|
-
count.should eq(3)
|
136
|
-
returned.should eq(nil)
|
137
|
-
end
|
138
|
-
it 'has #group_by' do
|
139
|
-
expected = {3=>[3,3,3], 1=>[1,1,1], 2=>[2,2]}
|
140
|
-
Seq.new([1,1,1,2,3,2,3,3]).group_by {|i| i }.to_hash.should eq(expected)
|
141
|
-
end
|
142
|
-
it 'has #head' do
|
143
|
-
Seq.new(one_to_five).head.should eq(1)
|
144
|
-
end
|
145
|
-
it 'has #head_option and it works with Some' do
|
146
|
-
some = Seq.new(one_to_five).head_option
|
147
|
-
some.get_or_else(999).should eq(1)
|
148
|
-
end
|
149
|
-
it 'has #head_option and it works with None' do
|
150
|
-
none = Seq.new([]).head_option
|
151
|
-
none.get_or_else(999).should eq(999)
|
152
|
-
end
|
153
|
-
it 'has #indices' do
|
154
|
-
Seq.new([1,2,3]).indices.to_a.should eq([0,1,2])
|
155
|
-
end
|
156
|
-
it 'has #init' do
|
157
|
-
Seq.new([1,2,3]).init.to_a.should eq([1,2])
|
158
|
-
end
|
159
|
-
it 'has #intersect' do
|
160
|
-
Seq.new([1,2,3]).intersect([2,3,4]).to_a.should eq([2,3])
|
161
|
-
end
|
162
|
-
it 'has #is_empty' do
|
163
|
-
Seq.new([1,2,3]).is_empty.should eq(false)
|
164
|
-
Seq.new([nil]).is_empty.should eq(false)
|
165
|
-
Seq.new([]).is_empty.should eq(true)
|
166
|
-
Seq.new(nil).is_empty.should eq(true)
|
167
|
-
end
|
168
|
-
it 'has #last' do
|
169
|
-
Seq.new([1,2,3]).last.should eq(3)
|
170
|
-
end
|
171
|
-
it 'has #last_option' do
|
172
|
-
some = Seq.new([1,2,3]).last_option
|
173
|
-
some.get.should eq(3)
|
174
|
-
none = Seq.new([]).last_option
|
175
|
-
none.is_defined.should eq(false)
|
176
|
-
end
|
177
|
-
it 'has #lift' do
|
178
|
-
seq_lift = Seq.new([1,2,3]).lift
|
179
|
-
seq_lift.apply(0).get.should eq(1)
|
180
|
-
seq_lift.apply(1).get.should eq(2)
|
181
|
-
seq_lift.apply(2).get.should eq(3)
|
182
|
-
seq_lift.apply(3).is_defined.should eq(false)
|
183
|
-
seq_lift.call(0).get.should eq(1)
|
184
|
-
seq_lift.call(1).get.should eq(2)
|
185
|
-
seq_lift.call(2).get.should eq(3)
|
186
|
-
seq_lift.call(3).is_defined.should eq(false)
|
187
|
-
end
|
188
|
-
it 'has #map' do
|
189
|
-
Seq.new([1,2,3]).map {|i| i + i }.to_a.should eq([2,4,6])
|
190
|
-
end
|
191
|
-
it 'has #max' do
|
192
|
-
Seq.new(one_to_five_shuffled).max.should eq(5)
|
193
|
-
end
|
194
|
-
it 'has #min' do
|
195
|
-
Seq.new(one_to_five_shuffled).min.should eq(1)
|
196
|
-
end
|
197
|
-
it 'has #mk_string' do
|
198
|
-
Seq.new(one_to_five).mk_string.should eq('12345')
|
199
|
-
Seq.new(one_to_five).mk_string(',').should eq('1,2,3,4,5')
|
200
|
-
Seq.new(one_to_five).mk_string('^',',','$').should eq('^1,2,3,4,5$')
|
201
|
-
begin
|
202
|
-
Seq.new(one_to_five).mk_string('a','b').should eq(nil)
|
203
|
-
rescue ArgumentError
|
204
|
-
end
|
205
|
-
Seq.new(one_to_five).mk_string('^',',','$','zzz').should eq('^1,2,3,4,5$')
|
206
|
-
end
|
207
|
-
it 'has #non_empty' do
|
208
|
-
Seq.new(one_to_five).non_empty.should eq(true)
|
209
|
-
Seq.new([]).non_empty.should eq(false)
|
210
|
-
Seq.new(nil).non_empty.should eq(false)
|
211
|
-
end
|
212
|
-
it 'has #partition' do
|
213
|
-
Seq.new([5,2,3,1,4,2,3]).partition {|i| i < 3 }.to_a.should eq([[2,1,2],[5,3,4,3]])
|
214
|
-
end
|
215
|
-
it 'has #patch' do
|
216
|
-
Seq.new([5,2,3,1,4,2,3]).patch(3,[111,222],3).to_a.should eq([5,2,3,111,222,3])
|
217
|
-
end
|
218
|
-
it 'has #reverse' do
|
219
|
-
Seq.new([1,2,3]).reverse.to_a.should eq([3,2,1])
|
220
|
-
end
|
221
|
-
it 'has #reverse_map' do
|
222
|
-
Seq.new([1,2,3]).reverse_map {|i| i + i }.to_a.should eq([6,4,2])
|
223
|
-
end
|
224
|
-
it 'has #same_elements' do
|
225
|
-
Seq.new([1,2,3]).same_elements([1,2,3]).should eq(true)
|
226
|
-
Seq.new([1,2,3]).same_elements([1,3,2]).should eq(false)
|
227
|
-
Seq.new([1,2,3]).same_elements([1,2]).should eq(false)
|
228
|
-
end
|
229
|
-
it 'has #scan_left' do
|
230
|
-
Seq.new([1,2,3]).scan_left(1) {|a,b| a + b }.to_a.should eq([1,2,4,7])
|
231
|
-
end
|
232
|
-
it 'has #scan_right' do
|
233
|
-
Seq.new([1,2,3]).scan_right(1) {|a,b| a + b }.to_a.should eq([7,6,4,1])
|
234
|
-
end
|
235
|
-
it 'has #slice' do
|
236
|
-
Seq.new([1,2,3,4,5]).slice(1,1).to_a.should eq([])
|
237
|
-
Seq.new([1,2,3,4,5]).slice(1,2).to_a.should eq([2])
|
238
|
-
Seq.new([1,2,3,4,5]).slice(1,3).to_a.should eq([2,3])
|
239
|
-
Seq.new([1,2,3,4,5]).slice(1,4).to_a.should eq([2,3,4])
|
240
|
-
end
|
241
|
-
it 'has #sliding' do
|
242
|
-
Seq.new([1,2,3,4,5]).sliding(2).to_a.should eq([[1,2],[2,3],[3,4],[4,5]])
|
243
|
-
end
|
244
|
-
it 'has #sort_with' do
|
245
|
-
Seq.new([1,3,2,4,5]).sort_with {|a,b| b <=> a }.to_a.should eq([5,4,3,2,1])
|
246
|
-
end
|
247
|
-
it 'has #span' do
|
248
|
-
Seq.new([1,2,3,2,1]).span {|i| i < 3 }.to_a.should eq([[1,2],[3,2,1]])
|
249
|
-
end
|
250
|
-
it 'has #split_at' do
|
251
|
-
Seq.new([1,2,3,2,1]).split_at(3).to_a.should eq([[1,2,3],[2,1]])
|
252
|
-
end
|
253
|
-
it 'has #starts_with' do
|
254
|
-
Seq.new([1,2,3]).starts_with([1]).should eq(true)
|
255
|
-
Seq.new([1,2,3]).starts_with([1,2]).should eq(true)
|
256
|
-
Seq.new([1,2,3]).starts_with([1,2,3]).should eq(true)
|
257
|
-
Seq.new([1,2,3]).starts_with([1,2,3,4]).should eq(false)
|
258
|
-
Seq.new([1,2,3]).starts_with([2,3]).should eq(false)
|
259
|
-
Seq.new([1,2,3]).starts_with([4,1,2,3]).should eq(false)
|
260
|
-
end
|
261
|
-
it 'has #sum' do
|
262
|
-
Seq.new([1,2,3]).sum.should eq(6)
|
263
|
-
end
|
264
|
-
it 'has #tail' do
|
265
|
-
Seq.new([1,2,3]).tail.to_a.should eq([2,3])
|
266
|
-
Seq.new([]).tail.to_a.should eq([])
|
267
|
-
end
|
268
|
-
it 'has #take' do
|
269
|
-
Seq.new([1,2,3]).take(0).to_a.should eq([])
|
270
|
-
Seq.new([1,2,3]).take(1).to_a.should eq([1])
|
271
|
-
Seq.new([1,2,3]).take(2).to_a.should eq([1,2])
|
272
|
-
Seq.new([1,2,3]).take(3).to_a.should eq([1,2,3])
|
273
|
-
Seq.new([1,2,3]).take(4).to_a.should eq([1,2,3])
|
274
|
-
end
|
275
|
-
it 'has #take_right' do
|
276
|
-
Seq.new([1,2,3]).take_right(0).to_a.should eq([])
|
277
|
-
Seq.new([1,2,3]).take_right(1).to_a.should eq([3])
|
278
|
-
Seq.new([1,2,3]).take_right(2).to_a.should eq([2,3])
|
279
|
-
Seq.new([1,2,3]).take_right(3).to_a.should eq([1,2,3])
|
280
|
-
Seq.new([1,2,3]).take_right(4).to_a.should eq([1,2,3])
|
281
|
-
end
|
282
|
-
it 'has #take_while' do
|
283
|
-
Seq.new([5,3,2,4,1]).take_while {|e| e > 2 }.to_a.should eq([5,3])
|
284
|
-
end
|
285
|
-
it 'has #union' do
|
286
|
-
Seq.new([1,2,3]).union([2,3,4]).to_a.should eq([1,2,3,2,3,4])
|
287
|
-
end
|
288
|
-
it 'has #updated' do
|
289
|
-
Seq.new([1,2,3]).updated(1,999).to_a.should eq([1,999,3])
|
290
|
-
end
|
291
|
-
it 'has #zip' do
|
292
|
-
Seq.new([1,2,3]).zip([2,3]).to_a.should eq([[1,2],[2,3]])
|
293
|
-
Seq.new([1,2,3]).zip([2,3,4]).to_a.should eq([[1,2],[2,3],[3,4]])
|
294
|
-
Seq.new([1,2,3]).zip([2,3,4,5]).to_a.should eq([[1,2],[2,3],[3,4]])
|
295
|
-
end
|
296
|
-
it 'has #zip_with_index' do
|
297
|
-
Seq.new([]).zip_with_index.to_a.should eq([])
|
298
|
-
Seq.new([1,2,3]).zip_with_index.to_a.should eq([[1,0],[2,1],[3,2]])
|
299
|
-
end
|
300
|
-
|
301
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'scaruby'
|
4
|
+
|
5
|
+
describe Seq do
|
6
|
+
|
7
|
+
one_to_five = 1.upto 5
|
8
|
+
one_to_five_shuffled = one_to_five.sort_by { rand }
|
9
|
+
|
10
|
+
it 'does not accept invalid args' do
|
11
|
+
begin
|
12
|
+
Seq.new('aaaa').should eq(nil)
|
13
|
+
raise 'Expected exception did not be raised!'
|
14
|
+
rescue AssertionError
|
15
|
+
end
|
16
|
+
begin
|
17
|
+
Seq.new(12345).should eq(nil)
|
18
|
+
raise 'Expected exception did not be raised!'
|
19
|
+
rescue AssertionError
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# as a sub type of Enumerable
|
24
|
+
it 'has #each' do
|
25
|
+
expected = 0
|
26
|
+
returned = Seq.new([0, 1, 2, 3]).each do |e|
|
27
|
+
e.should eq(expected)
|
28
|
+
expected += 1
|
29
|
+
end
|
30
|
+
returned.should eq(nil)
|
31
|
+
end
|
32
|
+
it 'has #all?' do
|
33
|
+
Seq.new([1, 2, 3]).all? { |e| e < 4 }.should eq(true)
|
34
|
+
Seq.new([1, 2, 3]).all? { |e| e > 2 }.should eq(false)
|
35
|
+
end
|
36
|
+
|
37
|
+
# defined
|
38
|
+
it 'has #to_a' do
|
39
|
+
Seq.new(one_to_five).to_a.should eq([1, 2, 3, 4, 5])
|
40
|
+
end
|
41
|
+
it 'has #corresponds' do
|
42
|
+
Seq.new([1, 2, 3]).corresponds([1, 2, 3]) { |a, b| a == b }.should eq(true)
|
43
|
+
Seq.new([1, 2, 3]).corresponds([3, 1, 2]) { |a, b| a == b }.should eq(false)
|
44
|
+
end
|
45
|
+
it 'has #count' do
|
46
|
+
Seq.new(one_to_five).count { |i| i > 2 }.should eq(3)
|
47
|
+
end
|
48
|
+
it 'has #diff' do
|
49
|
+
Seq.new([1, 2, 3]).diff([2, 3, 4]).to_a.should eq([1])
|
50
|
+
end
|
51
|
+
it 'has #distinct' do
|
52
|
+
Seq.new([1, 2, 4, 1, 3, 3, 3, 2, 4, 1]).distinct.to_a.should eq([1, 2, 4, 3])
|
53
|
+
end
|
54
|
+
it 'has #drop' do
|
55
|
+
Seq.new(one_to_five).drop(3).to_a.should eq([4, 5])
|
56
|
+
Seq.new(one_to_five).drop(7).to_a.should eq([])
|
57
|
+
end
|
58
|
+
it 'has #drop_right' do
|
59
|
+
Seq.new(one_to_five).drop_right(3).to_a.should eq([1, 2])
|
60
|
+
Seq.new(one_to_five).drop_right(7).to_a.should eq([])
|
61
|
+
end
|
62
|
+
it 'has #drop_while' do
|
63
|
+
Seq.new([5, 3, 2, 4, 1]).drop_while { |e| e > 2 }.to_a.should eq([2, 4, 1])
|
64
|
+
end
|
65
|
+
it 'has #ends_with' do
|
66
|
+
Seq.new([1, 2, 3]).ends_with([1, 2]).should eq(false)
|
67
|
+
Seq.new([1, 2, 3]).ends_with([1, 2, 3]).should eq(true)
|
68
|
+
Seq.new([1, 2, 3]).ends_with([1, 1, 2, 3]).should eq(false)
|
69
|
+
Seq.new([1, 2, 3]).ends_with([2, 3]).should eq(true)
|
70
|
+
Seq.new([1, 2, 3]).ends_with([3]).should eq(true)
|
71
|
+
end
|
72
|
+
it 'has #exists' do
|
73
|
+
Seq.new([1, 2, 3]).exists { |i| i < 2 }.should eq(true)
|
74
|
+
Seq.new([1, 2, 3]).exists { |i| i < 4 }.should eq(true)
|
75
|
+
Seq.new([2, 3, 4]).exists { |i| i > 4 }.should eq(false)
|
76
|
+
end
|
77
|
+
it 'has #filter' do
|
78
|
+
Seq.new(one_to_five).filter { |i| i > 3 }.to_a.should eq([4, 5])
|
79
|
+
Seq.new(one_to_five).filter { |i| i > 10 }.to_a.should eq([])
|
80
|
+
end
|
81
|
+
it 'has #filter_not' do
|
82
|
+
Seq.new(one_to_five).filter_not { |i| i > 3 }.to_a.should eq([1, 2, 3])
|
83
|
+
Seq.new(one_to_five).filter_not { |i| i > 10 }.to_a.should eq([1, 2, 3, 4, 5])
|
84
|
+
end
|
85
|
+
it 'has #find' do
|
86
|
+
some = Seq.new(one_to_five).find { |i| i < 3 }
|
87
|
+
some.get.should eq(1)
|
88
|
+
none = Seq.new(one_to_five).find { |i| i > 10 }
|
89
|
+
none.is_defined.should eq(false)
|
90
|
+
end
|
91
|
+
it 'has #flat_map and it works with nested arrays' do
|
92
|
+
Seq.new([[1, 2], [3, 4], [5]]).flat_map { |i| i }.to_a.should eq([1, 2, 3, 4, 5])
|
93
|
+
end
|
94
|
+
it 'has #flat_map and it works with Option elements' do
|
95
|
+
Seq.new([1, 2, nil, 3]).flat_map { |i| Option.new(i) }.to_a.should eq([1, 2, 3])
|
96
|
+
end
|
97
|
+
it 'has #fold_left' do
|
98
|
+
input = [1, 2, 3]
|
99
|
+
expected = [1, 2, 3]
|
100
|
+
idx = 0
|
101
|
+
Seq.new(input).fold_left(0) { |z, x|
|
102
|
+
x.should eq(expected[idx])
|
103
|
+
idx += 1
|
104
|
+
z + x
|
105
|
+
}.should eq(6)
|
106
|
+
end
|
107
|
+
it 'has #fold_right' do
|
108
|
+
input = [1, 2, 3]
|
109
|
+
expected = [3, 2, 1]
|
110
|
+
idx = 0
|
111
|
+
Seq.new(input).fold_right(0) { |z, x|
|
112
|
+
x.should eq(expected[idx])
|
113
|
+
idx += 1
|
114
|
+
z + x
|
115
|
+
}.should eq(6)
|
116
|
+
end
|
117
|
+
it 'has #flatten' do
|
118
|
+
Seq.new([[1, 2], [3, 4], [5]]).flatten.to_a.should eq([1, 2, 3, 4, 5])
|
119
|
+
Seq.new(
|
120
|
+
[Option.new(1),
|
121
|
+
Option.new(2),
|
122
|
+
Option.new(nil),
|
123
|
+
Option.new(3)]
|
124
|
+
).flatten.to_a.should eq([1, 2, 3])
|
125
|
+
end
|
126
|
+
it 'has #forall' do
|
127
|
+
Seq.new([1, 2, 3]).forall { |i| i > 0 }.should eq(true)
|
128
|
+
Seq.new([1, 2, 3]).forall { |i| i > 1 }.should eq(false)
|
129
|
+
end
|
130
|
+
it 'has #foreach' do
|
131
|
+
count = 0
|
132
|
+
returned = Seq.new([1, 2, 3]).foreach do |i|
|
133
|
+
count += 1
|
134
|
+
end
|
135
|
+
count.should eq(3)
|
136
|
+
returned.should eq(nil)
|
137
|
+
end
|
138
|
+
it 'has #group_by' do
|
139
|
+
expected = {3 => [3, 3, 3], 1 => [1, 1, 1], 2 => [2, 2]}
|
140
|
+
Seq.new([1, 1, 1, 2, 3, 2, 3, 3]).group_by { |i| i }.to_hash.should eq(expected)
|
141
|
+
end
|
142
|
+
it 'has #head' do
|
143
|
+
Seq.new(one_to_five).head.should eq(1)
|
144
|
+
end
|
145
|
+
it 'has #head_option and it works with Some' do
|
146
|
+
some = Seq.new(one_to_five).head_option
|
147
|
+
some.get_or_else(999).should eq(1)
|
148
|
+
end
|
149
|
+
it 'has #head_option and it works with None' do
|
150
|
+
none = Seq.new([]).head_option
|
151
|
+
none.get_or_else(999).should eq(999)
|
152
|
+
end
|
153
|
+
it 'has #indices' do
|
154
|
+
Seq.new([1, 2, 3]).indices.to_a.should eq([0, 1, 2])
|
155
|
+
end
|
156
|
+
it 'has #init' do
|
157
|
+
Seq.new([1, 2, 3]).init.to_a.should eq([1, 2])
|
158
|
+
end
|
159
|
+
it 'has #intersect' do
|
160
|
+
Seq.new([1, 2, 3]).intersect([2, 3, 4]).to_a.should eq([2, 3])
|
161
|
+
end
|
162
|
+
it 'has #is_empty' do
|
163
|
+
Seq.new([1, 2, 3]).is_empty.should eq(false)
|
164
|
+
Seq.new([nil]).is_empty.should eq(false)
|
165
|
+
Seq.new([]).is_empty.should eq(true)
|
166
|
+
Seq.new(nil).is_empty.should eq(true)
|
167
|
+
end
|
168
|
+
it 'has #last' do
|
169
|
+
Seq.new([1, 2, 3]).last.should eq(3)
|
170
|
+
end
|
171
|
+
it 'has #last_option' do
|
172
|
+
some = Seq.new([1, 2, 3]).last_option
|
173
|
+
some.get.should eq(3)
|
174
|
+
none = Seq.new([]).last_option
|
175
|
+
none.is_defined.should eq(false)
|
176
|
+
end
|
177
|
+
it 'has #lift' do
|
178
|
+
seq_lift = Seq.new([1, 2, 3]).lift
|
179
|
+
seq_lift.apply(0).get.should eq(1)
|
180
|
+
seq_lift.apply(1).get.should eq(2)
|
181
|
+
seq_lift.apply(2).get.should eq(3)
|
182
|
+
seq_lift.apply(3).is_defined.should eq(false)
|
183
|
+
seq_lift.call(0).get.should eq(1)
|
184
|
+
seq_lift.call(1).get.should eq(2)
|
185
|
+
seq_lift.call(2).get.should eq(3)
|
186
|
+
seq_lift.call(3).is_defined.should eq(false)
|
187
|
+
end
|
188
|
+
it 'has #map' do
|
189
|
+
Seq.new([1, 2, 3]).map { |i| i + i }.to_a.should eq([2, 4, 6])
|
190
|
+
end
|
191
|
+
it 'has #max' do
|
192
|
+
Seq.new(one_to_five_shuffled).max.should eq(5)
|
193
|
+
end
|
194
|
+
it 'has #min' do
|
195
|
+
Seq.new(one_to_five_shuffled).min.should eq(1)
|
196
|
+
end
|
197
|
+
it 'has #mk_string' do
|
198
|
+
Seq.new(one_to_five).mk_string.should eq('12345')
|
199
|
+
Seq.new(one_to_five).mk_string(',').should eq('1,2,3,4,5')
|
200
|
+
Seq.new(one_to_five).mk_string('^', ',', '$').should eq('^1,2,3,4,5$')
|
201
|
+
begin
|
202
|
+
Seq.new(one_to_five).mk_string('a', 'b').should eq(nil)
|
203
|
+
rescue ArgumentError
|
204
|
+
end
|
205
|
+
Seq.new(one_to_five).mk_string('^', ',', '$', 'zzz').should eq('^1,2,3,4,5$')
|
206
|
+
end
|
207
|
+
it 'has #non_empty' do
|
208
|
+
Seq.new(one_to_five).non_empty.should eq(true)
|
209
|
+
Seq.new([]).non_empty.should eq(false)
|
210
|
+
Seq.new(nil).non_empty.should eq(false)
|
211
|
+
end
|
212
|
+
it 'has #partition' do
|
213
|
+
Seq.new([5, 2, 3, 1, 4, 2, 3]).partition { |i| i < 3 }.to_a.should eq([[2, 1, 2], [5, 3, 4, 3]])
|
214
|
+
end
|
215
|
+
it 'has #patch' do
|
216
|
+
Seq.new([5, 2, 3, 1, 4, 2, 3]).patch(3, [111, 222], 3).to_a.should eq([5, 2, 3, 111, 222, 3])
|
217
|
+
end
|
218
|
+
it 'has #reverse' do
|
219
|
+
Seq.new([1, 2, 3]).reverse.to_a.should eq([3, 2, 1])
|
220
|
+
end
|
221
|
+
it 'has #reverse_map' do
|
222
|
+
Seq.new([1, 2, 3]).reverse_map { |i| i + i }.to_a.should eq([6, 4, 2])
|
223
|
+
end
|
224
|
+
it 'has #same_elements' do
|
225
|
+
Seq.new([1, 2, 3]).same_elements([1, 2, 3]).should eq(true)
|
226
|
+
Seq.new([1, 2, 3]).same_elements([1, 3, 2]).should eq(false)
|
227
|
+
Seq.new([1, 2, 3]).same_elements([1, 2]).should eq(false)
|
228
|
+
end
|
229
|
+
it 'has #scan_left' do
|
230
|
+
Seq.new([1, 2, 3]).scan_left(1) { |a, b| a + b }.to_a.should eq([1, 2, 4, 7])
|
231
|
+
end
|
232
|
+
it 'has #scan_right' do
|
233
|
+
Seq.new([1, 2, 3]).scan_right(1) { |a, b| a + b }.to_a.should eq([7, 6, 4, 1])
|
234
|
+
end
|
235
|
+
it 'has #slice' do
|
236
|
+
Seq.new([1, 2, 3, 4, 5]).slice(1, 1).to_a.should eq([])
|
237
|
+
Seq.new([1, 2, 3, 4, 5]).slice(1, 2).to_a.should eq([2])
|
238
|
+
Seq.new([1, 2, 3, 4, 5]).slice(1, 3).to_a.should eq([2, 3])
|
239
|
+
Seq.new([1, 2, 3, 4, 5]).slice(1, 4).to_a.should eq([2, 3, 4])
|
240
|
+
end
|
241
|
+
it 'has #sliding' do
|
242
|
+
Seq.new([1, 2, 3, 4, 5]).sliding(2).to_a.should eq([[1, 2], [2, 3], [3, 4], [4, 5]])
|
243
|
+
end
|
244
|
+
it 'has #sort_with' do
|
245
|
+
Seq.new([1, 3, 2, 4, 5]).sort_with { |a, b| b <=> a }.to_a.should eq([5, 4, 3, 2, 1])
|
246
|
+
end
|
247
|
+
it 'has #span' do
|
248
|
+
Seq.new([1, 2, 3, 2, 1]).span { |i| i < 3 }.to_a.should eq([[1, 2], [3, 2, 1]])
|
249
|
+
end
|
250
|
+
it 'has #split_at' do
|
251
|
+
Seq.new([1, 2, 3, 2, 1]).split_at(3).to_a.should eq([[1, 2, 3], [2, 1]])
|
252
|
+
end
|
253
|
+
it 'has #starts_with' do
|
254
|
+
Seq.new([1, 2, 3]).starts_with([1]).should eq(true)
|
255
|
+
Seq.new([1, 2, 3]).starts_with([1, 2]).should eq(true)
|
256
|
+
Seq.new([1, 2, 3]).starts_with([1, 2, 3]).should eq(true)
|
257
|
+
Seq.new([1, 2, 3]).starts_with([1, 2, 3, 4]).should eq(false)
|
258
|
+
Seq.new([1, 2, 3]).starts_with([2, 3]).should eq(false)
|
259
|
+
Seq.new([1, 2, 3]).starts_with([4, 1, 2, 3]).should eq(false)
|
260
|
+
end
|
261
|
+
it 'has #sum' do
|
262
|
+
Seq.new([1, 2, 3]).sum.should eq(6)
|
263
|
+
end
|
264
|
+
it 'has #tail' do
|
265
|
+
Seq.new([1, 2, 3]).tail.to_a.should eq([2, 3])
|
266
|
+
Seq.new([]).tail.to_a.should eq([])
|
267
|
+
end
|
268
|
+
it 'has #take' do
|
269
|
+
Seq.new([1, 2, 3]).take(0).to_a.should eq([])
|
270
|
+
Seq.new([1, 2, 3]).take(1).to_a.should eq([1])
|
271
|
+
Seq.new([1, 2, 3]).take(2).to_a.should eq([1, 2])
|
272
|
+
Seq.new([1, 2, 3]).take(3).to_a.should eq([1, 2, 3])
|
273
|
+
Seq.new([1, 2, 3]).take(4).to_a.should eq([1, 2, 3])
|
274
|
+
end
|
275
|
+
it 'has #take_right' do
|
276
|
+
Seq.new([1, 2, 3]).take_right(0).to_a.should eq([])
|
277
|
+
Seq.new([1, 2, 3]).take_right(1).to_a.should eq([3])
|
278
|
+
Seq.new([1, 2, 3]).take_right(2).to_a.should eq([2, 3])
|
279
|
+
Seq.new([1, 2, 3]).take_right(3).to_a.should eq([1, 2, 3])
|
280
|
+
Seq.new([1, 2, 3]).take_right(4).to_a.should eq([1, 2, 3])
|
281
|
+
end
|
282
|
+
it 'has #take_while' do
|
283
|
+
Seq.new([5, 3, 2, 4, 1]).take_while { |e| e > 2 }.to_a.should eq([5, 3])
|
284
|
+
end
|
285
|
+
it 'has #union' do
|
286
|
+
Seq.new([1, 2, 3]).union([2, 3, 4]).to_a.should eq([1, 2, 3, 2, 3, 4])
|
287
|
+
end
|
288
|
+
it 'has #updated' do
|
289
|
+
Seq.new([1, 2, 3]).updated(1, 999).to_a.should eq([1, 999, 3])
|
290
|
+
end
|
291
|
+
it 'has #zip' do
|
292
|
+
Seq.new([1, 2, 3]).zip([2, 3]).to_a.should eq([[1, 2], [2, 3]])
|
293
|
+
Seq.new([1, 2, 3]).zip([2, 3, 4]).to_a.should eq([[1, 2], [2, 3], [3, 4]])
|
294
|
+
Seq.new([1, 2, 3]).zip([2, 3, 4, 5]).to_a.should eq([[1, 2], [2, 3], [3, 4]])
|
295
|
+
end
|
296
|
+
it 'has #zip_with_index' do
|
297
|
+
Seq.new([]).zip_with_index.to_a.should eq([])
|
298
|
+
Seq.new([1, 2, 3]).zip_with_index.to_a.should eq([[1, 0], [2, 1], [3, 2]])
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|