Hasty Briefsbeta

Bilingual

Programming languages should have a tree traversal primitive

a year ago
  • #programming
  • #tree-traversal
  • #control-flow
  • Programming languages lack a tree traversal primitive, similar to for/foreach loops for linear traversal.
  • Proposal for a 'for_tree' control flow construct in C++-ish syntax to simplify tree-like traversal.
  • 'for_tree' would compile down to recursive function calls, offering a cleaner, less error-prone alternative.
  • Additional benefits include using 'break', 'continue', and 'return' within the loop body, similar to for loops.
  • Introduction of 'prune' keyword to prevent traversal into branches of the current node.
  • Comparison with range-based for loops highlights 'for_tree's ability to operate on non-memory data structures.
  • Discussion on depth-first vs. breadth-first traversal, favoring depth-first for minimal memory usage.
  • Example provided for generating strings of length ≤8 using 'for_tree', showcasing its versatility.
  • Potential footgun: traversing one level deeper than necessary, with manual solutions proposed.
  • Proof-of-concept C++ implementation using templates and macros, acknowledging limitations.