sequences.deep: adding deep-reduce.

db4
John Benediktsson 2015-06-09 08:42:54 -07:00
parent 10c0f808ca
commit a732c2afc5
2 changed files with 8 additions and 0 deletions

View File

@ -6,6 +6,11 @@ HELP: deep-each
{ $description "Execute a quotation on each nested element of an object and its children, in preorder." }
{ $see-also each } ;
HELP: deep-reduce
{ $values { "obj" object } { "identity" object } { "quot" { $quotation ( ... prev elt -- ... next ) } } { "result" "the final result" } }
{ $description "Execute a quotation on each nested element of an object and its children, in preorder. The first iteration is called with " { $code "identity" } " and the first element. Subsequence iterations are called with the result of the previous iteration and the next element." }
{ $see-also reduce } ;
HELP: deep-map
{ $values { "obj" object } { "quot" { $quotation ( ... elt -- ... elt' ) } } { "newobj" "the mapped object" } }
{ $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." }

View File

@ -16,6 +16,9 @@ M: object branch? drop f ;
[ call ] 2keep over branch?
[ '[ _ deep-each ] each ] [ 2drop ] if ; inline recursive
: deep-reduce ( ... obj identity quot: ( ... prev elt -- ... next ) -- ... result )
swapd deep-each ; inline
: deep-map ( ... obj quot: ( ... elt -- ... elt' ) -- ... newobj )
[ call ] keep over branch?
[ '[ _ deep-map ] map ] [ drop ] if ; inline recursive