From 49df807568bbd7455e8c3440c4ade03ea5be752a Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sun, 31 Jul 2022 13:21:38 -0500 Subject: [PATCH] sequences: add index-of-last --- basis/documents/documents.factor | 2 +- basis/ini-file/ini-file.factor | 2 +- basis/json/reader/reader.factor | 2 +- basis/math/combinatorics/combinatorics.factor | 2 +- basis/persistent/vectors/vectors.factor | 2 +- core/assocs/assocs.factor | 2 +- core/io/pathnames/pathnames.factor | 2 +- core/sequences/sequences.factor | 12 +++++++----- extra/gap-buffer/gap-buffer.factor | 2 +- extra/sequences/extras/extras.factor | 2 +- extra/tensors/tensors.factor | 2 +- 11 files changed, 17 insertions(+), 15 deletions(-) diff --git a/basis/documents/documents.factor b/basis/documents/documents.factor index 65a20ab66b..6e02a734ca 100644 --- a/basis/documents/documents.factor +++ b/basis/documents/documents.factor @@ -88,7 +88,7 @@ CONSTANT: doc-start { 0 0 } 0 swap [ append ] change-nth ; : append-last ( str seq -- ) - [ length 1 - ] keep [ prepend ] change-nth ; + index-of-last [ prepend ] change-nth ; : loc-col/str ( loc document -- str col ) [ first2 swap ] dip nth swap ; diff --git a/basis/ini-file/ini-file.factor b/basis/ini-file/ini-file.factor index 8254b4059e..090c97efe0 100644 --- a/basis/ini-file/ini-file.factor +++ b/basis/ini-file/ini-file.factor @@ -68,7 +68,7 @@ IN: ini-file [ space? ] trim ; : unwrap ( str -- str' ) - 1 swap [ length 1 - ] keep subseq ; + 1 swap index-of-last subseq ; : uncomment ( str -- str' ) ";#" [ over index [ head ] when* ] each ; diff --git a/basis/json/reader/reader.factor b/basis/json/reader/reader.factor index b47fada6ba..a71a36ac09 100644 --- a/basis/json/reader/reader.factor +++ b/basis/json/reader/reader.factor @@ -70,7 +70,7 @@ DEFER: (read-json-string) [ length 2 - ] [ nth-unsafe ] bi ; inline : pop-unsafe ( seq -- elt ) - [ length 1 - ] keep [ nth-unsafe ] [ shorten ] 2bi ; inline + index-of-last [ nth-unsafe ] [ shorten ] 2bi ; inline : check-length ( seq n -- seq ) [ dup length ] [ >= ] bi* [ json-error ] unless ; inline diff --git a/basis/math/combinatorics/combinatorics.factor b/basis/math/combinatorics/combinatorics.factor index 0b8713cc6b..ed3af83d7c 100644 --- a/basis/math/combinatorics/combinatorics.factor +++ b/basis/math/combinatorics/combinatorics.factor @@ -270,7 +270,7 @@ INSTANCE: combinations immutable-sequence [ drop 1 + dup ] map! 2drop ; inline : increment-last ( seq -- ) - [ [ length 1 - ] keep [ 1 + ] change-nth-unsafe ] unless-empty ; inline + [ index-of-last [ 1 + ] change-nth-unsafe ] unless-empty ; inline :: next-combination ( seq n -- seq ) seq n find-max-index [ diff --git a/basis/persistent/vectors/vectors.factor b/basis/persistent/vectors/vectors.factor index 8910e99d3c..b0aba0590b 100644 --- a/basis/persistent/vectors/vectors.factor +++ b/basis/persistent/vectors/vectors.factor @@ -63,7 +63,7 @@ M: persistent-vector nth-unsafe dup full? [ [ level>> 1node ] keep swap ] [ node-add f ] if ; : new-last ( val seq -- seq' ) - [ length 1 - ] keep new-nth ; + index-of-last new-nth ; : node-set-last ( child node -- node' ) clone [ new-last ] change-children ; diff --git a/core/assocs/assocs.factor b/core/assocs/assocs.factor index 2f661e7481..bb9fd94c38 100644 --- a/core/assocs/assocs.factor +++ b/core/assocs/assocs.factor @@ -142,7 +142,7 @@ M: assoc values [ nip ] { } assoc>map ; assoc-size 0 = ; inline : assoc-stack ( key seq -- value ) - [ length 1 - ] keep assoc-stack-from ; flushable + index-of-last assoc-stack-from ; flushable : assoc-subset? ( assoc1 assoc2 -- ? ) [ at* [ = ] [ 2drop f ] if ] with-assoc assoc-all? ; diff --git a/core/io/pathnames/pathnames.factor b/core/io/pathnames/pathnames.factor index 64bc3f762a..d181b5c97a 100644 --- a/core/io/pathnames/pathnames.factor +++ b/core/io/pathnames/pathnames.factor @@ -17,7 +17,7 @@ SYMBOL: current-directory [ path-separator? ] trim-head ; : last-path-separator ( path -- n ? ) - [ length 1 - ] keep [ path-separator? ] find-last-from ; + index-of-last [ path-separator? ] find-last-from ; HOOK: root-directory? io-backend ( path -- ? ) diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index d9825b8b99..fe8b28cbfd 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -185,7 +185,7 @@ PRIVATE> : ?first ( seq -- elt/f ) 0 swap ?nth ; inline : ?second ( seq -- elt/f ) 1 swap ?nth ; inline : ?last ( seq -- elt/f ) - [ length 1 - ] keep over 0 < + index-of-last over 0 < [ 2drop f ] [ nth-unsafe ] if ; inline MIXIN: virtual-sequence @@ -249,6 +249,8 @@ M: slice length [ to>> ] [ from>> ] bi - ; inline : index-or-length ( seq n -- seq n' ) over length min ; inline +: index-of-last ( seq -- n seq ) [ length 1 - ] keep ; inline + : head-slice ( seq n -- slice ) head-to-index ; inline : tail-slice ( seq n -- slice ) index-to-tail ; inline @@ -572,7 +574,7 @@ PRIVATE> '[ _ find-last-from-unsafe ] bounds-check-find ; inline : find-last ( ... seq quot: ( ... elt -- ... ? ) -- ... i elt ) - [ [ length 1 - ] keep ] dip find-last-from ; inline + [ index-of-last ] dip find-last-from ; inline : find-index-from ( ... n seq quot: ( ... elt i -- ... ? ) -- ... i elt ) '[ @@ -817,7 +819,7 @@ PRIVATE> : append! ( seq1 seq2 -- seq1 ) over push-all ; inline : last ( seq -- elt ) - [ length 1 - ] keep + index-of-last over 0 < [ bounds-error ] [ nth-unsafe ] if ; inline PRIVATE> : set-last ( elt seq -- ) - [ length 1 - ] keep + index-of-last over 0 < [ bounds-error ] [ set-nth-unsafe ] if ; inline : pop* ( seq -- ) [ length 1 - ] [ shorten ] bi ; @@ -888,7 +890,7 @@ PRIVATE> [ [ dup 1 + ] dip snip-slice ] keep append-as ; : pop ( seq -- elt ) - [ length 1 - ] keep over 0 >= + index-of-last over 0 >= [ [ nth-unsafe ] [ shorten ] 2bi ] [ bounds-error ] if ; diff --git a/extra/gap-buffer/gap-buffer.factor b/extra/gap-buffer/gap-buffer.factor index c4f1cf3d91..dc3cd28740 100644 --- a/extra/gap-buffer/gap-buffer.factor +++ b/extra/gap-buffer/gap-buffer.factor @@ -262,7 +262,7 @@ M: sequence insert* : pop-start ( gb -- elem ) 0 swap pop-elem ; -: pop-end ( gb -- elem ) [ length 1 - ] keep pop-elem ; +: pop-end ( gb -- elem ) index-of-last pop-elem ; : rotate-right ( gb -- ) dup [ pop-end ] keep push-start drop ; diff --git a/extra/sequences/extras/extras.factor b/extra/sequences/extras/extras.factor index e26bbb2ad7..7c6bc05d0c 100644 --- a/extra/sequences/extras/extras.factor +++ b/extra/sequences/extras/extras.factor @@ -655,7 +655,7 @@ PRIVATE> ] bounds-check-find ; inline : find-last-index ( ... seq quot: ( ... elt i -- ... ? ) -- ... i elt ) - [ [ length 1 - ] keep ] dip find-last-index-from ; inline + [ index-of-last ] dip find-last-index-from ; inline : map-find-last-index ( ... seq quot: ( ... elt index -- ... result/f ) -- ... result i elt ) [ find-last-index ] (map-find-index) ; inline diff --git a/extra/tensors/tensors.factor b/extra/tensors/tensors.factor index ef2bc0ae70..157a82e6a3 100644 --- a/extra/tensors/tensors.factor +++ b/extra/tensors/tensors.factor @@ -465,7 +465,7 @@ PRIVATE> ! Compute the starting index / truncate dupd * ! Compute the ending index - swap over + + tuck + ! Take a slice rot ; -- 2.34.1