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