{ $description "Execute a quotation on each nested element of an object and its children, in preorder. That is, the result of the execution of the quotation on the outer is used to map the inner elements." }
{ $see-also map } ;
+HELP: deep-filter-as
+{ $values { "obj" object } { "quot" { $quotation "( ... elt -- ... ? )" } } { "exemplar" sequence } { "seq" sequence } }
+{ $description "Creates a sequence (of the same type as " { $snippet "exemplar" } ") of sub-nodes in the object which satisfy the given quotation, in preorder. This includes the object itself, if it passes the quotation." } ;
+
HELP: deep-filter
-{ $values { "obj" object } { "quot" { $quotation "( ... elt -- ... ? )" } } { "seq" "a sequence" } }
+{ $values { "obj" object } { "quot" { $quotation "( ... elt -- ... ? )" } } { "seq" sequence } }
{ $description "Creates a sequence of sub-nodes in the object which satisfy the given quotation, in preorder. This includes the object itself, if it passes the quotation." }
{ $see-also filter } ;
{ $description "Tests whether the given object or any subnode satisfies the given quotation." }
{ $see-also any? } ;
+HELP: flatten-as
+{ $values { "obj" object } { "exemplar" sequence } { "seq" sequence } }
+{ $description "Creates a sequence (of the same type as " { $snippet "exemplar" } ") of all of the leaf nodes (non-sequence nodes, but including strings and numbers) in the object." } ;
+
HELP: flatten
-{ $values { "obj" object } { "seq" "a sequence" } }
+{ $values { "obj" object } { "seq" sequence } }
{ $description "Creates a sequence of all of the leaf nodes (non-sequence nodes, but including strings and numbers) in the object." } ;
HELP: deep-map!
[ [ "hello" 3 4 swap ] ] [ [ { "hello" V{ 3 4 } } swap ] flatten ] unit-test
+{ "ABC" } [ { { 65 } 66 { { 67 } } } "" flatten-as ] unit-test
+
[ "foo" t ] [ { { "foo" } "bar" } [ string? ] (deep-find) ] unit-test
[ f f ] [ { { "foo" } "bar" } [ number? ] (deep-find) ] unit-test
[ call ] keep over branch?
[ '[ _ deep-map ] map ] [ drop ] if ; inline recursive
+: deep-filter-as ( ... obj quot: ( ... elt -- ... ? ) exemplar -- ... seq )
+ [ selector [ deep-each ] dip ] dip [ like ] when* ; inline recursive
+
: deep-filter ( ... obj quot: ( ... elt -- ... ? ) -- ... seq )
- over [ selector [ deep-each ] dip ] dip
- dup branch? [ like ] [ drop ] if ; inline recursive
+ over dup branch? [ drop f ] unless deep-filter-as ; inline
: (deep-find) ( ... obj quot: ( ... elt -- ... ? ) -- ... elt ? )
[ call ] 2keep rot [ drop t ] [
over branch? [
[ f ] 2dip '[ nip _ (deep-find) ] any?
- ] [ 2drop f f ] if
+ ] [ 2drop f f ] if
] if ; inline recursive
: deep-find ( ... obj quot: ( ... elt -- ... ? ) -- ... elt ) (deep-find) drop ; inline
: flatten ( obj -- seq )
[ branch? not ] deep-filter ;
+
+: flatten-as ( obj exemplar -- seq )
+ [ branch? not ] swap deep-filter-as ;