Stream: beginners

Topic: What is the syntax sugar for simple functions?


view this post on Zulip Jacob (Jul 24 2023 at 20:51):

I am pretty sure I saw that this exists, but I can't find it. A syntax sugar for simple functions so instead of List.map(\x -> x + 1), it's List.map(\. + 1) or something along those lines.

view this post on Zulip Brendan Hansknecht (Jul 24 2023 at 21:28):

Nothing of that nature exists.

view this post on Zulip Richard Feldman (Jul 24 2023 at 22:19):

I think I've seen something like that in other languages though - I think Closure has a % variable that can be used inside lambdas for this, if memory serves?

view this post on Zulip Ayaz Hafiz (Jul 24 2023 at 22:24):

scala does this with _ https://docs.scala-lang.org/scala3/book/fun-anonymous-functions.html

view this post on Zulip Jacob (Jul 24 2023 at 23:43):

oh, guess i was imagining things :sweat_smile:

view this post on Zulip Jacob (Jul 24 2023 at 23:55):

Looked up to see if it was common:
Swift has $

numbers.map({ $0 * 2 })

Kotlin has it

numbers.map { it * 2 }

view this post on Zulip Ajai Nelson (Jul 25 2023 at 04:54):

Ruby and Elixir have something similar too. Personally, I think Roc's regular lambda syntax looks more lightweight than regular lambdas in any of those other languages, since parentheses aren't required in the common case: List.map l \x -> x + 1. (That's one my favorite things about Roc's syntax!) So I think a shorthand for that wouldn't be as helpful for Roc as it is for other languages.

view this post on Zulip Hannes (Jul 25 2023 at 05:20):

Unlike Ajai, I get very confused when a lambda doesn't have brackets, so I always add them :sweat_smile: however, I still agree that Roc's lambda syntax is light enough that I don't find myself wanting a lighter way to write it.

view this post on Zulip Ajai Nelson (Jul 25 2023 at 07:11):

That's interesting, it's always good to be reminded how subjective our syntax opinions often are. Being a native Schemer myself, you'd think I would prefer the parentheses haha

view this post on Zulip Fábio Beirão (Jul 25 2023 at 07:45):

I'm not familiar with Kotlin, but that it example immediately reminded me of javascript's this and we can all agree this is not good :sweat_smile:

view this post on Zulip Jacob (Jul 25 2023 at 14:09):

Ah, what I was thinking of is something that does \x -> \x.foo from just .foo in addition to \x -> \x + 1, Lean allows that but uses a placeholder for the single argument of a lambda. But if it isn't already in the language, it is a good idea to not support it (it just makes things way more complicated and confusing). Especially because the lambda syntax is so light as mentioned.


Last updated: Jul 06 2025 at 12:14 UTC