     over length - '[ _ + >= ] find-index drop ; inline
 
 : increment-rest ( i seq -- )
-    [ nth ] [ swap tail-slice ] 2bi
+    [ nth-unsafe ] [ swap (tail) <slice-unsafe> ] 2bi
     [ drop 1 + dup ] map! 2drop ; inline
 
 : increment-last ( seq -- )
-    [ [ length 1 - ] keep [ 1 + ] change-nth ] unless-empty ; inline
+    [ [ length 1 - ] keep [ 1 + ] change-nth-unsafe ] unless-empty ; inline
 
 :: next-combination ( seq n -- seq )
     seq n find-max-index [

<PRIVATE

: should-swap? ( start curr seq -- ? )
    [ nipd nth ] [ <slice> member? not ] 3bi ; inline

:: unique-permutations ( ... seq i n quot: ( ... elt -- ... ) -- ... )
    i n >= [

    '[ dup _ >= [ 1 + ] when ] map! drop ; inline

: (>permutation) ( seq n index -- seq )
    swap [ dupd (head) <slice-unsafe> ] dip bump-indices ;

: >permutation ( factoradic -- permutation )
    reverse! dup [ (>permutation) ] each-index reverse! ;

: permutation-indices ( n seq -- permutation )
    length [ factoradic ] dip 0 pad-head >permutation ;

PRIVATE>

