sequences.extras: adding nth-index
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 21 Aug 2022 16:16:34 +0000 (09:16 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 21 Aug 2022 16:16:34 +0000 (09:16 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index 430e82ea3336781d350a162b500ace5af2b69205..53104befe7033f88fa843fa2a16b4dfef3bd71b0 100644 (file)
@@ -322,4 +322,9 @@ math prettyprint sequences sequences.extras strings tools.test ;
 
 { { 5 11 26 61 } } [
     V{ 5 16 42 103 } [ - ] { } map-prior-as
-] unit-test
\ No newline at end of file
+] unit-test
+
+{ f } [ 0 CHAR: a "foo" nth-index ] unit-test
+{ 0 } [ 0 CHAR: a "abba" nth-index ] unit-test
+{ 3 } [ 1 CHAR: a "abba" nth-index ] unit-test
+{ f } [ 2 CHAR: a "abba" nth-index ] unit-test
index f47ebc56508934065d8e4552fe9df11cd3efa84e..027a3af19969716904c232efaf834c26190bfbdf 100644 (file)
@@ -823,3 +823,6 @@ M: virtual-zip-index nth-unsafe
     over [ seq>> nth-unsafe ] [ 2array ] bi* ; inline
 
 INSTANCE: virtual-zip-index immutable-sequence
+
+: nth-index ( n obj seq -- i )
+    [ = dup [ drop 1 - dup 0 < ] when ] with find drop nip ;