-
Notifications
You must be signed in to change notification settings - Fork 18
Closed
Description
I realize a lot of functions could just be implemented as calls to a generic tree_map function. For example,
tree_map(t -> 1, tree; merge=max)would calculate the depth of a tree. The “merge” function would be used to aggregate left/right child for binary nodes. For example,
tree_map(t -> 1, tree; merge=(+))would count the total number of nodes. Meanwhile,
tree_map(tree; merge=(+)) do t
Int(t.degree==2)
endwould count the number of binary operators. Then something like
tree_map(tree; merge=(l, r)->[l…, r…]) do t
if t.degree != 0 || !t.constant
return []
end
return [t.val]
endwould return all constants in a tree (in depth-first traversal order).
@Moelf would this have been helpful for writing that NYT puzzle solver? What do you think of the API?
@AlCap23 any comment?
Metadata
Metadata
Assignees
Labels
No labels