So I was struggling with a problem where I needed to evaluate multiple lists simultaneously. A classic use case for a loop.
Coda doesn’t have built-in formulas for classic loops such as for
or while
. The closest thing being FormulaMap
. It takes a list
as input and runs every item through an expression
. Inside the expression, you use CurrentValue
to reference the current item being evaluated. As a result, CurrentValue
can be of any type (row, date, etc).
But there’s no obvious way to get the key
, or the item position inside the list.
Unless…
You make CurrentValue
to be exactly the same as its position! You can do that using Sequence
to create an auxiliary list.
Sequence(1,finalValue) // =[1,2,3...finalValue]
Then run FormulaMap
over this list.
Sequence(1, finalValue).FormulaMap(CurrentValue*2) // =[2,4,6...finalValue*2]
Applying it to my case:
//List1 = [1,2,3]
//List2 = [5,7,1]
Sequence(1, List1.Count()).FormulaMap(List1.Nth(CurrentValue)+List2.Nth(CurrentValue)) // =[6,9,4]
The more general form would be:
Sequence(initialValue, finalValue, step).FormulaMap(expression(CurrentValue))
More or less equivalent to
for (i = initialValue; i <= finalValue; i+=step) {
expression // use i here
}
Since CurrentValue
is a number from the sequence, it serves the purpose of the i
.