Simulate a For Loop (Reference position inside FormulaMap)

I’ll get to the syntax at the bottom, but conceptually I believe a for loop would have to be an ⚡Action, since it doesn’t return anything.

(After writing that I realize now I don’t think a loop would be a better solution for my initial problem in this post - the better one would be having the key keyword inside FormulaMap).


There are a few problems with iterative formulas in Coda regarding scope and evaluation of values. What @Ander pointed out is definitely one.

Another one is they all use the same alias for CurrentValue, so when you nest them you lose the parent’s context. I guess this could be solved by allowing we to call their parent, as in parent.CurrentValue and parent.parent....


Finally, a loop would also need to modify external data in real time. Correct me if I’m wrong, but the way I see it FormulaMap (or every other formula) makes a local copy of its arguments and evaluates everything then returns a result. So you can’t make a step rely on the result of the previous one.

For example, take this table

image

and add this to a button

Sequence(2,10).FormulaMap(AddRow(Table12, Column 1, Table12.Column 1.Nth(CurrentValue-1).Power(2)))

Initially you’d expect to run AddRow 9 times, squaring each previous row, but actually the sequence halts after the first step, and you get

image

That is even explicit when you edit the formula, it evaluates to a list of actions with only one valid action

image

Which becames obvious when you get it that every time Nth() is evaluated Table 12 contains only one row.


After all that am I wrong in extrapolating that every reference to a table or data inside a formula is not really a pointer to the real thing but just a local snapshot? (The exception being as the first parameter of an Action). This would be the root of lots of confusing moments.

Maybe a pointer syntax would be good then (although very resource demanding?) or maybe I’m overcomplicating things…

I really like the intuitive way we use functions without having to name variables and arguments, it’s one of the appealing features of Coda. I can see why you would avoid it. So the syntax problem is a real one for you devs haha.

1 Like