opa
opa copied to clipboard
Add generic reduce construct
In some cases, it would be useful if Rego allowed for limited/local mutation of a variable, similar to reduce
from functional languages. Rego is not a functional language, but we could either allow limited use of first-class functions (1½-class functions?) or try and figure out a construct that feels more native in our context. Very much open to ideas here!
Some ideas to get the discussion started :)
"Comprehension style"
sum := reduce([acc | some x in [1, 2, 3]; acc := acc + x])
# or
sum := reduce([acc, x | acc := acc + x, 0, [1, 2, 3]])
"Functional style"
sum := reduce(plus, [1, 2, 3])
"Every style"
sum := reduce acc, x in [1, 2, 3] {
acc := acc + x
}
We currently have some built-in functions taking either a single value, or a _n
"version" taking a collection which does something like a reduce behind the scenes (i.e. not in Rego). Providing a generic construct would avoid having to add more whenever there's a request for that. It would also simplify solving certain class of problems, that may currently require some quite arduous workarounds.