Skip to content

Instantly share code, notes, and snippets.

@n400
Created April 10, 2020 15:18
Show Gist options
  • Save n400/6b565a5d0ce99d1bffc1d0c2c3926cfd to your computer and use it in GitHub Desktop.
Save n400/6b565a5d0ce99d1bffc1d0c2c3926cfd to your computer and use it in GitHub Desktop.
# Recursion UDF from FaunaDB Community user
---
Q: Is there an approved way to loop/repeat FQL an arbitrary number of times? Or generate a set/array of arbitrary size to Foreach on?
A: You can play with UDF to achieve it. Here is a quick idea (using recursion, careful it's max 200 iterations).
Calling it would be something like this Call("Repeat", ["3", "AnotherFunctionName", 0]) .
It's really just an example, you can mess around it to add input values if needs be, etc.
const Repeat = Query(
Lambda(
["times", "fn", "executed"],
Let(
{
end: LTE(ToNumber(Var("times")), 1),
remainingTimes: Subtract(ToNumber(Var("times")), 1),
runQuery: Call(Var("fn")),
executedTimes: Add(Var("executed"), 1)
},
If(
Var("end"),
{ ok: true, executed: Var("executedTimes")},
Call("Repeat", Var("remainingTimes"), Var("fn"), Var("executedTimes"))
)
)
)
---
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment