From: Doug Coleman Date: Sat, 25 Feb 2023 04:57:35 +0000 (-0600) Subject: sequences.extras: move ??nth X-Git-Tag: 0.99~524 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=08b2744c3f35f64380d2bfdbef73a223b369294a sequences.extras: move ??nth --- diff --git a/extra/sequences/extras/extras-docs.factor b/extra/sequences/extras/extras-docs.factor index f1a2d822c4..c9944b4dd4 100644 --- a/extra/sequences/extras/extras-docs.factor +++ b/extra/sequences/extras/extras-docs.factor @@ -1074,6 +1074,10 @@ HELP: nth? } { $description "Check if the nth element of " { $snippet "seq" } " satisfies the condition given by " { $snippet "quot" } "." } ; +HELP: ??nth +{ $values { "n" integer } { "seq" sequence } { "elt/f" { $maybe object } } { "?" boolean } } +{ $description "A forgiving version of " { $link nth } ". If the index is out of bounds, or if the sequence is " { $link f } ", simply outputs " { $link f } ". Also outputs a boolean to distinguish between the sequence containing an " { $link f } " or an out of bounds index." } ; + HELP: odd-indices { $values { "seq" sequence } diff --git a/extra/sequences/extras/extras.factor b/extra/sequences/extras/extras.factor index 5c8b8e4edc..ef66d0652f 100644 --- a/extra/sequences/extras/extras.factor +++ b/extra/sequences/extras/extras.factor @@ -333,6 +333,8 @@ PRIVATE> : nth-of ( seq n -- elt ) swap nth ; inline : set-nth-of ( seq n elt -- seq ) spin [ set-nth ] keep ; inline : ?nth-of ( seq n -- elt/f ) swap ?nth ; inline +: ??nth ( n seq -- elt/f ? ) + 2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; inline : ??nth-of ( seq n -- elt/f ? ) swap ??nth ; inline : reduce-of ( seq quot: ( prev elt -- next ) identity -- result )