]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: adding "none?" and "one?".
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 6 Aug 2012 15:52:45 +0000 (08:52 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 6 Aug 2012 15:52:45 +0000 (08:52 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index 70c23a1896c6ee349675a61bd0c38976ff1c33b2..47e8dc87094b78e223494b31e3e882a2acfda056 100644 (file)
@@ -100,3 +100,11 @@ IN: sequences.extras.tests
 { "" } [ "" "" "" unsurround ] unit-test
 { "" } [ "  " " " " " unsurround ] unit-test
 { "foo.com" } [ "http://foo.com" "http://" "/" unsurround ] unit-test
+
+{ t } [ { 1 3 5 7 } [ even? ] none? ] unit-test
+{ f } [ { 1 2 3 4 } [ even? ] none? ] unit-test
+{ t } [ { } [ even? ] none? ] unit-test
+
+{ f } [ { 1 2 3 4 } [ even? ] one? ] unit-test
+{ t } [ { 1 2 3 } [ even? ] one? ] unit-test
+{ f } [ { } [ even? ] one? ] unit-test
index 9c2ab19083e059e33f8722902e83bba61a0a9d91..6833ceed387c833623b09e5a520edca059232c0c 100644 (file)
@@ -216,3 +216,11 @@ PRIVATE>
 
 : unsurround ( newseq seq2 seq3 -- seq1 )
    [ ?head drop ] [ ?tail drop ] bi* ;
+
+: none? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
+    any? not ; inline
+
+: one? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
+    [ find ] 2keep rot [
+        [ 1 + ] 2dip find-from drop not
+    ] [ 3drop f ] if ; inline