scaruby 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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/lib/scaruby/seq.rb
CHANGED
@@ -1,395 +1,395 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
module Scaruby
|
4
|
-
class Seq
|
5
|
-
include Enumerable
|
6
|
-
|
7
|
-
attr :array
|
8
|
-
|
9
|
-
def each(&block)
|
10
|
-
@array.each do |e|
|
11
|
-
yield e
|
12
|
-
end
|
13
|
-
nil
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.apply(enumerable)
|
17
|
-
Seq.new(enumerable)
|
18
|
-
end
|
19
|
-
|
20
|
-
def initialize(enumerable)
|
21
|
-
assert_type(enumerable, Enumerable)
|
22
|
-
@array = enumerable.to_a
|
23
|
-
end
|
24
|
-
|
25
|
-
def to_a
|
26
|
-
@array
|
27
|
-
end
|
28
|
-
|
29
|
-
def corresponds(that, &predicate)
|
30
|
-
@array.zip(that).inject(true) {|still_matched,e|
|
31
|
-
if !
|
32
|
-
false
|
33
|
-
elsif yield e[0], e[1]
|
34
|
-
true
|
35
|
-
else
|
36
|
-
false
|
37
|
-
end
|
38
|
-
}
|
39
|
-
end
|
40
|
-
|
41
|
-
def count(&predicate)
|
42
|
-
@array.count(&predicate)
|
43
|
-
end
|
44
|
-
|
45
|
-
def diff(that)
|
46
|
-
filter {|e| !
|
47
|
-
end
|
48
|
-
|
49
|
-
def distinct
|
50
|
-
Seq.new(@array.uniq)
|
51
|
-
end
|
52
|
-
|
53
|
-
def drop(n)
|
54
|
-
Seq.new(@array.drop(n))
|
55
|
-
end
|
56
|
-
|
57
|
-
def drop_right(n)
|
58
|
-
Seq.new(@array.reverse.drop(n).reverse)
|
59
|
-
end
|
60
|
-
|
61
|
-
def drop_while(&predicate)
|
62
|
-
Seq.new(@array.inject([false,[]]) {|passed,x|
|
63
|
-
no_need_to_yield, result = passed[0], passed[1]
|
64
|
-
if no_need_to_yield
|
65
|
-
[true, result.push(x)]
|
66
|
-
elsif yield x
|
67
|
-
passed
|
68
|
-
else
|
69
|
-
[true, result.push(x)]
|
70
|
-
end
|
71
|
-
}[1])
|
72
|
-
end
|
73
|
-
|
74
|
-
def ends_with(that)
|
75
|
-
if that.nil? || that.length > @array.length
|
76
|
-
return false
|
77
|
-
end
|
78
|
-
this_end = @array.reverse.take(that.length).reverse
|
79
|
-
this_end.zip(that).inject(true) {|all_matched,a|
|
80
|
-
if !
|
81
|
-
false
|
82
|
-
elsif a.length != 2
|
83
|
-
false
|
84
|
-
else
|
85
|
-
a[0] == a[1]
|
86
|
-
end
|
87
|
-
}
|
88
|
-
end
|
89
|
-
|
90
|
-
def exists(&predicate)
|
91
|
-
@array.any?(&predicate)
|
92
|
-
end
|
93
|
-
|
94
|
-
def filter(&predicate)
|
95
|
-
Seq.new(@array.select {|e| yield e })
|
96
|
-
end
|
97
|
-
|
98
|
-
def filter_not(&predicate)
|
99
|
-
Seq.new(@array.select {|e| !
|
100
|
-
end
|
101
|
-
|
102
|
-
def find(&predicate)
|
103
|
-
Option.new(@array.find(&predicate))
|
104
|
-
end
|
105
|
-
|
106
|
-
def flat_map(&block)
|
107
|
-
Seq.new(@array.inject([]) {|z,x|
|
108
|
-
applied = yield x
|
109
|
-
if applied.is_a?(Enumerable)
|
110
|
-
applied.inject(z) {|z,elm| z.push(elm) }
|
111
|
-
elsif applied.is_a?(Seq)
|
112
|
-
applied.to_a.inject(z) {|z,elm| z.push(elm) }
|
113
|
-
elsif applied.is_a?(Option)
|
114
|
-
applied.is_defined ? z.push(applied.get) : z
|
115
|
-
else
|
116
|
-
z.push(applied)
|
117
|
-
end
|
118
|
-
})
|
119
|
-
end
|
120
|
-
|
121
|
-
def flatten
|
122
|
-
Seq.new(@array.inject([]) {|z,x|
|
123
|
-
if x.is_a?(Enumerable)
|
124
|
-
x.inject(z) {|z,elm| z.push(elm) }
|
125
|
-
elsif x.is_a?(Option)
|
126
|
-
x.is_defined ? z.push(x.get) : z
|
127
|
-
else
|
128
|
-
z.push(x)
|
129
|
-
end
|
130
|
-
})
|
131
|
-
end
|
132
|
-
|
133
|
-
def fold_left(z, &block)
|
134
|
-
@array.inject(z) {|z,x| yield z, x }
|
135
|
-
end
|
136
|
-
|
137
|
-
def fold_right(z, &block)
|
138
|
-
@array.reverse.inject(z) {|z,x| yield z, x }
|
139
|
-
end
|
140
|
-
|
141
|
-
def forall(&predicate)
|
142
|
-
@array.all?(&predicate)
|
143
|
-
end
|
144
|
-
|
145
|
-
def foreach(&block)
|
146
|
-
@array.each do |e|
|
147
|
-
yield e
|
148
|
-
end
|
149
|
-
nil
|
150
|
-
end
|
151
|
-
|
152
|
-
def group_by(&block)
|
153
|
-
Map.new(@array.inject({}) {|z,e|
|
154
|
-
if z[e].nil?
|
155
|
-
z[e] = [e]
|
156
|
-
else
|
157
|
-
z[e] = z[e].push(e)
|
158
|
-
end
|
159
|
-
z
|
160
|
-
})
|
161
|
-
end
|
162
|
-
|
163
|
-
def head
|
164
|
-
@array.first
|
165
|
-
end
|
166
|
-
|
167
|
-
def head_option
|
168
|
-
Option.new(@array.first)
|
169
|
-
end
|
170
|
-
|
171
|
-
def indices
|
172
|
-
Seq.new(0.upto(@array.length-1))
|
173
|
-
end
|
174
|
-
|
175
|
-
def init
|
176
|
-
Seq.new(@array.take(@array.length - 1))
|
177
|
-
end
|
178
|
-
|
179
|
-
def intersect(that)
|
180
|
-
Seq.new(@array.inject([]) {|z,x|
|
181
|
-
that.include?(x) ? z.push(x) : z
|
182
|
-
})
|
183
|
-
end
|
184
|
-
|
185
|
-
def is_empty
|
186
|
-
@array.nil? || @array.empty?
|
187
|
-
end
|
188
|
-
|
189
|
-
def last
|
190
|
-
@array.last
|
191
|
-
end
|
192
|
-
|
193
|
-
def last_option
|
194
|
-
Option.new(@array.last)
|
195
|
-
end
|
196
|
-
|
197
|
-
def length
|
198
|
-
@array.length
|
199
|
-
end
|
200
|
-
|
201
|
-
def lift
|
202
|
-
AppliableProc.new {|i| Option.new(@array[i]) }
|
203
|
-
end
|
204
|
-
|
205
|
-
def map(&block)
|
206
|
-
#Seq.new(@array.collect {|e| yield e })
|
207
|
-
Seq.new(@array.map {|e| yield e })
|
208
|
-
end
|
209
|
-
|
210
|
-
def max
|
211
|
-
@array.max
|
212
|
-
end
|
213
|
-
|
214
|
-
def min
|
215
|
-
@array.min
|
216
|
-
end
|
217
|
-
|
218
|
-
def mk_string(*args)
|
219
|
-
case args.length
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
end
|
229
|
-
start_part + @array.join(sep) + end_part
|
230
|
-
end
|
231
|
-
|
232
|
-
def non_empty
|
233
|
-
!
|
234
|
-
end
|
235
|
-
|
236
|
-
def partition(&predicate)
|
237
|
-
Seq.new(@array.chunk(&predicate).inject([[],[]]) {|z, matched_and_elm|
|
238
|
-
matched, elm = matched_and_elm[0], matched_and_elm[1].first
|
239
|
-
if matched
|
240
|
-
[z[0].push(elm), z[1]]
|
241
|
-
else
|
242
|
-
[z[0],z[1].push(elm)]
|
243
|
-
end
|
244
|
-
})
|
245
|
-
end
|
246
|
-
|
247
|
-
def patch(from, patch, replaced)
|
248
|
-
#Seq.new(@array.take(from).concat(patch).concat(@array.drop(from).drop(replaced)))
|
249
|
-
Seq.new(@array.take(from) + patch + @array.drop(from).drop(replaced))
|
250
|
-
end
|
251
|
-
|
252
|
-
def reverse
|
253
|
-
Seq.new(@array.reverse)
|
254
|
-
end
|
255
|
-
|
256
|
-
def reverse_map(&block)
|
257
|
-
#Seq.new(@array.reverse.collect {|e| yield e })
|
258
|
-
Seq.new(@array.reverse.map {|e| yield e })
|
259
|
-
end
|
260
|
-
|
261
|
-
def same_elements(that)
|
262
|
-
@array.zip(that).inject(true) {|still_same,a|
|
263
|
-
if !
|
264
|
-
false
|
265
|
-
elsif a.length != 2
|
266
|
-
false
|
267
|
-
else
|
268
|
-
a[0] == a[1]
|
269
|
-
end
|
270
|
-
}
|
271
|
-
end
|
272
|
-
|
273
|
-
def scan_left(n, &block)
|
274
|
-
Seq.new(@array.inject([n,[n]]) {|last_and_array,x|
|
275
|
-
last, array = last_and_array[0], last_and_array[1]
|
276
|
-
applied = yield last, x
|
277
|
-
[applied, array.push(applied)]
|
278
|
-
}[1])
|
279
|
-
end
|
280
|
-
|
281
|
-
def scan_right(n, &block)
|
282
|
-
Seq.new(@array.reverse.inject([n,[n]]) {|last_and_array,x|
|
283
|
-
last, array = last_and_array[0], last_and_array[1]
|
284
|
-
applied = yield last, x
|
285
|
-
[applied, array.push(applied)]
|
286
|
-
}[1].reverse)
|
287
|
-
end
|
288
|
-
|
289
|
-
def size
|
290
|
-
@array.length
|
291
|
-
end
|
292
|
-
|
293
|
-
def slice(from, until_n)
|
294
|
-
Seq.new(@array.drop(from).take(until_n - from))
|
295
|
-
end
|
296
|
-
|
297
|
-
def sliding(len)
|
298
|
-
result = []
|
299
|
-
@array.each_with_index do |e,idx|
|
300
|
-
if idx < (@array.length - len + 1)
|
301
|
-
result.push(@array.slice(idx, len))
|
302
|
-
end
|
303
|
-
end
|
304
|
-
Seq.new(result)
|
305
|
-
end
|
306
|
-
|
307
|
-
def sort_with(&predicate)
|
308
|
-
@array.sort(&predicate)
|
309
|
-
end
|
310
|
-
|
311
|
-
def span(&predicate)
|
312
|
-
result = @array.inject([true,[],[]]) {|z,x|
|
313
|
-
still_matched, first, second = z[0], z[1], z[2]
|
314
|
-
if !
|
315
|
-
[false,first,second.push(x)]
|
316
|
-
elsif yield x
|
317
|
-
[true,first.push(x),second]
|
318
|
-
else
|
319
|
-
[false,first,second.push(x)]
|
320
|
-
end
|
321
|
-
}
|
322
|
-
Seq.new([result[1],result[2]])
|
323
|
-
end
|
324
|
-
|
325
|
-
def split_at(n)
|
326
|
-
Seq.new([@array.take(n)
|
327
|
-
end
|
328
|
-
|
329
|
-
def starts_with(that)
|
330
|
-
if that.nil? || that.length > @array.length
|
331
|
-
return false
|
332
|
-
end
|
333
|
-
this_start = @array.take(that.length)
|
334
|
-
this_start.zip(that).inject(true) {|all_matched,a|
|
335
|
-
if !
|
336
|
-
false
|
337
|
-
elsif a.length != 2
|
338
|
-
false
|
339
|
-
else
|
340
|
-
a[0] == a[1]
|
341
|
-
end
|
342
|
-
}
|
343
|
-
end
|
344
|
-
|
345
|
-
def sum
|
346
|
-
@array.inject(0) {|z,x| z + x }
|
347
|
-
end
|
348
|
-
|
349
|
-
def tail
|
350
|
-
Seq.new(@array.drop(1))
|
351
|
-
end
|
352
|
-
|
353
|
-
def take(n)
|
354
|
-
Seq.new(@array.take(n))
|
355
|
-
end
|
356
|
-
|
357
|
-
def take_right(n)
|
358
|
-
Seq.new(@array.reverse.take(n).reverse)
|
359
|
-
end
|
360
|
-
|
361
|
-
def take_while(&predicate)
|
362
|
-
Seq.new(@array.inject([false,[]]) {|passed,x|
|
363
|
-
is_already_unmatched, result = passed[0], passed[1]
|
364
|
-
if is_already_unmatched
|
365
|
-
passed
|
366
|
-
elsif yield x
|
367
|
-
[false,result.push(x)]
|
368
|
-
else
|
369
|
-
[true, result]
|
370
|
-
end
|
371
|
-
}[1])
|
372
|
-
end
|
373
|
-
|
374
|
-
def union(that)
|
375
|
-
Seq.new(@array.concat(that))
|
376
|
-
end
|
377
|
-
|
378
|
-
def updated(idx, elm)
|
379
|
-
Seq.new(@array.fill(elm,idx,1))
|
380
|
-
end
|
381
|
-
|
382
|
-
def zip(that)
|
383
|
-
Seq.new(@array.zip(that).select {|a,b| !
|
384
|
-
end
|
385
|
-
|
386
|
-
def zip_with_index
|
387
|
-
with_index = []
|
388
|
-
@array.each_with_index do |v,idx|
|
389
|
-
with_index.push([v,idx])
|
390
|
-
end
|
391
|
-
with_index
|
392
|
-
end
|
393
|
-
|
394
|
-
end
|
395
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Scaruby
|
4
|
+
class Seq
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr :array
|
8
|
+
|
9
|
+
def each(&block)
|
10
|
+
@array.each do |e|
|
11
|
+
yield e
|
12
|
+
end
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.apply(enumerable)
|
17
|
+
Seq.new(enumerable)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(enumerable)
|
21
|
+
assert_type(enumerable, Enumerable)
|
22
|
+
@array = enumerable.to_a
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_a
|
26
|
+
@array
|
27
|
+
end
|
28
|
+
|
29
|
+
def corresponds(that, &predicate)
|
30
|
+
@array.zip(that).inject(true) { |still_matched, e|
|
31
|
+
if !still_matched
|
32
|
+
false
|
33
|
+
elsif yield e[0], e[1]
|
34
|
+
true
|
35
|
+
else
|
36
|
+
false
|
37
|
+
end
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def count(&predicate)
|
42
|
+
@array.count(&predicate)
|
43
|
+
end
|
44
|
+
|
45
|
+
def diff(that)
|
46
|
+
filter { |e| !that.include?(e) }
|
47
|
+
end
|
48
|
+
|
49
|
+
def distinct
|
50
|
+
Seq.new(@array.uniq)
|
51
|
+
end
|
52
|
+
|
53
|
+
def drop(n)
|
54
|
+
Seq.new(@array.drop(n))
|
55
|
+
end
|
56
|
+
|
57
|
+
def drop_right(n)
|
58
|
+
Seq.new(@array.reverse.drop(n).reverse)
|
59
|
+
end
|
60
|
+
|
61
|
+
def drop_while(&predicate)
|
62
|
+
Seq.new(@array.inject([false, []]) { |passed, x|
|
63
|
+
no_need_to_yield, result = passed[0], passed[1]
|
64
|
+
if no_need_to_yield
|
65
|
+
[true, result.push(x)]
|
66
|
+
elsif yield x
|
67
|
+
passed
|
68
|
+
else
|
69
|
+
[true, result.push(x)]
|
70
|
+
end
|
71
|
+
}[1])
|
72
|
+
end
|
73
|
+
|
74
|
+
def ends_with(that)
|
75
|
+
if that.nil? || that.length > @array.length
|
76
|
+
return false
|
77
|
+
end
|
78
|
+
this_end = @array.reverse.take(that.length).reverse
|
79
|
+
this_end.zip(that).inject(true) { |all_matched, a|
|
80
|
+
if !all_matched
|
81
|
+
false
|
82
|
+
elsif a.length != 2
|
83
|
+
false
|
84
|
+
else
|
85
|
+
a[0] == a[1]
|
86
|
+
end
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
def exists(&predicate)
|
91
|
+
@array.any?(&predicate)
|
92
|
+
end
|
93
|
+
|
94
|
+
def filter(&predicate)
|
95
|
+
Seq.new(@array.select { |e| yield e })
|
96
|
+
end
|
97
|
+
|
98
|
+
def filter_not(&predicate)
|
99
|
+
Seq.new(@array.select { |e| !yield e })
|
100
|
+
end
|
101
|
+
|
102
|
+
def find(&predicate)
|
103
|
+
Option.new(@array.find(&predicate))
|
104
|
+
end
|
105
|
+
|
106
|
+
def flat_map(&block)
|
107
|
+
Seq.new(@array.inject([]) { |z, x|
|
108
|
+
applied = yield x
|
109
|
+
if applied.is_a?(Enumerable)
|
110
|
+
applied.inject(z) { |z, elm| z.push(elm) }
|
111
|
+
elsif applied.is_a?(Seq)
|
112
|
+
applied.to_a.inject(z) { |z, elm| z.push(elm) }
|
113
|
+
elsif applied.is_a?(Option)
|
114
|
+
applied.is_defined ? z.push(applied.get) : z
|
115
|
+
else
|
116
|
+
z.push(applied)
|
117
|
+
end
|
118
|
+
})
|
119
|
+
end
|
120
|
+
|
121
|
+
def flatten
|
122
|
+
Seq.new(@array.inject([]) { |z, x|
|
123
|
+
if x.is_a?(Enumerable)
|
124
|
+
x.inject(z) { |z, elm| z.push(elm) }
|
125
|
+
elsif x.is_a?(Option)
|
126
|
+
x.is_defined ? z.push(x.get) : z
|
127
|
+
else
|
128
|
+
z.push(x)
|
129
|
+
end
|
130
|
+
})
|
131
|
+
end
|
132
|
+
|
133
|
+
def fold_left(z, &block)
|
134
|
+
@array.inject(z) { |z, x| yield z, x }
|
135
|
+
end
|
136
|
+
|
137
|
+
def fold_right(z, &block)
|
138
|
+
@array.reverse.inject(z) { |z, x| yield z, x }
|
139
|
+
end
|
140
|
+
|
141
|
+
def forall(&predicate)
|
142
|
+
@array.all?(&predicate)
|
143
|
+
end
|
144
|
+
|
145
|
+
def foreach(&block)
|
146
|
+
@array.each do |e|
|
147
|
+
yield e
|
148
|
+
end
|
149
|
+
nil
|
150
|
+
end
|
151
|
+
|
152
|
+
def group_by(&block)
|
153
|
+
Map.new(@array.inject({}) { |z, e|
|
154
|
+
if z[e].nil?
|
155
|
+
z[e] = [e]
|
156
|
+
else
|
157
|
+
z[e] = z[e].push(e)
|
158
|
+
end
|
159
|
+
z
|
160
|
+
})
|
161
|
+
end
|
162
|
+
|
163
|
+
def head
|
164
|
+
@array.first
|
165
|
+
end
|
166
|
+
|
167
|
+
def head_option
|
168
|
+
Option.new(@array.first)
|
169
|
+
end
|
170
|
+
|
171
|
+
def indices
|
172
|
+
Seq.new(0.upto(@array.length-1))
|
173
|
+
end
|
174
|
+
|
175
|
+
def init
|
176
|
+
Seq.new(@array.take(@array.length - 1))
|
177
|
+
end
|
178
|
+
|
179
|
+
def intersect(that)
|
180
|
+
Seq.new(@array.inject([]) { |z, x|
|
181
|
+
that.include?(x) ? z.push(x) : z
|
182
|
+
})
|
183
|
+
end
|
184
|
+
|
185
|
+
def is_empty
|
186
|
+
@array.nil? || @array.empty?
|
187
|
+
end
|
188
|
+
|
189
|
+
def last
|
190
|
+
@array.last
|
191
|
+
end
|
192
|
+
|
193
|
+
def last_option
|
194
|
+
Option.new(@array.last)
|
195
|
+
end
|
196
|
+
|
197
|
+
def length
|
198
|
+
@array.length
|
199
|
+
end
|
200
|
+
|
201
|
+
def lift
|
202
|
+
AppliableProc.new { |i| Option.new(@array[i]) }
|
203
|
+
end
|
204
|
+
|
205
|
+
def map(&block)
|
206
|
+
#Seq.new(@array.collect {|e| yield e })
|
207
|
+
Seq.new(@array.map { |e| yield e })
|
208
|
+
end
|
209
|
+
|
210
|
+
def max
|
211
|
+
@array.max
|
212
|
+
end
|
213
|
+
|
214
|
+
def min
|
215
|
+
@array.min
|
216
|
+
end
|
217
|
+
|
218
|
+
def mk_string(*args)
|
219
|
+
case args.length
|
220
|
+
when 0
|
221
|
+
start_part, sep, end_part = '', '', ''
|
222
|
+
when 1
|
223
|
+
start_part, sep, end_part = '', args[0], ''
|
224
|
+
when 2
|
225
|
+
raise ArgumentError, 'Illegal number of arguments (' + args.length.to_s + ')'
|
226
|
+
else
|
227
|
+
start_part, sep, end_part = args[0], args[1], args[2]
|
228
|
+
end
|
229
|
+
start_part + @array.join(sep) + end_part
|
230
|
+
end
|
231
|
+
|
232
|
+
def non_empty
|
233
|
+
!is_empty
|
234
|
+
end
|
235
|
+
|
236
|
+
def partition(&predicate)
|
237
|
+
Seq.new(@array.chunk(&predicate).inject([[], []]) { |z, matched_and_elm|
|
238
|
+
matched, elm = matched_and_elm[0], matched_and_elm[1].first
|
239
|
+
if matched
|
240
|
+
[z[0].push(elm), z[1]]
|
241
|
+
else
|
242
|
+
[z[0], z[1].push(elm)]
|
243
|
+
end
|
244
|
+
})
|
245
|
+
end
|
246
|
+
|
247
|
+
def patch(from, patch, replaced)
|
248
|
+
#Seq.new(@array.take(from).concat(patch).concat(@array.drop(from).drop(replaced)))
|
249
|
+
Seq.new(@array.take(from) + patch + @array.drop(from).drop(replaced))
|
250
|
+
end
|
251
|
+
|
252
|
+
def reverse
|
253
|
+
Seq.new(@array.reverse)
|
254
|
+
end
|
255
|
+
|
256
|
+
def reverse_map(&block)
|
257
|
+
#Seq.new(@array.reverse.collect {|e| yield e })
|
258
|
+
Seq.new(@array.reverse.map { |e| yield e })
|
259
|
+
end
|
260
|
+
|
261
|
+
def same_elements(that)
|
262
|
+
@array.zip(that).inject(true) { |still_same, a|
|
263
|
+
if !still_same
|
264
|
+
false
|
265
|
+
elsif a.length != 2
|
266
|
+
false
|
267
|
+
else
|
268
|
+
a[0] == a[1]
|
269
|
+
end
|
270
|
+
}
|
271
|
+
end
|
272
|
+
|
273
|
+
def scan_left(n, &block)
|
274
|
+
Seq.new(@array.inject([n, [n]]) { |last_and_array, x|
|
275
|
+
last, array = last_and_array[0], last_and_array[1]
|
276
|
+
applied = yield last, x
|
277
|
+
[applied, array.push(applied)]
|
278
|
+
}[1])
|
279
|
+
end
|
280
|
+
|
281
|
+
def scan_right(n, &block)
|
282
|
+
Seq.new(@array.reverse.inject([n, [n]]) { |last_and_array, x|
|
283
|
+
last, array = last_and_array[0], last_and_array[1]
|
284
|
+
applied = yield last, x
|
285
|
+
[applied, array.push(applied)]
|
286
|
+
}[1].reverse)
|
287
|
+
end
|
288
|
+
|
289
|
+
def size
|
290
|
+
@array.length
|
291
|
+
end
|
292
|
+
|
293
|
+
def slice(from, until_n)
|
294
|
+
Seq.new(@array.drop(from).take(until_n - from))
|
295
|
+
end
|
296
|
+
|
297
|
+
def sliding(len)
|
298
|
+
result = []
|
299
|
+
@array.each_with_index do |e, idx|
|
300
|
+
if idx < (@array.length - len + 1)
|
301
|
+
result.push(@array.slice(idx, len))
|
302
|
+
end
|
303
|
+
end
|
304
|
+
Seq.new(result)
|
305
|
+
end
|
306
|
+
|
307
|
+
def sort_with(&predicate)
|
308
|
+
@array.sort(&predicate)
|
309
|
+
end
|
310
|
+
|
311
|
+
def span(&predicate)
|
312
|
+
result = @array.inject([true, [], []]) { |z, x|
|
313
|
+
still_matched, first, second = z[0], z[1], z[2]
|
314
|
+
if !still_matched
|
315
|
+
[false, first, second.push(x)]
|
316
|
+
elsif yield x
|
317
|
+
[true, first.push(x), second]
|
318
|
+
else
|
319
|
+
[false, first, second.push(x)]
|
320
|
+
end
|
321
|
+
}
|
322
|
+
Seq.new([result[1], result[2]])
|
323
|
+
end
|
324
|
+
|
325
|
+
def split_at(n)
|
326
|
+
Seq.new([@array.take(n), @array.drop(n)])
|
327
|
+
end
|
328
|
+
|
329
|
+
def starts_with(that)
|
330
|
+
if that.nil? || that.length > @array.length
|
331
|
+
return false
|
332
|
+
end
|
333
|
+
this_start = @array.take(that.length)
|
334
|
+
this_start.zip(that).inject(true) { |all_matched, a|
|
335
|
+
if !all_matched
|
336
|
+
false
|
337
|
+
elsif a.length != 2
|
338
|
+
false
|
339
|
+
else
|
340
|
+
a[0] == a[1]
|
341
|
+
end
|
342
|
+
}
|
343
|
+
end
|
344
|
+
|
345
|
+
def sum
|
346
|
+
@array.inject(0) { |z, x| z + x }
|
347
|
+
end
|
348
|
+
|
349
|
+
def tail
|
350
|
+
Seq.new(@array.drop(1))
|
351
|
+
end
|
352
|
+
|
353
|
+
def take(n)
|
354
|
+
Seq.new(@array.take(n))
|
355
|
+
end
|
356
|
+
|
357
|
+
def take_right(n)
|
358
|
+
Seq.new(@array.reverse.take(n).reverse)
|
359
|
+
end
|
360
|
+
|
361
|
+
def take_while(&predicate)
|
362
|
+
Seq.new(@array.inject([false, []]) { |passed, x|
|
363
|
+
is_already_unmatched, result = passed[0], passed[1]
|
364
|
+
if is_already_unmatched
|
365
|
+
passed
|
366
|
+
elsif yield x
|
367
|
+
[false, result.push(x)]
|
368
|
+
else
|
369
|
+
[true, result]
|
370
|
+
end
|
371
|
+
}[1])
|
372
|
+
end
|
373
|
+
|
374
|
+
def union(that)
|
375
|
+
Seq.new(@array.concat(that))
|
376
|
+
end
|
377
|
+
|
378
|
+
def updated(idx, elm)
|
379
|
+
Seq.new(@array.fill(elm, idx, 1))
|
380
|
+
end
|
381
|
+
|
382
|
+
def zip(that)
|
383
|
+
Seq.new(@array.zip(that).select { |a, b| !a.nil? && !b.nil? })
|
384
|
+
end
|
385
|
+
|
386
|
+
def zip_with_index
|
387
|
+
with_index = []
|
388
|
+
@array.each_with_index do |v, idx|
|
389
|
+
with_index.push([v, idx])
|
390
|
+
end
|
391
|
+
with_index
|
392
|
+
end
|
393
|
+
|
394
|
+
end
|
395
|
+
end
|