musa-dsl 0.22.6 → 0.23.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/lib/musa-dsl.rb +14 -8
- data/lib/musa-dsl/core-ext/deep-copy.rb +12 -1
- data/lib/musa-dsl/core-ext/smart-proc-binder.rb +13 -11
- data/lib/musa-dsl/datasets/p.rb +14 -10
- data/lib/musa-dsl/generative/backboner.rb +6 -11
- data/lib/musa-dsl/generative/generative-grammar.rb +1 -3
- data/lib/musa-dsl/generative/markov.rb +10 -6
- data/lib/musa-dsl/neumalang/neumalang.rb +1 -1
- data/lib/musa-dsl/neumas/array-to-neumas.rb +1 -1
- data/lib/musa-dsl/sequencer/base-sequencer-implementation-play-helper.rb +2 -2
- data/lib/musa-dsl/sequencer/sequencer-dsl.rb +6 -6
- data/lib/musa-dsl/series/base-series.rb +293 -144
- data/lib/musa-dsl/series/buffer-serie.rb +237 -0
- data/lib/musa-dsl/series/hash-or-array-serie-splitter.rb +128 -129
- data/lib/musa-dsl/series/main-serie-constructors.rb +247 -154
- data/lib/musa-dsl/series/main-serie-operations.rb +281 -316
- data/lib/musa-dsl/series/proxy-serie.rb +21 -41
- data/lib/musa-dsl/series/quantizer-serie.rb +38 -38
- data/lib/musa-dsl/series/queue-serie.rb +39 -43
- data/lib/musa-dsl/series/series-composer.rb +149 -0
- data/lib/musa-dsl/series/series.rb +5 -1
- data/lib/musa-dsl/series/timed-serie.rb +106 -119
- data/musa-dsl.gemspec +12 -2
- metadata +7 -7
- data/lib/musa-dsl/series/holder-serie.rb +0 -87
@@ -1,6 +1,6 @@
|
|
1
1
|
module Musa
|
2
2
|
module Series
|
3
|
-
module
|
3
|
+
module Operations
|
4
4
|
def autorestart
|
5
5
|
Autorestart.new self
|
6
6
|
end
|
@@ -108,12 +108,12 @@ module Musa
|
|
108
108
|
|
109
109
|
alias_method :eval, :with
|
110
110
|
|
111
|
-
def map(&
|
112
|
-
ProcessWith.new self, &
|
111
|
+
def map(&block)
|
112
|
+
ProcessWith.new self, &block
|
113
113
|
end
|
114
114
|
|
115
|
-
def anticipate(&
|
116
|
-
Anticipate.new self, &
|
115
|
+
def anticipate(&block)
|
116
|
+
Anticipate.new self, &block
|
117
117
|
end
|
118
118
|
|
119
119
|
###
|
@@ -121,34 +121,36 @@ module Musa
|
|
121
121
|
###
|
122
122
|
|
123
123
|
class ProcessWith
|
124
|
-
include
|
125
|
-
include Serie
|
126
|
-
|
127
|
-
attr_reader :source, :on_restart, :block
|
128
|
-
def with_sources; @sources; end
|
124
|
+
include Serie.with(source: true, sources: true, sources_as: :with_sources, smart_block: true)
|
129
125
|
|
130
126
|
def initialize(serie, with_series = nil, on_restart = nil, &block)
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
127
|
+
self.source = serie
|
128
|
+
self.with_sources = with_series || {}
|
129
|
+
self.on_restart = on_restart
|
130
|
+
self.proc = block if block
|
131
|
+
|
132
|
+
init
|
133
|
+
end
|
135
134
|
|
136
|
-
|
137
|
-
|
135
|
+
def on_restart(&block)
|
136
|
+
if block
|
137
|
+
@on_restart = block
|
138
138
|
else
|
139
|
-
@
|
139
|
+
@on_restart
|
140
140
|
end
|
141
|
+
end
|
141
142
|
|
142
|
-
|
143
|
+
def on_restart=(block)
|
144
|
+
@on_restart = block
|
143
145
|
end
|
144
146
|
|
145
|
-
def _restart
|
147
|
+
private def _restart
|
146
148
|
@source.restart
|
147
|
-
@sources.values.each
|
149
|
+
@sources.values.each(&:restart)
|
148
150
|
@on_restart.call if @on_restart
|
149
151
|
end
|
150
152
|
|
151
|
-
def _next_value
|
153
|
+
private def _next_value
|
152
154
|
main = @source.next_value
|
153
155
|
others = @sources.transform_values { |v| v.next_value }
|
154
156
|
|
@@ -173,23 +175,20 @@ module Musa
|
|
173
175
|
private_constant :ProcessWith
|
174
176
|
|
175
177
|
class Anticipate
|
176
|
-
include
|
177
|
-
include Serie
|
178
|
-
|
179
|
-
attr_reader :source, :block
|
178
|
+
include Serie.with(source: true, block: true)
|
180
179
|
|
181
180
|
def initialize(serie, &block)
|
182
|
-
|
183
|
-
|
181
|
+
self.source = serie
|
182
|
+
self.proc = block
|
184
183
|
|
185
|
-
|
184
|
+
init
|
186
185
|
end
|
187
186
|
|
188
|
-
def _restart
|
187
|
+
private def _restart
|
189
188
|
@source.restart
|
190
189
|
end
|
191
190
|
|
192
|
-
def _next_value
|
191
|
+
private def _next_value
|
193
192
|
previous_value = @source.current_value
|
194
193
|
value = @source.next_value
|
195
194
|
peek_next_value = @source.peek_next_value
|
@@ -209,30 +208,16 @@ module Musa
|
|
209
208
|
private_constant :Anticipate
|
210
209
|
|
211
210
|
class Switcher
|
212
|
-
include Serie
|
213
|
-
|
214
|
-
attr_accessor :sources
|
215
|
-
def selector; @source; end
|
211
|
+
include Serie.with(source: true, sources: true, sources_as: :options)
|
216
212
|
|
217
213
|
def initialize(selector, indexed_series, hash_series)
|
214
|
+
self.source = selector
|
215
|
+
self.options = indexed_series || hash_series
|
218
216
|
|
219
|
-
|
220
|
-
get = @source.prototype? ? :prototype : :instance
|
221
|
-
|
222
|
-
if indexed_series && !indexed_series.empty?
|
223
|
-
@sources = indexed_series.collect(&get)
|
224
|
-
elsif hash_series && !hash_series.empty?
|
225
|
-
@sources = hash_series.clone.transform_values(&get)
|
226
|
-
end
|
227
|
-
|
228
|
-
if get == :prototype!
|
229
|
-
@sources.freeze
|
230
|
-
end
|
231
|
-
|
232
|
-
mark_regarding! @source
|
217
|
+
init
|
233
218
|
end
|
234
219
|
|
235
|
-
def _restart
|
220
|
+
private def _restart
|
236
221
|
@source.restart
|
237
222
|
if @sources.is_a? Array
|
238
223
|
@sources.each(&:restart)
|
@@ -241,7 +226,7 @@ module Musa
|
|
241
226
|
end
|
242
227
|
end
|
243
228
|
|
244
|
-
def _next_value
|
229
|
+
private def _next_value
|
245
230
|
value = nil
|
246
231
|
|
247
232
|
index_or_key = @source.next_value
|
@@ -259,46 +244,31 @@ module Musa
|
|
259
244
|
private_constant :Switcher
|
260
245
|
|
261
246
|
class MultiplexSelector
|
262
|
-
include Serie
|
263
|
-
|
264
|
-
def selector; @source; end
|
265
|
-
attr_accessor :sources
|
247
|
+
include Serie.with(source: true, sources: true, sources_as: :options)
|
266
248
|
|
267
249
|
def initialize(selector, indexed_series, hash_series)
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
if indexed_series && !indexed_series.empty?
|
272
|
-
@sources = indexed_series.collect(&get)
|
273
|
-
elsif hash_series && !hash_series.empty?
|
274
|
-
@sources = hash_series.clone.transform_values(&get)
|
275
|
-
end
|
276
|
-
|
277
|
-
_restart false
|
278
|
-
|
279
|
-
if get == :prototype
|
280
|
-
@sources.freeze
|
281
|
-
end
|
250
|
+
self.source = selector
|
251
|
+
self.options = indexed_series || hash_series
|
282
252
|
|
283
|
-
|
253
|
+
init
|
284
254
|
end
|
285
255
|
|
286
|
-
def
|
256
|
+
private def _init
|
287
257
|
@current_value = nil
|
258
|
+
@first = true
|
259
|
+
end
|
288
260
|
|
289
|
-
|
290
|
-
|
291
|
-
if @sources.is_a? Array
|
292
|
-
@sources.each(&:restart)
|
293
|
-
elsif @sources.is_a? Hash
|
294
|
-
@sources.each { |_key, serie| serie.restart }
|
295
|
-
end
|
296
|
-
end
|
261
|
+
private def _restart
|
262
|
+
@source.restart
|
297
263
|
|
298
|
-
@
|
264
|
+
if @sources.is_a? Array
|
265
|
+
@sources.each(&:restart)
|
266
|
+
elsif @sources.is_a? Hash
|
267
|
+
@sources.values.each(&:restart)
|
268
|
+
end
|
299
269
|
end
|
300
270
|
|
301
|
-
def _next_value
|
271
|
+
private def _next_value
|
302
272
|
@current_value =
|
303
273
|
if @first || !@current_value.nil?
|
304
274
|
@first = false
|
@@ -318,34 +288,21 @@ module Musa
|
|
318
288
|
private_constant :MultiplexSelector
|
319
289
|
|
320
290
|
class SwitchFullSerie
|
321
|
-
include Serie
|
322
|
-
|
323
|
-
def selector; @source; end
|
324
|
-
attr_accessor :sources
|
291
|
+
include Serie.with(source: true, sources: true, sources_as: :options)
|
325
292
|
|
326
293
|
def initialize(selector, indexed_series, hash_series)
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
if indexed_series && !indexed_series.empty?
|
331
|
-
@sources = indexed_series.collect(&get)
|
332
|
-
elsif hash_series && !hash_series.empty?
|
333
|
-
@sources = hash_series.clone.transform_values(&get)
|
334
|
-
end
|
335
|
-
|
336
|
-
if get == :prototype
|
337
|
-
@sources.freeze
|
338
|
-
end
|
294
|
+
self.source = selector
|
295
|
+
self.options = indexed_series || hash_series
|
339
296
|
|
340
|
-
|
297
|
+
init
|
341
298
|
end
|
342
299
|
|
343
|
-
def _restart
|
300
|
+
private def _restart
|
344
301
|
@source.restart
|
345
302
|
@sources.each(&:restart)
|
346
303
|
end
|
347
304
|
|
348
|
-
def _next_value
|
305
|
+
private def _next_value
|
349
306
|
value = nil
|
350
307
|
|
351
308
|
value = @sources[@index_or_key].next_value unless @index_or_key.nil?
|
@@ -367,21 +324,18 @@ module Musa
|
|
367
324
|
private_constant :SwitchFullSerie
|
368
325
|
|
369
326
|
class InfiniteRepeater
|
370
|
-
include Serie
|
371
|
-
|
372
|
-
attr_reader :source
|
327
|
+
include Serie.with(source: true)
|
373
328
|
|
374
329
|
def initialize(serie)
|
375
|
-
|
376
|
-
|
377
|
-
mark_regarding! @source
|
330
|
+
self.source = serie
|
331
|
+
init
|
378
332
|
end
|
379
333
|
|
380
|
-
def _restart
|
334
|
+
private def _restart
|
381
335
|
@source.restart
|
382
336
|
end
|
383
337
|
|
384
|
-
def _next_value
|
338
|
+
private def _next_value
|
385
339
|
value = @source.next_value
|
386
340
|
|
387
341
|
if value.nil?
|
@@ -400,38 +354,47 @@ module Musa
|
|
400
354
|
private_constant :InfiniteRepeater
|
401
355
|
|
402
356
|
class Repeater
|
403
|
-
include Serie
|
404
|
-
|
405
|
-
attr_reader :source, :times, :condition
|
357
|
+
include Serie.with(source: true)
|
406
358
|
|
407
359
|
def initialize(serie, times = nil, &condition)
|
408
|
-
|
360
|
+
self.source = serie
|
361
|
+
self.times = times
|
362
|
+
self.condition = condition
|
409
363
|
|
410
|
-
|
411
|
-
|
364
|
+
init
|
365
|
+
end
|
412
366
|
|
413
|
-
|
414
|
-
@condition = calculate_condition
|
367
|
+
attr_reader :times
|
415
368
|
|
416
|
-
|
369
|
+
def times=(value)
|
370
|
+
@times = value
|
371
|
+
calculate_condition
|
417
372
|
end
|
418
373
|
|
419
|
-
def
|
420
|
-
|
421
|
-
|
374
|
+
def condition(&block)
|
375
|
+
if block
|
376
|
+
@external_condition = block
|
377
|
+
calculate_condition
|
378
|
+
else
|
379
|
+
@external_condition
|
380
|
+
end
|
422
381
|
end
|
423
382
|
|
424
|
-
def
|
425
|
-
|
426
|
-
|
383
|
+
def condition=(block)
|
384
|
+
@external_condition = block
|
385
|
+
calculate_condition
|
427
386
|
end
|
428
387
|
|
429
|
-
def
|
388
|
+
private def _init
|
430
389
|
@count = 0
|
431
|
-
|
390
|
+
calculate_condition
|
391
|
+
end
|
392
|
+
|
393
|
+
private def _restart
|
394
|
+
@source.restart
|
432
395
|
end
|
433
396
|
|
434
|
-
def _next_value
|
397
|
+
private def _next_value
|
435
398
|
value = @source.next_value
|
436
399
|
|
437
400
|
if value.nil?
|
@@ -451,38 +414,39 @@ module Musa
|
|
451
414
|
end
|
452
415
|
|
453
416
|
private def calculate_condition
|
454
|
-
if @external_condition
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
417
|
+
@condition = if @external_condition
|
418
|
+
@external_condition
|
419
|
+
elsif @times
|
420
|
+
proc { @count < @times }
|
421
|
+
else
|
422
|
+
proc { false }
|
423
|
+
end
|
461
424
|
end
|
462
425
|
end
|
463
426
|
|
464
427
|
private_constant :Repeater
|
465
428
|
|
466
429
|
class LengthLimiter
|
467
|
-
include Serie
|
468
|
-
|
469
|
-
attr_reader :source, :length
|
430
|
+
include Serie.with(source: true)
|
470
431
|
|
471
432
|
def initialize(serie, length)
|
472
|
-
|
473
|
-
|
433
|
+
self.source = serie
|
434
|
+
self.length = length
|
474
435
|
|
475
|
-
|
476
|
-
|
477
|
-
mark_regarding! @source
|
436
|
+
init
|
478
437
|
end
|
479
438
|
|
480
|
-
|
439
|
+
attr_accessor :length
|
440
|
+
|
441
|
+
private def _init
|
481
442
|
@position = 0
|
482
|
-
@source.restart if restart_sources
|
483
443
|
end
|
484
444
|
|
485
|
-
def
|
445
|
+
private def _restart
|
446
|
+
@source.restart
|
447
|
+
end
|
448
|
+
|
449
|
+
private def _next_value
|
486
450
|
if @position < @length
|
487
451
|
@position += 1
|
488
452
|
@source.next_value
|
@@ -497,25 +461,26 @@ module Musa
|
|
497
461
|
private_constant :LengthLimiter
|
498
462
|
|
499
463
|
class Skipper
|
500
|
-
include Serie
|
501
|
-
|
502
|
-
attr_reader :source, :length
|
464
|
+
include Serie.with(source: true)
|
503
465
|
|
504
466
|
def initialize(serie, length)
|
505
|
-
|
506
|
-
|
467
|
+
self.source = serie
|
468
|
+
self.length = length
|
507
469
|
|
508
|
-
|
509
|
-
|
510
|
-
mark_regarding! @source
|
470
|
+
init
|
511
471
|
end
|
512
472
|
|
513
|
-
|
514
|
-
|
473
|
+
attr_accessor :length
|
474
|
+
|
475
|
+
private def _init
|
515
476
|
@first = true
|
516
477
|
end
|
517
478
|
|
518
|
-
def
|
479
|
+
private def _restart
|
480
|
+
@source.restart
|
481
|
+
end
|
482
|
+
|
483
|
+
private def _next_value
|
519
484
|
@length.times { @source.next_value } if @first
|
520
485
|
@first = nil
|
521
486
|
|
@@ -530,40 +495,25 @@ module Musa
|
|
530
495
|
private_constant :Skipper
|
531
496
|
|
532
497
|
class Flattener
|
533
|
-
include Serie
|
534
|
-
|
535
|
-
attr_reader :source
|
498
|
+
include Serie.base
|
536
499
|
|
537
500
|
def initialize(serie)
|
538
501
|
@source = serie
|
539
|
-
|
540
|
-
_restart false
|
541
|
-
|
542
502
|
mark_regarding! @source
|
503
|
+
init
|
543
504
|
end
|
544
505
|
|
545
|
-
def
|
546
|
-
|
547
|
-
|
548
|
-
end
|
549
|
-
|
550
|
-
def _instance!
|
551
|
-
super
|
552
|
-
_restart false
|
506
|
+
private def _init
|
507
|
+
@stack = [@source]
|
508
|
+
@restart_each_serie = false
|
553
509
|
end
|
554
510
|
|
555
|
-
def _restart
|
556
|
-
|
557
|
-
|
558
|
-
@restart_each_serie = true
|
559
|
-
else
|
560
|
-
@restart_each_serie = false
|
561
|
-
end
|
562
|
-
|
563
|
-
@stack = [@source]
|
511
|
+
private def _restart
|
512
|
+
@source.restart
|
513
|
+
@restart_each_serie = true
|
564
514
|
end
|
565
515
|
|
566
|
-
def _next_value
|
516
|
+
private def _next_value
|
567
517
|
if @stack.last
|
568
518
|
value = @stack.last.next_value
|
569
519
|
|
@@ -590,26 +540,24 @@ module Musa
|
|
590
540
|
private_constant :Flattener
|
591
541
|
|
592
542
|
class MergeSerieOfSeries
|
593
|
-
include Serie
|
594
|
-
|
595
|
-
attr_reader :source
|
543
|
+
include Serie.with(source: true)
|
596
544
|
|
597
545
|
def initialize(serie)
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
mark_regarding! @source
|
546
|
+
self.source = serie
|
547
|
+
init
|
602
548
|
end
|
603
549
|
|
604
|
-
def
|
605
|
-
if restart_sources
|
606
|
-
@source.restart
|
607
|
-
@restart_each_serie = true
|
608
|
-
end
|
550
|
+
private def _init
|
609
551
|
@current_serie = nil
|
552
|
+
@restart_each_serie = false
|
610
553
|
end
|
611
554
|
|
612
|
-
def
|
555
|
+
private def _restart
|
556
|
+
@source.restart
|
557
|
+
@restart_each_serie = true
|
558
|
+
end
|
559
|
+
|
560
|
+
private def _next_value
|
613
561
|
value = nil
|
614
562
|
|
615
563
|
restart_current_serie_if_needed = false
|
@@ -646,27 +594,28 @@ module Musa
|
|
646
594
|
private_constant :MergeSerieOfSeries
|
647
595
|
|
648
596
|
class Processor
|
649
|
-
include
|
650
|
-
include Serie
|
651
|
-
|
652
|
-
attr_reader :source
|
597
|
+
include Serie.with(source: true, smart_block: true)
|
653
598
|
|
654
599
|
def initialize(serie, parameters, &processor)
|
655
|
-
|
656
|
-
@parameters = parameters
|
657
|
-
@processor = SmartProcBinder.new(processor)
|
600
|
+
self.source = serie
|
658
601
|
|
659
|
-
|
602
|
+
self.parameters = parameters
|
603
|
+
self.proc = processor if processor
|
660
604
|
|
661
|
-
|
605
|
+
init
|
662
606
|
end
|
663
607
|
|
664
|
-
|
665
|
-
|
608
|
+
attr_accessor :parameters
|
609
|
+
|
610
|
+
private def _init
|
666
611
|
@pending_values = []
|
667
612
|
end
|
668
613
|
|
669
|
-
def
|
614
|
+
private def _restart
|
615
|
+
@source.restart
|
616
|
+
end
|
617
|
+
|
618
|
+
private def _next_value
|
670
619
|
if @pending_values.empty?
|
671
620
|
|
672
621
|
v = @source.next_value
|
@@ -674,7 +623,7 @@ module Musa
|
|
674
623
|
if v.nil?
|
675
624
|
nil
|
676
625
|
else
|
677
|
-
value = @
|
626
|
+
value = @block.call(v, **@parameters)
|
678
627
|
|
679
628
|
if value.is_a?(Array)
|
680
629
|
@pending_values = value
|
@@ -700,23 +649,22 @@ module Musa
|
|
700
649
|
end
|
701
650
|
|
702
651
|
class Autorestart
|
703
|
-
include Serie
|
704
|
-
|
705
|
-
attr_reader :source
|
652
|
+
include Serie.with(source: true)
|
706
653
|
|
707
654
|
def initialize(serie)
|
708
|
-
|
655
|
+
self.source = serie
|
656
|
+
init
|
657
|
+
end
|
709
658
|
|
659
|
+
private def _init
|
710
660
|
@restart_on_next = false
|
711
|
-
|
712
|
-
mark_regarding! @source
|
713
661
|
end
|
714
662
|
|
715
|
-
def _restart
|
663
|
+
private def _restart
|
716
664
|
@source.restart
|
717
665
|
end
|
718
666
|
|
719
|
-
def _next_value
|
667
|
+
private def _next_value
|
720
668
|
if @restart_on_next
|
721
669
|
@source.restart
|
722
670
|
@restart_on_next = false
|
@@ -733,53 +681,59 @@ module Musa
|
|
733
681
|
private_constant :Autorestart
|
734
682
|
|
735
683
|
class Cutter
|
736
|
-
include Serie
|
737
|
-
|
738
|
-
attr_reader :source, :length
|
684
|
+
include Serie.with(source: true)
|
739
685
|
|
740
686
|
def initialize(serie, length)
|
741
|
-
|
742
|
-
|
687
|
+
self.source = serie
|
688
|
+
self.length = length
|
689
|
+
init
|
690
|
+
end
|
743
691
|
|
744
|
-
|
692
|
+
def source=(serie)
|
693
|
+
super
|
694
|
+
@previous&.source = serie
|
745
695
|
end
|
746
696
|
|
747
|
-
|
748
|
-
|
697
|
+
attr_reader :length
|
698
|
+
|
699
|
+
def length=(value)
|
700
|
+
@length = value
|
701
|
+
@previous&.length = value
|
749
702
|
end
|
750
703
|
|
751
|
-
def
|
752
|
-
@
|
704
|
+
private def _restart
|
705
|
+
@source.restart
|
706
|
+
end
|
753
707
|
|
708
|
+
private def _next_value
|
709
|
+
@previous&.materialize
|
754
710
|
@previous = CutSerie.new @source, @length if @source.peek_next_value
|
755
711
|
end
|
756
712
|
|
757
|
-
private
|
758
|
-
|
759
713
|
class CutSerie
|
760
|
-
include Serie
|
714
|
+
include Serie.with(source: true)
|
761
715
|
|
762
716
|
def initialize(serie, length)
|
763
|
-
|
764
|
-
|
717
|
+
self.source = serie.instance
|
718
|
+
self.length = length
|
765
719
|
|
766
720
|
@values = []
|
767
|
-
|
768
|
-
|
769
|
-
mark_as_instance!
|
721
|
+
init
|
770
722
|
end
|
771
723
|
|
724
|
+
attr_accessor :length
|
725
|
+
|
772
726
|
def _prototype!
|
773
727
|
# TODO review why cannot get prototype of a cut serie
|
774
|
-
raise
|
728
|
+
raise PrototypingError, 'Cannot get prototype of a cut serie'
|
775
729
|
end
|
776
730
|
|
777
|
-
def
|
731
|
+
private def _init
|
778
732
|
@count = 0
|
779
733
|
end
|
780
734
|
|
781
|
-
def _next_value
|
782
|
-
value
|
735
|
+
private def _next_value
|
736
|
+
value = @values[@count]
|
783
737
|
value ||= @values[@count] = @source.next_value if @count < @length
|
784
738
|
|
785
739
|
@count += 1
|
@@ -791,30 +745,29 @@ module Musa
|
|
791
745
|
(@values.size..@length - 1).each { |i| @values[i] = @source.next_value }
|
792
746
|
end
|
793
747
|
end
|
748
|
+
|
749
|
+
private_constant :CutSerie
|
794
750
|
end
|
795
751
|
|
796
752
|
private_constant :Cutter
|
797
753
|
|
798
754
|
class Locker
|
799
|
-
include Serie
|
800
|
-
|
801
|
-
attr_reader :source
|
755
|
+
include Serie.with(source: true)
|
802
756
|
|
803
757
|
def initialize(serie)
|
804
|
-
|
758
|
+
self.source = serie
|
759
|
+
|
805
760
|
@values = []
|
806
761
|
@first_round = true
|
807
762
|
|
808
|
-
|
809
|
-
|
810
|
-
mark_regarding! @source
|
763
|
+
init
|
811
764
|
end
|
812
765
|
|
813
|
-
def
|
766
|
+
private def _init
|
814
767
|
@index = 0
|
815
768
|
end
|
816
769
|
|
817
|
-
def _next_value
|
770
|
+
private def _next_value
|
818
771
|
if @first_round
|
819
772
|
value = @source.next_value
|
820
773
|
|
@@ -836,34 +789,33 @@ module Musa
|
|
836
789
|
private_constant :Locker
|
837
790
|
|
838
791
|
class Reverser
|
839
|
-
include Serie
|
840
|
-
|
841
|
-
attr_reader :source
|
792
|
+
include Serie.with(source: true)
|
842
793
|
|
843
794
|
def initialize(serie)
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
mark_regarding! @source
|
795
|
+
self.source = serie
|
796
|
+
init
|
848
797
|
end
|
849
798
|
|
850
|
-
def
|
799
|
+
def source=(serie)
|
800
|
+
raise ArgumentError, "A serie to reverse can't be infinite" if serie.infinite?
|
851
801
|
super
|
852
|
-
|
802
|
+
init
|
853
803
|
end
|
854
804
|
|
855
|
-
def
|
856
|
-
@
|
857
|
-
@reversed = FromArray.new(next_values_array_of(@source).reverse).instance if get_reversed
|
805
|
+
private def _init
|
806
|
+
@reversed = nil
|
858
807
|
end
|
859
808
|
|
860
|
-
def
|
861
|
-
@
|
809
|
+
private def _restart
|
810
|
+
@source.restart
|
862
811
|
end
|
863
812
|
|
864
|
-
private
|
813
|
+
private def _next_value
|
814
|
+
@reversed ||= Constructors.S(*next_values_array_of(@source).reverse).instance
|
815
|
+
@reversed.next_value
|
816
|
+
end
|
865
817
|
|
866
|
-
def next_values_array_of(serie)
|
818
|
+
private def next_values_array_of(serie)
|
867
819
|
array = []
|
868
820
|
|
869
821
|
until (value = serie.next_value).nil?
|
@@ -877,25 +829,27 @@ module Musa
|
|
877
829
|
private_constant :Reverser
|
878
830
|
|
879
831
|
class Randomizer
|
880
|
-
include Serie
|
881
|
-
|
882
|
-
attr_reader :source, :random
|
832
|
+
include Serie.with(source: true)
|
883
833
|
|
884
834
|
def initialize(serie, random)
|
885
|
-
|
886
|
-
|
835
|
+
self.source = serie
|
836
|
+
self.random = random
|
887
837
|
|
888
|
-
|
838
|
+
init
|
839
|
+
end
|
889
840
|
|
890
|
-
|
841
|
+
attr_accessor :random
|
842
|
+
|
843
|
+
private def _init
|
844
|
+
@values = @source.to_a(duplicate: false, restart: false)
|
891
845
|
end
|
892
846
|
|
893
|
-
def _restart
|
894
|
-
@source.restart
|
895
|
-
@values = @source.to_a
|
847
|
+
private def _restart
|
848
|
+
@source.restart
|
849
|
+
@values = @source.to_a(duplicate: false, restart: false)
|
896
850
|
end
|
897
851
|
|
898
|
-
def _next_value
|
852
|
+
private def _next_value
|
899
853
|
if !@values.empty?
|
900
854
|
position = @random.rand(0...@values.size)
|
901
855
|
value = @values[position]
|
@@ -912,30 +866,46 @@ module Musa
|
|
912
866
|
private_constant :Randomizer
|
913
867
|
|
914
868
|
class Shifter
|
915
|
-
include Serie
|
916
|
-
|
917
|
-
attr_reader :source, :shift
|
869
|
+
include Serie.with(source: true)
|
918
870
|
|
919
871
|
def initialize(serie, shift)
|
920
|
-
|
921
|
-
|
872
|
+
self.source = serie
|
873
|
+
self.shift = shift
|
922
874
|
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
_restart false
|
875
|
+
init
|
876
|
+
end
|
927
877
|
|
928
|
-
|
878
|
+
def source=(serie)
|
879
|
+
raise ArgumentError, "cannot shift to right an infinite serie" if @shift > 0 && serie.infinite?
|
880
|
+
super
|
881
|
+
# should _restart(false) ??? if so, we lost the shifted values of the previous serie; if not we don't shift the new serie values
|
882
|
+
# I think it's better to not _restart unless it's explicitly called by the caller
|
929
883
|
end
|
930
884
|
|
931
|
-
|
932
|
-
|
885
|
+
attr_reader :shift
|
886
|
+
|
887
|
+
def shift=(value)
|
888
|
+
raise ArgumentError, "cannot shift to right an infinite serie" if value > 0 && @source&.infinite?
|
889
|
+
raise NotImplementedError, 'cannot shift to right: function not yet implemented' if value > 0
|
933
890
|
|
891
|
+
@shift = value
|
892
|
+
end
|
893
|
+
|
894
|
+
private def _init
|
934
895
|
@shifted = []
|
935
|
-
@
|
896
|
+
@first = true
|
936
897
|
end
|
937
898
|
|
938
|
-
def
|
899
|
+
private def _restart
|
900
|
+
@source.restart
|
901
|
+
end
|
902
|
+
|
903
|
+
private def _next_value
|
904
|
+
if @first
|
905
|
+
@shift.abs.times { @shifted << @source.next_value } if @shift < 0
|
906
|
+
@first = false
|
907
|
+
end
|
908
|
+
|
939
909
|
value = @source.next_value
|
940
910
|
return value unless value.nil?
|
941
911
|
|
@@ -946,26 +916,26 @@ module Musa
|
|
946
916
|
private_constant :Shifter
|
947
917
|
|
948
918
|
class Remover
|
949
|
-
include Serie
|
950
|
-
|
951
|
-
attr_reader :source
|
919
|
+
include Serie.with(source: true, block: true)
|
952
920
|
|
953
921
|
def initialize(serie, &block)
|
954
|
-
|
955
|
-
|
956
|
-
@history = []
|
922
|
+
self.source = serie
|
923
|
+
self.proc = block
|
957
924
|
|
958
|
-
|
925
|
+
@history = []
|
959
926
|
|
960
|
-
|
927
|
+
init
|
961
928
|
end
|
962
929
|
|
963
|
-
def
|
964
|
-
@source.restart if restart_sources
|
930
|
+
private def _init
|
965
931
|
@history.clear
|
966
932
|
end
|
967
933
|
|
968
|
-
def
|
934
|
+
private def _restart
|
935
|
+
@source.restart
|
936
|
+
end
|
937
|
+
|
938
|
+
private def _next_value
|
969
939
|
if value = @source.next_value
|
970
940
|
while value && @block.call(value, @history)
|
971
941
|
@history << value
|
@@ -980,24 +950,20 @@ module Musa
|
|
980
950
|
private_constant :Remover
|
981
951
|
|
982
952
|
class Selector
|
983
|
-
include Serie
|
984
|
-
|
985
|
-
attr_reader :source
|
953
|
+
include Serie.with(source: true, block: true)
|
986
954
|
|
987
955
|
def initialize(serie, &block)
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
_restart false
|
956
|
+
self.source = serie
|
957
|
+
self.proc = block
|
992
958
|
|
993
|
-
|
959
|
+
init
|
994
960
|
end
|
995
961
|
|
996
|
-
def _restart
|
997
|
-
@source.restart
|
962
|
+
private def _restart
|
963
|
+
@source.restart
|
998
964
|
end
|
999
965
|
|
1000
|
-
def _next_value
|
966
|
+
private def _next_value
|
1001
967
|
value = @source.next_value
|
1002
968
|
until value.nil? || @block.call(value)
|
1003
969
|
value = @source.next_value
|
@@ -1009,23 +975,22 @@ module Musa
|
|
1009
975
|
private_constant :Selector
|
1010
976
|
|
1011
977
|
class HashFromSeriesArray
|
1012
|
-
include Serie
|
1013
|
-
|
1014
|
-
attr_reader :source, :keys
|
978
|
+
include Serie.with(source: true)
|
1015
979
|
|
1016
980
|
def initialize(serie, keys)
|
1017
|
-
|
1018
|
-
|
1019
|
-
_restart false
|
981
|
+
self.source = serie
|
982
|
+
self.keys = keys
|
1020
983
|
|
1021
|
-
|
984
|
+
init
|
1022
985
|
end
|
1023
986
|
|
1024
|
-
|
1025
|
-
|
987
|
+
attr_accessor :keys
|
988
|
+
|
989
|
+
private def _restart
|
990
|
+
@source.restart
|
1026
991
|
end
|
1027
992
|
|
1028
|
-
def _next_value
|
993
|
+
private def _next_value
|
1029
994
|
array = @source.next_value
|
1030
995
|
|
1031
996
|
return nil unless array
|