1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00
nu_scripts/modules/recursion/tree.nu
Ed Howland b2fb2b441b
Adds Modules/recursion : Examples of Fun with Recursive functions in Nu (#717)
This directory contains some examples of running recursive algorithms in
Nu. It also has the module : tramp which implements the Trampoline
pattern to overcome the lack of Tail Call Optimization (TCO) avoiding
unlimited stack growth.

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-12-27 13:04:52 -06:00

54 lines
1.3 KiB
Text

# tree.nu: module for working with trees
source typeof.nu # Requires Nushell version 0.88 or later
# A tree is a recursive data structure. In Nu, we take the view that any single atomic value
# is a leaf. E.g. int, float, string, bool, etc.
# Any structured data is some kind of a tree. E.g. list, record or table.
# Applies closure to atomic data.
def visit-scalar [act: closure] {
let data = $in
do $act $data
}
# Visit every element of list and apply closure
def visit-list [act: closure] {
let l = $in
$l |each {|x| $x | visit $act }
}
# Apply closure to every column and value of record in input. Does a visit on
# each key and then on each value.
def visit-record [cl: closure] {
items {|k, v| $k | visit $cl; $v | visit $cl }
}
# Applies closure to every row in table passed to input. Defers to visit-record
# for each row.
def visit-table [act: closure] {
each {|r| $r | visit-record $act }
}
# Applies closure to every node in tree passed to input recursively.
def visit [act: closure] {
let stream = $in
match ($stream | typeof) {
'list' => { do $act 'list'; $stream | visit-list $act },
'record' => { do $act 'record'; $stream | visit-record $act },
'table' => { do $act 'table'; $stream | visit-table $act },
_ => { $stream | visit-scalar $act }
}
}