Add deep-member? and deep-subseq? to sequences.deep.

db4
Doug Coleman 2008-12-11 23:11:04 -06:00
parent 05d6cf873f
commit afe942130e
3 changed files with 28 additions and 2 deletions

View File

@ -1 +1,2 @@
Daniel Ehrenberg
Doug Coleman

View File

@ -24,3 +24,18 @@ IN: sequences.deep.tests
[ "foo" ] [ "foo" [ string? ] deep-find ] unit-test
[ { { 1 2 } 1 2 } ] [ [ { 1 2 } [ , ] deep-each ] { } make ] unit-test
[ t ]
[ { { 1 2 3 } 4 } { { { 1 { { 1 2 3 } 4 } } } 2 } deep-member? ] unit-test
[ t ]
[ { { 1 2 3 } 4 } { { { 1 2 3 } 4 } 2 } deep-member? ] unit-test
[ f ]
[ { 1 2 3 4 } { 1 2 3 { 4 } } deep-subseq? ] unit-test
[ t ]
[ { 1 2 3 4 } { 1 2 3 4 } deep-subseq? ] unit-test
[ t ]
[ { 1 2 3 4 } { { 1 2 3 4 } } deep-subseq? ] unit-test

View File

@ -1,6 +1,6 @@
! Copyright (C) 2007 Daniel Ehrenberg
! Copyright (C) 2007, 2008 Daniel Ehrenberg, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: sequences kernel strings math ;
USING: sequences kernel strings math fry ;
IN: sequences.deep
! All traversal goes in postorder
@ -38,6 +38,16 @@ M: object branch? drop f ;
: deep-all? ( obj quot -- ? )
[ not ] compose deep-contains? not ; inline
: deep-member? ( obj seq -- ? )
swap '[
_ swap dup branch? [ member? ] [ 2drop f ] if
] deep-find >boolean ;
: deep-subseq? ( subseq seq -- ? )
swap '[
_ swap dup branch? [ subseq? ] [ 2drop f ] if
] deep-find >boolean ;
: deep-change-each ( obj quot: ( elt -- elt' ) -- )
over branch? [
[ [ call ] keep over [ deep-change-each ] dip ] curry change-each