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