Launched: Reuse logic and create nested loops with the new WithName() formula

We’re excited to introduce the new WithName() formula which enables two key scenarios - reusing logic within a formula, and nested loops.

WithNameExample

Reusing logic within a formula

Sometimes you need to use the same logic twice in different parts of the formula. Until now, you had a few choices:

  1. Duplicate the logic twice. For example:
  If(
      Tasks.Filter(Team="Eng").Count() > 0, 
      Tasks.Filter(Team="Eng").Count(),
      “Done!”
   )
  1. Create an intermediate column or named formula. For example:
  • First write the formula Tasks.Filter(Team="Eng").Count() and name it EngCount
  • Then write a second formula: If(EngCount > 0, EngCount, "Done!")

The first approach is prone to errors if you update the logic and forget to keep it in sync. The second requires extra columns or formulas, even if you never need to use the sub-expression elsewhere.

These cases are now simpler with the new WithName() formula. You can use it to name any value or sub-expression for use elsewhere in the formula. Just specify:

  1. A value you want to name
  2. The name you want to give it
  3. An expression for the formula you want to evaluate. Within that expression, you can use name to reference the corresponding value.

The example above now becomes:

    WithName(
      Tasks.Filter(Team="Eng").Count(), 
      EngCount, 
      If(EngCount > 0, EngCount, "Done!")
    )

We hope this helps you create simpler & more readable formulas that are less error-prone, while avoiding bloating your doc.

Note: If you intend to reference the expression you’re renaming in multiple formulas, we still recommend storing it in a separate column or named formula so it only evaluates once. Any formula with multiple costly expressions may also evaluate more efficiently if you represent each part in a separate column or named formula.

Nested loops

Certain loop formulas in Coda like Filter(), FormulaMap() and AddRows() run multiple times across a list, and use the keyword CurrentValue to reference the current item from the list.

For example, if you want to add a row to a Schedule table for each date in a Dates table, you can create a button with this formula:

    Dates.FormulaMap(
        AddRow(Schedule, Date, currentValue)
    )

Things broke down when you needed to nest multiple loop formulas. For example, if you wanted a row in the Schedule table for each combination of dates and people from a People table, you might try:

    Dates.FormulaMap(
        People.FormulaMap(
            AddRow(Schedule, Date, CurrentValue, Person, currentValue)
        )
    )

The problem this created is that you want the first currentValue to refer to the current date from the Dates table, and the second currentValue to refer to the current person from the People table. Unfortunately, this wasn’t possible.

With the new WithName formula, you can now simply assign a unique name to each currentValue. Here’s an example:

    Dates.FormulaMap(
        WithName(currentValue, currentDate,
            People.FormulaMap(
                WithName(currentValue, currentPerson,
                    AddRow(Schedule, Date, currentDate, Person, currentPerson)
                 )
            )
        )
    )

If you prefer, you can also write the same formulas in a different order with dot-chaining:

    Dates.FormulaMap(
        currentValue.WithName(currentDate,
            People.FormulaMap(
                currentValue.WithName(currentPerson,
                    AddRow(Schedule, Date, currentDate, Person, currentPerson)
                )
            )
        )
    )

We’re excited for the opportunities this opens up for more advanced needs. We know many of you have felt the pain of this in the past, and resorted to clever but cumbersome workarounds. Thanks for highlighting this issue and for your patience as we worked to find a flexible solution!!

71 Likes

This is awesome. Should make things a lot easier in some cases.

5 Likes

Wow! What a surprise!
I’m gonna check all my formulas and try to implement it! :heart_eyes:

4 Likes

Yaaaaay!

unnamed

This is a much better solution than the one I suggested (the CurrentValues stack.)

P.S. As an insider I knew a few days ago but never thought it would hit production so fast. Coda once again delivers.

15 Likes

That’s already fantastic to have

let name = value in expression.

You made my day !

PS: I am still waiting for:

Call(formula name, name, value, name2, value2...)

to reuse formula code in multiple places with different inputs.

11 Likes

Nice! I know the nested looping was a tricky one to figure out an efficient syntax for, and I think this is a good approach. Thanks for engaging with the community on it and listening to the use cases we have out in the wild.

6 Likes

Now we’re cooking with gas!! :star_struck:

10 Likes

I wondered why you were so silent in the outside CurrentValue conversation.

I am so insanely grateful for this. Thank you

4 Likes

OK to zoom out to big picture, is the idea for WithName() to function somewhat like RunActions() where it is the ‘parent’ in a formula that contains everything within? ie:

WithName( Tasks.Count(), n,
  Switch(n,
    1, Product(n,3),
    2, Quotient(n,5),
    3, Power(n,7)
  )
)
2 Likes

Hi @Johg_Ananda,

Essentially yes. It creates a namespace or closure in which your variable name is defined and available for reuse.

– Jason

5 Likes

Thank you so much! I have a lot of formulas that reuse logic, along with a number of things I tried to do before and couldn’t get to work because of the issues with nested loops. I’m very excited to go back and redo my formulas to use this :heart_eyes:

3 Likes

Yaaayyy :tada: !!!

Thank you so much Dear Codans :grin: !!!

Pretty sure, I’ll find quite a lot of formulas I’m going to need to update :yum: and tables where I’m going to be able to reduce the number of field I used :grin: !

3 Likes

This is super great!
You guys made an incredible job and… so quickly!

WithName() is my formula of the year! :trophy:

5 Likes

Yay this is awesome! :smiley:

I suggest WithName to be able to define more than one name, so it will be like this
WithName(value1, name1, [value2], [name2], ..., expression)

12 Likes

This is brilliant! I encountered this limitation way too many times! Thank you Coda team. :grinning_face_with_smiling_eyes:

4 Likes

Nice effort! A foray into less-aesthetically-pleasing but more functional constructs

Let’s see how people take to this - could be a Marmite feature :slight_smile:

3 Likes

Design philosophy question here:

Is the Coda formula language inspired by Closure/lisp and WolframLanguage?

3 Likes

Thank you so much for this information. I am wondering if this new tool can be used to iterate over groups of tasks. We are currently working on a project where we produce instructional content and we have a task list for the entire production process from end-to-end. Now, we are seeing that some classes are popular and we want to bring them back, which means we don’t need the entire task list, but just a subgroup of tasks to be able to repeat as needed. If the nested loops tool wouldn’t work for this, do you have other suggestions?

Looking forward to getting your opinion on this. Thanks for the help.

Best,

2 Likes

Wow. I know enough to see how cool Nested Loops are, but not enough to actually use them. It’s like being able to read a language but not speak it. I’m determined, I’ll figure this out!

3 Likes

I had a similar reaction to this @Jack_DePew! :sweat_smile:

3 Likes