From cc198178cb6449471cba32d46fad596fa7137d15 Mon Sep 17 00:00:00 2001 From: Texas Toland Date: Tue, 19 Mar 2024 21:40:58 -0500 Subject: [PATCH] Initial jq `recurse()` implementation BFS for jq parity and imperative to avoid call stack limit. --- stdlib-candidate/std-rfc/mod.nu | 1 + stdlib-candidate/std-rfc/recurse.nu | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 stdlib-candidate/std-rfc/recurse.nu diff --git a/stdlib-candidate/std-rfc/mod.nu b/stdlib-candidate/std-rfc/mod.nu index d2c7dc9ee..4ea82ad3a 100644 --- a/stdlib-candidate/std-rfc/mod.nu +++ b/stdlib-candidate/std-rfc/mod.nu @@ -4,3 +4,4 @@ export module str/ # commands export use fs.nu * export use set-env.nu * +export use recurse.nu * diff --git a/stdlib-candidate/std-rfc/recurse.nu b/stdlib-candidate/std-rfc/recurse.nu new file mode 100644 index 000000000..803a3384b --- /dev/null +++ b/stdlib-candidate/std-rfc/recurse.nu @@ -0,0 +1,23 @@ +export def main [ + --filter: closure +]: any -> list { + mut todo = [$in] + mut done = [] + while ($todo | length) > 0 { + # pop + let value = $todo | last + $todo = ($todo | drop) + # save + if $filter == null or ($value | do $filter $value) { + $done ++= [$value] + } + # push + $todo ++= (match ($value | describe --detailed | get type) { + table => ($value | values | flatten) + record => ($value | values) + list => $value + _ => continue + } | reverse) + } + $done +}