]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: Add first=, first?, etc
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 10 Sep 2012 19:10:33 +0000 (12:10 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 10 Sep 2012 19:10:33 +0000 (12:10 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index f4de100d9785459dc9355e82bba35449577b9690..754d93788b7f08a0db597f64cbca88f087c21cb1 100644 (file)
@@ -123,3 +123,17 @@ IN: sequences.extras.tests
 
 { 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
 { 0 } [ { 1 7 3 7 6 3 7 } arg-min ] unit-test
+
+{ t } [ { 1 2 3 4 5 } 1 first= ] unit-test
+{ t } [ { 1 2 3 4 5 } 2 second= ] unit-test
+{ t } [ { 1 2 3 4 5 } 3 third= ] unit-test
+{ t } [ { 1 2 3 4 5 } 4 fourth= ] unit-test
+{ t } [ { 1 2 3 4 5 } 5 last= ] unit-test
+{ t } [ 4 { 1 2 3 4 5 } 5 nth= ] unit-test
+
+{ t } [ { 1 2 3 4 5 } [ 1 = ] first? ] unit-test
+{ t } [ { 1 2 3 4 5 } [ 2 = ] second? ] unit-test
+{ t } [ { 1 2 3 4 5 } [ 3 = ] third? ] unit-test
+{ t } [ { 1 2 3 4 5 } [ 4 = ] fourth? ] unit-test
+{ t } [ { 1 2 3 4 5 } [ 5 = ] last? ] unit-test
+{ t } [ 4 { 1 2 3 4 5 } [ 5 = ] nth? ] unit-test
index 13d5634aa2eb27e8922e11cfb0e545916cb9b1d2..495b38f9bd74b33400be816518d6eb9789c6af5c 100644 (file)
@@ -294,3 +294,17 @@ INSTANCE: odds immutable-sequence
 : arg-where ( ... seq quot: ( ... elt -- ... ? ) -- ... indices )
     [ dup length iota zip ] dip
     [ first-unsafe ] prepose filter values ; inline
+
+: first= ( seq elt -- ? ) [ first ] dip = ; inline
+: second= ( seq elt -- ? ) [ second ] dip = ; inline
+: third= ( seq elt -- ? ) [ third ] dip = ; inline
+: fourth= ( seq elt -- ? ) [ fourth ] dip = ; inline
+: last= ( seq elt -- ? ) [ last ] dip = ; inline
+: nth= ( n seq elt -- ? ) [ nth ] dip = ; inline
+
+: first? ( seq quot -- ? ) [ first ] dip call ; inline
+: second? ( seq quot -- ? ) [ second ] dip call ; inline
+: third? ( seq quot -- ? ) [ third ] dip call ; inline
+: fourth? ( seq quot -- ? ) [ fourth ] dip call ; inline
+: last? ( seq quot -- ? ) [ last ] dip call ; inline
+: nth? ( n seq quot -- ? ) [ nth ] dip call ; inline