 : ?set-nth ( elt n seq -- )
     2dup bounds-check? [ set-nth-unsafe ] [ 3drop ] if ; inline
 
+: maybe-nth ( i/f seq -- elt/f )
+    over [ nth ] [ 2drop f ] if ; inline
+
 : ?first ( seq -- elt/f ) 0 swap ?nth ; inline
 : ?second ( seq -- elt/f ) 1 swap ?nth ; inline
 : ?last ( seq -- elt/f )

: (3each) ( seq1 seq2 seq3 quot -- n quot' )
    [ setup-3each ] dip compose ; inline

: finish-find ( i seq -- i elt )
    over [ dupd nth-unsafe ] [ drop f ] if ; inline

: (find) ( seq quot quot' -- i elt )
    pick [ [ length-operator ] dip call ] dip finish-find ; inline

: (find-from) ( n seq quot quot' -- i elt )
    [ 2dup bounds-check? ] 2dip
    '[ _ _ (find) ] [ 2drop f f ] if ; inline

: (find-index) ( seq quot quot' -- i elt )
    pick [ [ sequence-index-operator ] dip call ] dip finish-find ; inline

: (find-index-from) ( n seq quot quot' -- i elt )
    [ 2dup bounds-check? ] 2dip
    '[ _ _ (find-index) ] [ 2drop f f ] if ; inline

: (accumulate) ( seq identity quot -- identity seq quot' )
    swapd [ keepd ] curry ; inline

: 3map ( ... seq1 seq2 seq3 quot: ( ... elt1 elt2 elt3 -- ... newelt ) -- ... newseq )
    pickd swap 3map-as ; inline

: bounds-check-find ( n seq quot -- elt i )
    2over bounds-check? [ call ] [ 3drop f f ] if ; inline

: find-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
    '[
        _ [ rot sequence-operator-from find-integer-from ] keepd
        index/element
    ] bounds-check-find ; inline

: find ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
    [ 0 ] 2dip find-from ; inline

: find-last-from ( ... n seq quot: ( ... elt -- ... ? ) -- ... i elt )
    '[
        _ [ rot sequence-operator-last-from find-last-integer ] keepd
        index/element
    ] bounds-check-find ; inline

: find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt )
    [ [ length 1 - ] keep ] dip find-last-from ; inline

: find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt )
    '[
        _ [ sequence-index-operator find-integer-from ] keepd
        index/element
    ] bounds-check-find ; inline

: find-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt )
    [ 0 ] 2dip find-index-from ; inline

: all? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
    sequence-operator all-integers-from? ; inline
