WHILE loop implementation

Coda is now officially Turing-complete :grin:

Here’s an implementation of a while loop that runs as long as the value in the table is not equal to 0. It can be easily modified for any boolean condition by changing the formula in While condition.

The hack used here: a button cannot push itself, but it can push another button, which will then push the first button.

7 Likes

Dear @Paul_Danyliuk,

Thank you sharing your creative approach and thereby supporting the “out of the box thinking” :fountain:

Amicable,
JP

1 Like

Ha nice!

Btw, in case you didn’t find it, a commonly used tricky for a FOR() loop is to use the Sequence() formula and FormulaMap() - e.g. FormulaMap(Sequence(1, 10), CurrentValue+1) or FormulaMap(Contacts, Gmail::SendEmail(... CurrentValue ...))

3 Likes

I did, and I posted a link in that thread to here.

The problem with Sequence/FormulaMap approach is that it only works for specified number of iteration, whereas while loops can have dynamic condition.

Hold on, I’m about to post another doc show-and-tell right now.

2 Likes

Very cool!

I suppose you could separate the check from the action button for clarity.

Also where’s the panic button? I just broke Coda, it’s counting to -Inf ! :stuck_out_tongue:

Thanks. Updated the example.

No panic button in this demo :slight_smile: but you can manually set the counter value to above zero to terminate the loop eventually.

Here’s what I did with this newly acquired knowledge, lol:

This is my new favorite thread on this forum. What a laugh!

Why?
This actually proved to be very useful. E.g., if you need to make two or more nested loops (e.g. you have two lists and need to make MxN rows for a cross-join), the only way to do it for now is via my WHILE implementation and a backing table to store current indices. That’s because we cannot access outer CurrentValue. I’ll make a demo on this when I get back from vacation.

UPD: it’s also possible to do it in one Sequence().FormulaMap() loop if we know M and N in advance. But even then it gets very impractical if you have 3 and more lists.

Example scenario I’m talking about: you have students, you have courses, and each course has variable number of lessons. You need to make rows where you link each student to each lesson directly.

@Paul_Danyliuk
I wholeheartedly commend the solution and creativity.

I suspect the laugh is mostly for the fact that this is required to handle these types of things - the fact that there is a legitimate need indicates it should be something that exists as a top-level function.

I myself used RegexExtract to get outer CurrentValue access so I could do GroupBy(). (I.e.: Map the indices to a: “X1-Y1,…,Xn-Yn” pattern, then used ((\d+)-[^,]+,*(\2-[^,]+,)*), "gm" to group and extract separate lists.)

And… I was absolutely frustrated by the mental exercise required to get the functionality I need as I needed to get things done, not have to reinvent the concept of GroupBy. I don’t want to have to jump through these hoops to do these things, nor have to implement what is essentially a manual-memory-using-multiple-tables turing machine.

And that is not even to mention the fact that you really don’t want to have this in a production document other people will touch.

1 Like

@GJ_Roelofs @cblock maybe you’ll also like this? :joy:

1 Like

If only Windows had this level of operational security.

useless-machines

That’s why!

(This is not to say that I do not appreciate the work here. I find it amusing, but also impressive and useful. )

1 Like