]> gitweb.factorcode.org Git - factor.git/blob - core/sequences/sequences.factor
sequences: change map-integers to require fixnum length.
[factor.git] / core / sequences / sequences.factor
1 ! Copyright (C) 2005, 2011 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel kernel.private math math.order
4 math.private slots.private ;
5 IN: sequences
6
7 MIXIN: sequence
8
9 GENERIC: length ( seq -- n ) flushable
10 GENERIC: set-length ( n seq -- )
11 GENERIC: nth ( n seq -- elt ) flushable
12 GENERIC: set-nth ( elt n seq -- )
13 GENERIC: new-sequence ( len seq -- newseq ) flushable
14 GENERIC: new-resizable ( len seq -- newseq ) flushable
15 GENERIC: like ( seq exemplar -- newseq ) flushable
16 GENERIC: clone-like ( seq exemplar -- newseq ) flushable
17
18 : new-like ( len exemplar quot -- seq )
19     over [ [ new-sequence ] dip call ] dip like ; inline
20
21 M: sequence like drop ; inline
22
23 GENERIC: lengthen ( n seq -- )
24 GENERIC: shorten ( n seq -- )
25
26 M: sequence lengthen 2dup length > [ set-length ] [ 2drop ] if ; inline
27 M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ; inline
28
29 : empty? ( seq -- ? ) length 0 = ; inline
30
31 : if-empty ( ..a seq quot1: ( ..a -- ..b ) quot2: ( ..a seq -- ..b ) -- ..b )
32     [ dup empty? ] [ [ drop ] prepose ] [ ] tri* if ; inline
33
34 : when-empty ( seq quot -- ) [ ] if-empty ; inline
35
36 : unless-empty ( seq quot -- ) [ ] swap if-empty ; inline
37
38 : delete-all ( seq -- ) 0 swap set-length ;
39
40 : first ( seq -- first ) 0 swap nth ; inline
41 : second ( seq -- second ) 1 swap nth ; inline
42 : third ( seq -- third ) 2 swap nth ; inline
43 : fourth ( seq -- fourth ) 3 swap nth ; inline
44
45 : set-first ( first seq -- ) 0 swap set-nth ; inline
46 : set-second ( second seq -- ) 1 swap set-nth ; inline
47 : set-third ( third seq -- ) 2 swap set-nth ; inline
48 : set-fourth  ( fourth seq -- ) 3 swap set-nth ; inline
49
50 : push ( elt seq -- ) [ length ] [ set-nth ] bi ;
51
52 ERROR: bounds-error index seq ;
53
54 GENERIC# bounds-check? 1 ( n seq -- ? )
55
56 M: integer bounds-check? ( n seq -- ? )
57     dupd length < [ 0 >= ] [ drop f ] if ; inline
58
59 : bounds-check ( n seq -- n seq )
60     2dup bounds-check? [ bounds-error ] unless ; inline
61
62 MIXIN: immutable-sequence
63
64 ERROR: immutable seq ;
65
66 M: immutable-sequence set-nth immutable ;
67
68 INSTANCE: immutable-sequence sequence
69
70 <PRIVATE
71
72 : array-nth ( n array -- elt )
73     swap 2 fixnum+fast slot ; inline
74
75 : set-array-nth ( elt n array -- )
76     swap 2 fixnum+fast set-slot ; inline
77
78 : dispatch ( n array -- ) array-nth call ;
79
80 GENERIC: resize ( n seq -- newseq ) flushable
81
82 ! Unsafe sequence protocol for inner loops
83 GENERIC: nth-unsafe ( n seq -- elt ) flushable
84 GENERIC: set-nth-unsafe ( elt n seq -- )
85
86 M: sequence nth bounds-check nth-unsafe ; inline
87 M: sequence set-nth bounds-check set-nth-unsafe ; inline
88
89 M: sequence nth-unsafe nth ; inline
90 M: sequence set-nth-unsafe set-nth ; inline
91
92 : change-nth-unsafe ( i seq quot -- )
93     [ [ nth-unsafe ] dip call ] 3keep drop set-nth-unsafe ; inline
94
95 PRIVATE>
96
97 ! The f object supports the sequence protocol trivially
98 M: f length drop 0 ; inline
99 M: f nth-unsafe nip ; inline
100 M: f like drop [ f ] when-empty ; inline
101
102 INSTANCE: f immutable-sequence
103
104 ! Integer sequences
105 TUPLE: iota { n integer read-only } ;
106
107 : iota ( n -- iota ) \ iota boa ; inline
108
109 M: iota length n>> ; inline
110 M: iota nth-unsafe drop ; inline
111
112 INSTANCE: iota immutable-sequence
113
114 <PRIVATE
115
116 : first-unsafe ( seq -- first ) 0 swap nth-unsafe ; inline
117 : second-unsafe ( seq -- second ) 1 swap nth-unsafe ; inline
118 : third-unsafe ( seq -- third ) 2 swap nth-unsafe ; inline
119 : fourth-unsafe ( seq -- fourth ) 3 swap nth-unsafe ; inline
120
121 : first2-unsafe ( seq -- first second )
122     [ first-unsafe ] [ second-unsafe ] bi ; inline
123
124 : first3-unsafe ( seq -- first second third )
125     [ first2-unsafe ] [ third-unsafe ] bi ; inline
126
127 : first4-unsafe ( seq -- first second third fourth )
128     [ first3-unsafe ] [ fourth-unsafe ] bi ; inline
129
130 : exchange-unsafe ( m n seq -- )
131     [ [ nth-unsafe ] curry bi@ ]
132     [ [ set-nth-unsafe ] curry bi@ ] 3bi ; inline
133
134 : (head) ( seq n -- from to seq ) [ 0 ] 2dip swap ; inline
135
136 : (tail) ( seq n -- from to seq ) swap [ length ] keep ; inline
137
138 : from-end ( seq n -- seq n' ) [ dup length ] dip - ; inline
139
140 : (1sequence) ( obj seq -- seq )
141     [ 0 swap set-nth-unsafe ] keep ; inline
142
143 : (2sequence) ( obj1 obj2 seq -- seq )
144     [ 1 swap set-nth-unsafe ] keep (1sequence) ; inline
145
146 : (3sequence) ( obj1 obj2 obj3 seq -- seq )
147     [ 2 swap set-nth-unsafe ] keep (2sequence) ; inline
148
149 : (4sequence) ( obj1 obj2 obj3 obj4 seq -- seq )
150     [ 3 swap set-nth-unsafe ] keep (3sequence) ; inline
151
152 PRIVATE>
153
154 : 1sequence ( obj exemplar -- seq )
155     1 swap [ (1sequence) ] new-like ; inline
156
157 : 2sequence ( obj1 obj2 exemplar -- seq )
158     2 swap [ (2sequence) ] new-like ; inline
159
160 : 3sequence ( obj1 obj2 obj3 exemplar -- seq )
161     3 swap [ (3sequence) ] new-like ; inline
162
163 : 4sequence ( obj1 obj2 obj3 obj4 exemplar -- seq )
164     4 swap [ (4sequence) ] new-like ; inline
165
166 : first2 ( seq -- first second )
167     1 swap bounds-check nip first2-unsafe ; inline
168
169 : first3 ( seq -- first second third )
170     2 swap bounds-check nip first3-unsafe ; inline
171
172 : first4 ( seq -- first second third fourth )
173     3 swap bounds-check nip first4-unsafe ; inline
174
175 : ?nth ( n seq -- elt/f )
176     2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; inline
177
178 : ?first ( seq -- elt/f ) 0 swap ?nth ; inline
179 : ?second ( seq -- elt/f ) 1 swap ?nth ; inline
180 : ?last ( seq -- elt/f )
181     [ length 1 - ] keep over 0 <
182     [ 2drop f ] [ nth-unsafe ] if ; inline
183
184 MIXIN: virtual-sequence
185 GENERIC: virtual-exemplar ( seq -- seq' )
186 GENERIC: virtual@ ( n seq -- n' seq' )
187
188 M: virtual-sequence nth virtual@ nth ; inline
189 M: virtual-sequence set-nth virtual@ set-nth ; inline
190 M: virtual-sequence nth-unsafe virtual@ nth-unsafe ; inline
191 M: virtual-sequence set-nth-unsafe virtual@ set-nth-unsafe ; inline
192 M: virtual-sequence like virtual-exemplar like ; inline
193 M: virtual-sequence new-sequence virtual-exemplar new-sequence ; inline
194
195 INSTANCE: virtual-sequence sequence
196
197 ! A reversal of an underlying sequence.
198 TUPLE: reversed { seq read-only } ;
199
200 C: <reversed> reversed
201
202 M: reversed virtual-exemplar seq>> ; inline
203 M: reversed virtual@ seq>> [ length swap - 1 - ] keep ; inline
204 M: reversed length seq>> length ; inline
205
206 INSTANCE: reversed virtual-sequence
207
208 ! A slice of another sequence.
209 TUPLE: slice
210 { from read-only }
211 { to read-only }
212 { seq read-only } ;
213
214 : collapse-slice ( m n slice -- m' n' seq )
215     [ from>> ] [ seq>> ] bi [ [ + ] curry bi@ ] dip ; inline
216
217 TUPLE: slice-error from to seq reason ;
218
219 : slice-error ( from to seq ? string -- from to seq )
220     [ \ slice-error boa throw ] curry when ; inline
221
222 : check-slice ( from to seq -- from to seq )
223     3dup
224     [ 2drop 0 < "start < 0" slice-error ]
225     [ [ drop ] 2dip length > "end > sequence" slice-error ]
226     [ drop > "start > end" slice-error ]
227     3tri ; inline
228
229 <PRIVATE
230
231 : <slice-unsafe> ( from to seq -- slice )
232     dup slice? [ collapse-slice ] when slice boa ; inline
233
234 PRIVATE>
235
236 : <slice> ( from to seq -- slice )
237     check-slice <slice-unsafe> ; inline
238
239 M: slice virtual-exemplar seq>> ; inline
240
241 M: slice virtual@ [ from>> + ] [ seq>> ] bi ; inline
242
243 M: slice length [ to>> ] [ from>> ] bi - ; inline
244
245 : short ( seq n -- seq n' ) over length min ; inline
246
247 : head-slice ( seq n -- slice ) (head) <slice> ; inline
248
249 : tail-slice ( seq n -- slice ) (tail) <slice> ; inline
250
251 : rest-slice ( seq -- slice ) 1 tail-slice ; inline
252
253 : head-slice* ( seq n -- slice ) from-end head-slice ; inline
254
255 : tail-slice* ( seq n -- slice ) from-end tail-slice ; inline
256
257 : but-last-slice ( seq -- slice ) 1 head-slice* ; inline
258
259 INSTANCE: slice virtual-sequence
260
261 ! One element repeated many times
262 TUPLE: repetition { len read-only } { elt read-only } ;
263
264 C: <repetition> repetition
265
266 M: repetition length len>> ; inline
267 M: repetition nth-unsafe nip elt>> ; inline
268
269 INSTANCE: repetition immutable-sequence
270
271 <PRIVATE
272
273 ERROR: integer-length-expected obj ;
274
275 : check-length ( n -- n )
276     dup integer? [ integer-length-expected ] unless ; inline
277
278 TUPLE: copy-state
279     { src-i read-only }
280     { src read-only }
281     { dst-i read-only }
282     { dst read-only } ;
283
284 C: <copy> copy-state
285
286 : ((copy)) ( n copy -- )
287     [ [ src-i>> + ] [ src>> ] bi nth-unsafe ]
288     [ [ dst-i>> + ] [ dst>> ] bi set-nth-unsafe ] 2bi ; inline
289
290 : (copy) ( n copy -- dst )
291     over 0 <= [ nip dst>> ] [ [ 1 - ] dip [ ((copy)) ] [ (copy) ] 2bi ] if ;
292     inline recursive
293
294 : subseq>copy ( from to seq -- n copy )
295     [ over - check-length swap ] dip
296     3dup nip new-sequence 0 swap <copy> ; inline
297
298 : bounds-check-head ( n seq -- n seq )
299     over 0 < [ bounds-error ] when ; inline
300
301 : check-copy ( src n dst -- src n dst )
302     3dup bounds-check-head
303     [ swap length + ] dip lengthen ; inline
304
305 PRIVATE>
306
307 : subseq ( from to seq -- subseq )
308     [ check-slice subseq>copy (copy) ] keep like ;
309
310 : head ( seq n -- headseq ) (head) subseq ;
311
312 : tail ( seq n -- tailseq ) (tail) subseq ;
313
314 : rest ( seq -- tailseq ) 1 tail ;
315
316 : head* ( seq n -- headseq ) from-end head ;
317
318 : tail* ( seq n -- tailseq ) from-end tail ;
319
320 : but-last ( seq -- headseq ) 1 head* ;
321
322 : copy ( src i dst -- )
323     #! The check-length call forces partial dispatch
324     [ [ length check-length 0 ] keep ] 2dip
325     check-copy <copy> (copy) drop ; inline
326
327 M: sequence clone-like
328     [ dup length ] dip new-sequence [ 0 swap copy ] keep ; inline
329
330 M: immutable-sequence clone-like like ; inline
331
332 : push-all ( src dst -- ) [ length ] [ copy ] bi ; inline
333
334 <PRIVATE
335
336 : (append) ( seq1 seq2 accum -- accum )
337     [ [ over length ] dip copy ]
338     [ 0 swap copy ]
339     [ ] tri ; inline
340
341 PRIVATE>
342
343 : append-as ( seq1 seq2 exemplar -- newseq )
344     [ 2dup [ length ] bi@ + ] dip
345     [ (append) ] new-like ; inline
346
347 : 3append-as ( seq1 seq2 seq3 exemplar -- newseq )
348     [ 3dup [ length ] tri@ + + ] dip [
349         [ [ 2over [ length ] bi@ + ] dip copy ]
350         [ (append) ] bi
351     ] new-like ; inline
352
353 : append ( seq1 seq2 -- newseq ) over append-as ;
354
355 : prepend-as ( seq1 seq2 exemplar -- newseq ) swapd append-as ; inline
356
357 : prepend ( seq1 seq2 -- newseq ) over prepend-as ;
358
359 : 3append ( seq1 seq2 seq3 -- newseq ) pick 3append-as ;
360
361 : surround ( seq1 seq2 seq3 -- newseq ) swapd 3append ; inline
362
363 : glue ( seq1 seq2 seq3 -- newseq ) swap 3append ; inline
364
365 : change-nth ( ..a i seq quot: ( ..a elt -- ..b newelt ) -- ..b )
366     [ [ nth ] dip call ] 3keep drop set-nth-unsafe ; inline
367
368 : min-length ( seq1 seq2 -- n ) [ length ] bi@ min ; inline
369
370 : max-length ( seq1 seq2 -- n ) [ length ] bi@ max ; inline
371
372 <PRIVATE
373
374 : ((each)) ( seq -- n quot )
375     [ length ] keep [ nth-unsafe ] curry ; inline
376
377 : (each) ( seq quot -- n quot' )
378     [ ((each)) ] dip compose ; inline
379
380 : (each-index) ( seq quot -- n quot' )
381     [ ((each)) [ keep ] curry ] dip compose ; inline
382
383 : (collect) ( quot into -- quot' )
384     [ [ keep ] dip set-nth-unsafe ] 2curry ; inline
385
386 : collect ( n quot into -- )
387     (collect) each-integer ; inline
388
389 : map-into ( seq quot into -- )
390     [ (each) ] dip collect ; inline
391
392 : 2nth-unsafe ( n seq1 seq2 -- elt1 elt2 )
393     [ nth-unsafe ] bi-curry@ bi ; inline
394
395 : ((2each)) ( seq1 seq2 -- n quot )
396     [ min-length ] 2keep [ 2nth-unsafe ] 2curry ; inline
397
398 : (2each) ( seq1 seq2 quot -- n quot' )
399     [ ((2each)) ] dip compose ; inline
400
401 : 3nth-unsafe ( n seq1 seq2 seq3 -- elt1 elt2 elt3 )
402     [ nth-unsafe ] tri-curry@ tri ; inline
403
404 : (3each) ( seq1 seq2 seq3 quot -- n quot' )
405     [
406         [ [ length ] tri@ min min ]
407         [ [ 3nth-unsafe ] 3curry ] 3bi
408     ] dip compose ; inline
409
410 : finish-find ( i seq -- i elt )
411     over [ dupd nth-unsafe ] [ drop f ] if ; inline
412
413 : (find) ( seq quot quot' -- i elt )
414     pick [ [ (each) ] dip call ] dip finish-find ; inline
415
416 : (find-from) ( n seq quot quot' -- i elt )
417     [ 2dup bounds-check? ] 2dip
418     [ (find) ] 2curry
419     [ 2drop f f ]
420     if ; inline
421
422 : (find-index) ( seq quot quot' -- i elt )
423     pick [ [ (each-index) ] dip call ] dip finish-find ; inline
424
425 : (find-index-from) ( n seq quot quot' -- i elt )
426     [ 2dup bounds-check? ] 2dip
427     [ (find-index) ] 2curry
428     [ 2drop f f ]
429     if ; inline
430
431 : (accumulate) ( seq identity quot -- identity seq quot )
432     swapd [ curry keep ] curry ; inline
433
434 PRIVATE>
435
436 : each ( ... seq quot: ( ... x -- ... ) -- ... )
437     (each) each-integer ; inline
438
439 : reduce ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
440     swapd each ; inline
441
442 : map-integers ( ... len quot: ( ... i -- ... elt ) exemplar -- ... newseq )
443     [ integer>fixnum-strict ] 2dip [ over ] dip
444     [ [ collect ] keep ] new-like ; inline
445
446 : map-as ( ... seq quot: ( ... elt -- ... newelt ) exemplar -- ... newseq )
447     [ (each) ] dip map-integers ; inline
448
449 : map ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
450     over map-as ; inline
451
452 : replicate-as ( ... len quot: ( ... -- ... newelt ) exemplar -- ... newseq )
453     [ [ drop ] prepose ] dip map-integers ; inline
454
455 : replicate ( ... len quot: ( ... -- ... newelt ) -- ... newseq )
456     { } replicate-as ; inline
457
458 : map! ( ... seq quot: ( ... elt -- ... newelt ) -- ... seq )
459     over [ map-into ] keep ; inline
460
461 : accumulate-as ( ... seq identity quot: ( ... prev elt -- ... next ) exemplar -- ... final newseq )
462     [ (accumulate) ] dip map-as ; inline
463
464 : accumulate ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final newseq )
465     pick accumulate-as ; inline
466
467 : accumulate! ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... final seq )
468     (accumulate) map! ; inline
469
470 : 2each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
471     (2each) each-integer ; inline
472
473 : 2reduce ( ... seq1 seq2 identity quot: ( ... prev elt1 elt2 -- ... next ) -- ... result )
474     [ -rot ] dip 2each ; inline
475
476 : 2map-as ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) exemplar -- ... newseq )
477     [ (2each) ] dip map-integers ; inline
478
479 : 2map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
480     pick 2map-as ; inline
481
482 : 2all? ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ? ) -- ... ? )
483     (2each) all-integers? ; inline
484
485 : 3each ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... ) -- ... )
486     (3each) each-integer ; inline
487
488 : 3map-as ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) exemplar -- ... newseq )
489     [ (3each) ] dip map-integers ; inline
490
491 : 3map ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) -- ... newseq )
492     [ pick ] dip swap 3map-as ; inline
493
494 : find-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
495     [ (find-integer) ] (find-from) ; inline
496
497 : find ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
498     [ find-integer ] (find) ; inline
499
500 : find-last-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
501     [ nip find-last-integer ] (find-from) ; inline
502
503 : find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
504     [ [ 1 - ] dip find-last-integer ] (find) ; inline
505
506 : find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt )
507     [ (find-integer) ] (find-index-from) ; inline
508
509 : find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
510     [ find-integer ] (find-index) ; inline
511
512 : all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
513     (each) all-integers? ; inline
514
515 : push-if ( ..a elt quot: ( ..a elt -- ..b ? ) accum -- ..b )
516     [ keep ] dip rot [ push ] [ 2drop ] if ; inline
517
518 : selector-for ( quot exemplar -- selector accum )
519     [ length ] keep new-resizable [ [ push-if ] 2curry ] keep ; inline
520
521 : selector ( quot -- selector accum )
522     V{ } selector-for ; inline
523
524 : filter-as ( ... seq quot: ( ... elt -- ... ? ) exemplar -- ... subseq )
525     dup [ selector-for [ each ] dip ] curry dip like ; inline
526
527 : filter ( ... seq quot: ( ... elt -- ... ? ) -- ... subseq )
528     over filter-as ; inline
529
530 : push-either ( ..a elt quot: ( ..a elt -- ..b ? ) accum1 accum2 -- ..b )
531     [ keep swap ] 2dip ? push ; inline
532
533 : 2selector ( quot -- selector accum1 accum2 )
534     V{ } clone V{ } clone [ [ push-either ] 3curry ] 2keep ; inline
535
536 : partition ( ... seq quot: ( ... elt -- ... ? ) -- ... trueseq falseseq )
537     over [ 2selector [ each ] 2dip ] dip [ like ] curry bi@ ; inline
538
539 : collector-for ( quot exemplar -- quot' vec )
540     [ length ] keep new-resizable [ [ push ] curry compose ] keep ; inline
541
542 : collector ( quot -- quot' vec )
543     V{ } collector-for ; inline
544
545 : produce-as ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) exemplar -- ..b seq )
546     dup [ collector-for [ while ] dip ] curry dip like ; inline
547
548 : produce ( ..a pred: ( ..a -- ..b ? ) quot: ( ..b -- ..a obj ) -- ..b seq )
549     { } produce-as ; inline
550
551 : follow ( ... obj quot: ( ... prev -- ... result/f ) -- ... seq )
552     [ dup ] swap [ keep ] curry produce nip ; inline
553
554 : each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
555     (each-index) each-integer ; inline
556
557 : interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )
558     pick empty? [ 3drop ] [
559         [ [ drop first-unsafe ] dip call ]
560         [ [ rest-slice ] 2dip [ bi* ] 2curry each ]
561         3bi
562     ] if ; inline
563
564 : map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
565     [ dup length iota ] dip 2map ; inline
566
567 : reduce-index ( ... seq identity quot: ( ... prev elt index -- ... next ) -- ... result )
568     swapd each-index ; inline
569
570 : index ( obj seq -- n )
571     [ = ] with find drop ;
572
573 : index-from ( obj i seq -- n )
574     rot [ = ] curry find-from drop ;
575
576 : last-index ( obj seq -- n )
577     [ = ] with find-last drop ;
578
579 : last-index-from ( obj i seq -- n )
580     rot [ = ] curry find-last-from drop ;
581
582 <PRIVATE
583
584 : (indices) ( elt i obj accum -- )
585     [ swap [ = ] dip ] dip [ push ] 2curry when ; inline
586
587 PRIVATE>
588
589 : indices ( obj seq -- indices )
590     swap V{ } clone
591     [ [ (indices) ] 2curry each-index ] keep ;
592
593 : nths ( indices seq -- seq' )
594     [ [ nth ] curry ] keep map-as ;
595
596 : any? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
597     find drop >boolean ; inline
598
599 : member? ( elt seq -- ? )
600     [ = ] with any? ;
601
602 : member-eq? ( elt seq -- ? )
603     [ eq? ] with any? ;
604
605 : remove ( elt seq -- newseq )
606     [ = not ] with filter ;
607
608 : remove-eq ( elt seq -- newseq )
609     [ eq? not ] with filter ;
610
611 : sift ( seq -- newseq )
612     [ ] filter ;
613
614 : harvest ( seq -- newseq )
615     [ empty? not ] filter ;
616
617 <PRIVATE
618
619 : mismatch-unsafe ( n seq1 seq2 -- i )
620     [ 2nth-unsafe = not ] 2curry find-integer ; inline
621
622 PRIVATE>
623
624 : mismatch ( seq1 seq2 -- i )
625     [ min-length ] 2keep mismatch-unsafe ; inline
626
627 M: sequence <=>
628     [ mismatch ] 2keep pick
629     [ 2nth-unsafe <=> ] [ [ length ] compare nip ] if ;
630
631 : sequence= ( seq1 seq2 -- ? )
632     2dup [ length ] bi@ dupd =
633     [ -rot mismatch-unsafe not ] [ 3drop f ] if ; inline
634
635 ERROR: assert-sequence got expected ;
636
637 : assert-sequence= ( a b -- )
638     2dup sequence= [ 2drop ] [ assert-sequence ] if ;
639
640 <PRIVATE
641
642 : sequence-hashcode-step ( oldhash newpart -- newhash )
643     integer>fixnum swap [
644         [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
645         fixnum+fast fixnum+fast
646     ] keep fixnum-bitxor ; inline
647
648 PRIVATE>
649
650 : sequence-hashcode ( n seq -- x )
651     [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
652
653 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
654
655 M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
656
657 : move ( to from seq -- )
658     2over =
659     [ 3drop ] [ [ nth swap ] [ set-nth ] bi ] if ; inline
660
661 <PRIVATE
662
663 : move-unsafe ( to from seq -- )
664     2over =
665     [ 3drop ] [ [ nth-unsafe swap ] [ set-nth-unsafe ] bi ] if ; inline
666
667 : (filter!) ( ... quot: ( ... elt -- ... ? ) store scan seq -- ... )
668     2dup length < [
669         [ move-unsafe ] 3keep
670         [ nth-unsafe pick call [ 1 + ] when ] 2keep
671         [ 1 + ] dip
672         (filter!)
673     ] [ nip set-length drop ] if ; inline recursive
674
675 PRIVATE>
676
677 : filter! ( ... seq quot: ( ... elt -- ... ? ) -- ... seq )
678     swap [ [ 0 0 ] dip (filter!) ] keep ; inline
679
680 : remove! ( elt seq -- seq )
681     [ = not ] with filter! ;
682
683 : remove-eq! ( elt seq -- seq )
684     [ eq? not ] with filter! ;
685
686 : prefix ( seq elt -- newseq )
687     over [ over length 1 + ] dip [
688         (1sequence) [ 1 swap copy ] keep
689     ] new-like ;
690
691 : suffix ( seq elt -- newseq )
692     over [ over length 1 + ] dip [
693         [ [ over length ] dip set-nth-unsafe ] keep
694         [ 0 swap copy ] keep
695     ] new-like ;
696
697 : suffix! ( seq elt -- seq ) over push ; inline
698
699 : append! ( seq1 seq2 -- seq1 ) over push-all ; inline
700
701 : last ( seq -- elt )
702     [ length 1 - ] keep
703     over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline
704
705 <PRIVATE
706
707 : last-unsafe ( seq -- elt ) [ length 1 - ] [ nth-unsafe ] bi ;
708
709 PRIVATE>
710
711 : set-last ( elt seq -- )
712     [ length 1 - ] keep
713     over 0 < [ bounds-error ] [ set-nth-unsafe ] if ; inline
714
715 : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ;
716
717 <PRIVATE
718
719 : move-backward ( shift from to seq -- )
720     2over = [
721         4drop
722     ] [
723         [ [ 2over + pick ] dip move-unsafe [ 1 + ] dip ] keep
724         move-backward
725     ] if ;
726
727 : move-forward ( shift from to seq -- )
728     2over = [
729         4drop
730     ] [
731         [ [ pick [ dup dup ] dip + swap ] dip move-unsafe 1 - ] keep
732         move-forward
733     ] if ;
734
735 : (open-slice) ( shift from to seq ? -- )
736     [
737         [ [ 1 - ] bi@ ] dip move-forward
738     ] [
739         [ over - ] 2dip move-backward
740     ] if ;
741
742 : open-slice ( shift from seq -- )
743     pick 0 = [
744         3drop
745     ] [
746         pick over length + over
747         [ pick 0 > [ [ length ] keep ] dip (open-slice) ] 2dip
748         set-length
749     ] if ;
750
751 PRIVATE>
752
753 : delete-slice ( from to seq -- )
754     check-slice [ over [ - ] dip ] dip open-slice ;
755
756 : remove-nth! ( n seq -- seq )
757     [ [ dup 1 + ] dip delete-slice ] keep ;
758
759 : snip ( from to seq -- head tail )
760     [ swap head ] [ swap tail ] bi-curry bi* ; inline
761
762 : snip-slice ( from to seq -- head tail )
763     [ swap head-slice ] [ swap tail-slice ] bi-curry bi* ; inline
764
765 : replace-slice ( new from to seq -- seq' )
766     snip-slice surround ;
767
768 : remove-nth ( n seq -- seq' )
769     [ [ { } ] dip dup 1 + ] dip replace-slice ;
770
771 : pop ( seq -- elt )
772     [ length 1 - ] keep over 0 >=
773     [ [ nth-unsafe ] [ shorten ] 2bi ]
774     [ bounds-error ] if ;
775
776 : exchange ( m n seq -- )
777     [ nip bounds-check 2drop ]
778     [ bounds-check 3drop ]
779     [ exchange-unsafe ]
780     3tri ;
781
782 : midpoint@ ( seq -- n ) length 2/ ; inline
783
784 : reverse! ( seq -- seq )
785     [
786         [ midpoint@ iota ] [ length ] [ ] tri
787         [ [ over - 1 - ] dip exchange-unsafe ] 2curry each
788     ] keep ;
789
790 : reverse ( seq -- newseq )
791     [
792         dup [ length ] keep new-sequence
793         [ 0 swap copy ] keep reverse!
794     ] keep like ;
795
796 : sum-lengths ( seq -- n )
797     0 [ length + ] reduce ;
798
799 : concat-as ( seq exemplar -- newseq )
800     swap [ { } ] [
801         [ sum-lengths over new-resizable ] keep
802         [ append! ] each
803     ] if-empty swap like ;
804
805 : concat ( seq -- newseq )
806     [ { } ] [ dup first concat-as ] if-empty ;
807
808 <PRIVATE
809
810 : joined-length ( seq glue -- n )
811     [ [ sum-lengths ] [ length 1 [-] ] bi ] dip length * + ;
812
813 PRIVATE>
814
815 : join ( seq glue -- newseq )
816     dup empty? [ concat-as ] [
817         [
818             2dup joined-length over new-resizable [
819                 [ [ push-all ] 2curry ] [ [ nip push-all ] 2curry ] 2bi
820                 interleave
821             ] keep
822         ] keep like
823     ] if ;
824
825 : padding ( ... seq n elt quot: ( ... seq1 seq2 -- ... newseq ) -- ... newseq )
826     [
827         [ over length [-] dup 0 = [ drop ] ] dip
828         [ <repetition> ] curry
829     ] dip compose if ; inline
830
831 : pad-head ( seq n elt -- padded )
832     [ swap dup append-as ] padding ;
833
834 : pad-tail ( seq n elt -- padded )
835     [ append ] padding ;
836
837 : shorter? ( seq1 seq2 -- ? ) [ length ] bi@ < ;
838
839 : head? ( seq begin -- ? )
840     2dup shorter? [
841         2drop f
842     ] [
843         [ nip ] [ length head-slice ] 2bi sequence=
844     ] if ;
845
846 : tail? ( seq end -- ? )
847     2dup shorter? [
848         2drop f
849     ] [
850         [ nip ] [ length tail-slice* ] 2bi sequence=
851     ] if ;
852
853 : cut-slice ( seq n -- before-slice after-slice )
854     [ head-slice ] [ tail-slice ] 2bi ; inline
855
856 : insert-nth ( elt n seq -- seq' )
857     swap cut-slice [ swap suffix ] dip append ;
858
859 : halves ( seq -- first-slice second-slice )
860     dup midpoint@ cut-slice ; inline
861
862 <PRIVATE
863
864 : nth2-unsafe ( n seq -- a b )
865     [ nth-unsafe ] [ [ 1 + ] dip nth-unsafe ] 2bi ; inline
866
867 : nth3-unsafe ( n seq -- a b c )
868     [ nth2-unsafe ] [ [ 2 + ] dip nth-unsafe ] 2bi ; inline
869
870 : (binary-reduce) ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) from length -- ... value )
871     #! We can't use case here since combinators depends on
872     #! sequences
873     dup 4 < [
874         integer>fixnum {
875             [ 2drop nip ]
876             [ 2nip swap nth-unsafe ]
877             [ -rot [ drop swap nth2-unsafe ] dip call ]
878             [ -rot [ drop swap nth3-unsafe ] dip bi@ ]
879         } dispatch
880     ] [
881         [ 2/ ] [ over - ] bi [ 2dup + ] dip
882         [ (binary-reduce) ] [ 2curry ] curry 2bi@
883         pick [ 3bi ] dip call
884     ] if ; inline recursive
885
886 PRIVATE>
887
888 : binary-reduce ( ... seq start quot: ( ... elt1 elt2 -- ... newelt ) -- ... value )
889     pick length 0 max 0 swap (binary-reduce) ; inline
890
891 : cut ( seq n -- before after )
892     [ head ] [ tail ] 2bi ;
893
894 : cut* ( seq n -- before after )
895     [ head* ] [ tail* ] 2bi ;
896
897 <PRIVATE
898
899 : (start) ( subseq seq n -- subseq seq ? )
900     pick length iota [
901         [ 3dup ] dip [ + swap nth-unsafe ] keep rot nth-unsafe =
902     ] all? nip ; inline
903
904 PRIVATE>
905
906 : start* ( subseq seq n -- i )
907     pick length pick length swap - 1 + iota
908     [ (start) ] find-from
909     swap [ 3drop ] dip ;
910
911 : start ( subseq seq -- i ) 0 start* ; inline
912
913 : subseq? ( subseq seq -- ? ) start >boolean ;
914
915 : drop-prefix ( seq1 seq2 -- slice1 slice2 )
916     2dup mismatch [ 2dup min-length ] unless*
917     [ tail-slice ] curry bi@ ;
918
919 : unclip ( seq -- rest first )
920     [ rest ] [ first-unsafe ] bi ;
921
922 : unclip-last ( seq -- butlast last )
923     [ but-last ] [ last-unsafe ] bi ;
924
925 : unclip-slice ( seq -- rest-slice first )
926     [ rest-slice ] [ first-unsafe ] bi ; inline
927
928 : map-reduce ( ..a seq map-quot: ( ..a elt -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
929     [ [ dup first ] dip [ call ] keep ] dip compose
930     swapd [ 1 ] 2dip (each) (each-integer) ; inline
931
932 : 2map-reduce ( ..a seq1 seq2 map-quot: ( ..a elt1 elt2 -- ..b intermediate ) reduce-quot: ( ..b prev intermediate -- ..a next ) -- ..a result )
933     [ [ 2dup [ first ] bi@ ] dip [ call ] keep ] dip compose
934     [ -rot ] dip [ 1 ] 3dip (2each) (each-integer) ; inline
935
936 <PRIVATE
937
938 : (map-find) ( seq quot find-quot -- result elt )
939     [ [ f ] 2dip [ [ nip ] dip call dup ] curry ] dip call
940     [ [ drop f ] unless ] dip ; inline
941
942 PRIVATE>
943
944 : map-find ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
945     [ find ] (map-find) ; inline
946
947 : map-find-last ( ... seq quot: ( ... elt -- ... result/f ) -- ... result elt )
948     [ find-last ] (map-find) ; inline
949
950 : unclip-last-slice ( seq -- butlast-slice last )
951     [ but-last-slice ] [ last-unsafe ] bi ; inline
952
953 <PRIVATE
954
955 : (trim-head) ( seq quot -- seq n )
956     over [ [ not ] compose find drop ] dip
957     [ length or ] keep swap ; inline
958
959 : (trim-tail) ( seq quot -- seq n )
960     over [ [ not ] compose find-last drop ?1+ ] dip
961     swap ; inline
962
963 PRIVATE>
964
965 : trim-head-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
966     (trim-head) tail-slice ; inline
967
968 : trim-head ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
969     (trim-head) tail ; inline
970
971 : trim-tail-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
972     (trim-tail) head-slice ; inline
973
974 : trim-tail ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
975     (trim-tail) head ; inline
976
977 : trim-slice ( ... seq quot: ( ... elt -- ... ? ) -- ... slice )
978     [ trim-head-slice ] [ trim-tail-slice ] bi ; inline
979
980 : trim ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
981     [ trim-slice ] [ drop ] 2bi like ; inline
982
983 GENERIC: sum ( seq -- n )
984 M: object sum 0 [ + ] binary-reduce ; inline
985
986 : product ( seq -- n ) 1 [ * ] binary-reduce ;
987
988 : infimum ( seq -- n ) [ ] [ min ] map-reduce ;
989
990 : supremum ( seq -- n ) [ ] [ max ] map-reduce ;
991
992 : map-sum ( ... seq quot: ( ... elt -- ... n ) -- ... n )
993     [ 0 ] 2dip [ dip + ] curry [ swap ] prepose each ; inline
994
995 : count ( ... seq quot: ( ... elt -- ... ? ) -- ... n )
996     [ 1 0 ? ] compose map-sum ; inline
997
998 : cartesian-each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
999     [ with each ] 2curry each ; inline
1000
1001 : cartesian-map ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... newelt ) -- ... newseq )
1002     [ with map ] 2curry map ; inline
1003
1004 : cartesian-product ( seq1 seq2 -- newseq )
1005     [ { } 2sequence ] cartesian-map ;
1006
1007 ! We hand-optimize flip to such a degree because type hints
1008 ! cannot express that an array is an array of arrays yet, and
1009 ! this word happens to be performance-critical since the compiler
1010 ! itself uses it. Optimizing it like this reduced compile time.
1011 <PRIVATE
1012
1013 : generic-flip ( matrix -- newmatrix )
1014     [
1015         [ first-unsafe length 1 ] keep
1016         [ length min ] (each) (each-integer) iota
1017     ] keep
1018     [ [ nth-unsafe ] with { } map-as ] curry { } map-as ; inline
1019
1020 USE: arrays
1021
1022 : array-length ( array -- len )
1023     { array } declare length>> ; inline
1024
1025 : array-flip ( matrix -- newmatrix )
1026     { array } declare
1027     [
1028         [ first-unsafe array-length 1 ] keep
1029         [ array-length min ] (each) (each-integer) iota
1030     ] keep
1031     [ [ { array } declare array-nth ] with { } map-as ] curry { } map-as ;
1032
1033 PRIVATE>
1034
1035 : flip ( matrix -- newmatrix )
1036     dup empty? [
1037         dup array? [
1038             dup [ array? ] all?
1039             [ array-flip ] [ generic-flip ] if
1040         ] [ generic-flip ] if
1041     ] unless ;