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 20, 2024
1 parent 7d662ad commit 5f34622
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.nu
# 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 5f34622

Please sign in to comment.