]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: adding nth* which is nth from the end.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 12 Sep 2013 19:47:04 +0000 (12:47 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 12 Sep 2013 19:47:04 +0000 (12:47 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index 0ee8e5242f096afed9277be8c16dbcdb3c21be34..d01f60752edee369e741d863cbe8a00219e414b9 100644 (file)
@@ -169,3 +169,5 @@ IN: sequences.extras.tests
 { "foo" " " } [ "foo " [ blank? ] cut-when ] unit-test
 { "" " foo" } [ " foo" [ blank? ] cut-when ] unit-test
 { "foo" " bar" } [ "foo bar" [ blank? ] cut-when ] unit-test
+
+{ { 4 0 3 1 2 } } [ { 0 4 1 3 2 } 5 iota [ nth* ] curry map ] unit-test
index fc6e90744a4ad044ae80f733b029dd929bce5069..03b1fcda5c058fb676f899cba5cdfe9c6132d499 100644 (file)
@@ -486,3 +486,6 @@ PRIVATE>
 : cut-when ( ... seq quot: ( ... elt -- ... ? ) -- ... before after )
     [ find drop ] 2keep drop swap
     [ cut ] [ f over like ] if* ; inline
+
+: nth* ( n seq -- elt )
+    [ length 1 - swap - ] [ nth ] bi ; inline