fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.
Fuzion
•
Idioms
•
Idiom # 128: Breadth-first traversing of a tree
Idiom # 128: Breadth-first traversing of a tree
See
programming-idioms.org
:
Code
breadth_first_traverse(f node -> unit, root node) => breadth_first_traverse(queue Sequence node) unit => match queue.as_list nil => c Cons => f c.head breadth_first_traverse (c.tail ++ c.head.children) breadth_first_traverse [root]
What are effects?
Complete Example
ex128 is node(name String, children Sequence node) is breadth_first_traverse(f node -> unit, root node) => breadth_first_traverse(queue Sequence node) unit => match queue.as_list nil => c Cons => f c.head breadth_first_traverse (c.tail ++ c.head.children) breadth_first_traverse [root] # World # ├─Europe # │ ├─Belarous # │ ├─Portugal # ├─Asia # ├─Oceania # NYI type inference: [] should work for (list node).empty five := node "Belarus" (list node).empty six := node "Portugal" (list node).empty two := node "Europe" [five, six] three := node "Asia" (list node).empty four := node "Oceania" (list node).empty one := node "World" [two, three, four] breadth_first_traverse (node -> say node.name) one
What are effects?
last changed: 2024-07-01
next: Idiom # 129: Breadth-first traversing in a graph