Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special keyword to define mapping functions in expressions #1010

Open
LeaVerou opened this issue Jan 7, 2024 · 3 comments
Open

Special keyword to define mapping functions in expressions #1010

LeaVerou opened this issue Jan 7, 2024 · 3 comments
Labels
enhancement Status: 3. Architecting We’ve designed this. Now how do we implement it? Topic: Formulas

Comments

@LeaVerou
Copy link
Member

LeaVerou commented Jan 7, 2024

We've often struggled with how to let authors define inline functions in expressions, the concept of an arrow function seeming too out of reach for them.
This would be useful for many things:

  • Mapping operations
  • Custom sorting
  • Filtering

Note that unlike Coda, we don't need things like CountIf() or All() since we can always do things like count(foo > 2) > 1, but they may be useful as sugar.

Coda’s formula language solves this in an interesting way: They allow for a special CurrentValue keyword, which essentially represents the closest list item. In Mavo’s syntax, presumably this could be $value or $current?

There is the problem of referring to outer currentvalues, which they solve with a special scoping function. I think that's suboptimal, I can think of two alternative ways to deal with it, which can be combined:

  • A special $outer keyword. $outer.$value would be the outer $value, and things like $outer.$outer.$value would also be possible
  • Supporting arrow functions as well, and framing it as "$value is the default parameter name, but you can provide your own name by using this syntax".
@DmitrySharabin
Copy link
Member

They allow for a special CurrentValue keyword, which essentially represents the closest list item. In Mavo’s syntax, presumably this could be $value or $current.

Correct me if I’m wrong, but we already have $item, which represents exactly what you described—the closest list item. Can’t we simply re-use it? What am I missing?

@LeaVerou
Copy link
Member Author

They allow for a special CurrentValue keyword, which essentially represents the closest list item. In Mavo’s syntax, presumably this could be $value or $current.

Correct me if I’m wrong, but we already have $item, which represents exactly what you described—the closest list item. Can’t we simply re-use it? What am I missing?

$item (and $this) are fixed within the same execution: they refer to data from the nearest node that matches certain criteria. $value has no meaning outside iterative functions, which loop over a list and $value refers to the current value each time.

Some examples (assume we had map() and sort() functions):

Hypotenuse using numbers in a list:

sqrt(sum(map(numbers, pow($value, 2))))

Filter a list and only keep even numbers:

filter(numbers, $value mod 2 = 0)

Single line pivot table: From a list of flights, to stats about outbound airports:

map(flights by outbound, group(outbound: $value.outbound, count: count($value.$items))

This can already be expressed with current syntax like so:

filter(numbers, numbers mod 2 = 0)

or

numbers where numbers mod 2 = 0

Similarly, it enables patterns like

people.sort($value.name, "asc")

(map each person to their name, then sort based on that)

@LeaVerou LeaVerou added Status: 3. Architecting We’ve designed this. Now how do we implement it? Topic: Formulas labels Jan 13, 2024
@DmitrySharabin
Copy link
Member

Makes sense. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Status: 3. Architecting We’ve designed this. Now how do we implement it? Topic: Formulas
Projects
None yet
Development

No branches or pull requests

2 participants