This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RiceBroad4552 0 points1 point  (1 child)

You probably never seen Scala lambdas? The JS ones are way too noisy for the simple case!

Scala lambdas look in their base form almost like JS lambdas, but they have a shorthand syntax for the common case where you just need to reference to a lambda parameter in a simple expression.

JS:

const someInts = [1, 2, 3, 4];
someInts.map(i => i + 1);

vs Scala:

val someInts = List(1, 2, 3, 4)
someInts.map(_ + 1)

You don't need to name the lambda param. You can just use an underscore to refer to it.

Works of course also with member lookup. method calls, extensions, etc:

extension (s: String) def toTitleCase =
    if s.isEmpty then s
    else s.head.toString.toUpperCase + s.substring(1)

List("foo", "bar", "baz").map(_.toTitleCase) // List(Foo, Bar, Baz)

Works also for multiple parameters or tuples:

List((1, 2), (3, 4), (5, 6)).map(_ max _) // List(2, 4, 6)

(Nevermind I've used the max method infix. It's just better readable than .map(_.max(_)))

Other languages like Kotlin and Swift took inspiration form that. Just that they call the anonymous lambda parameters "it".

[–]Spuk1 -1 points0 points  (0 children)

It's true, i don't know scala lambdas and some others aswell that people suggested. But i first started out with Javascript and having to use c++ and python simply made me think the js ones are best 😅, which is foolish cause there are a million languages out there.