]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: move ??nth
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 25 Feb 2023 04:57:35 +0000 (22:57 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 26 Feb 2023 23:11:04 +0000 (17:11 -0600)
extra/sequences/extras/extras-docs.factor
extra/sequences/extras/extras.factor

index f1a2d822c41988e4a10303ecc8a9797bd7dbdd1a..c9944b4dd4a7ac35e4c785ede2b4971e4aacaf3d 100644 (file)
@@ -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 }
index 5c8b8e4edcb6eb5997445fc42a1aea9de674ec84..ef66d0652f7a73d8f1f916ace0f3f8f7808cdfb2 100644 (file)
@@ -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 )