]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/sequences/extras/extras.factor
sequences.extras: Add a word to ensure that the underlying for slices is the same...
[factor.git] / extra / sequences / extras / extras.factor
index 7c3caa7b4a91e44c6fd23e986b1922992e23944b..55f24785a1b37cd8aeb8ec7588c1e624d546e53c 100644 (file)
@@ -198,6 +198,16 @@ ERROR: slices-don't-touch slice1 slice2 ;
 : rotate ( seq n -- seq' )
     over length mod dup 0 >= [ cut ] [ abs cut* ] if prepend ;
 
+ERROR: underlying-mismatch slice1 slice2 ;
+: ensure-same-underlying ( slice1 slice2 -- slice1 slice2 )
+    2dup [ seq>> ] bi@ eq? [ underlying-mismatch ] unless ;
+
+: span-slices ( slice1 slice2 -- slice )
+    ensure-same-underlying
+    [ [ from>> ] bi@ min ]
+    [ [ to>> ] bi@ max ]
+    [ drop seq>> ] 2tri <slice> ;
+
 :: rotate! ( seq n -- )
     seq length :> len
     n len mod dup 0 < [ len + ] when seq bounds-check drop 0 over