Skip to content

Commit

Permalink
Initial jq recurse() implementation
Browse files Browse the repository at this point in the history
BFS for jq parity and imperative to avoid call stack limit.
  • Loading branch information
texastoland committed Mar 21, 2024
1 parent 707cda3 commit cc19817
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions stdlib-candidate/std-rfc/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export module str/
# commands
export use fs.nu *
export use set-env.nu *
export use recurse.nu *
23 changes: 23 additions & 0 deletions stdlib-candidate/std-rfc/recurse.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export def main [
--filter: closure
]: any -> list<any> {
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
}

0 comments on commit cc19817

Please sign in to comment.