That javascript version is obviously objectively the best alternative.
data['store']['book'].filter(lambda x: x.price < 10).map(lambda x: x.title)
Yeah, not nearly as good. The syntax sugar of a.b instead of a['b'] and arrow functions instead of lambda really does make a pretty big difference. data.store.book.filter(_.price < 10).map(_.title)
Every language should just adapt it. data.store.book.filter(_.price < 10).map(_.title)
would be written as data.store.book.grep(*.price < 10).map(*.title) let possibleNow =
people
|> List.distinctBy _.Name
|> List.groupBy _.Age
|> List.map snd
|> List.map _.Head.Name
|> List.sortBy _.ToString() filter((x) => x.price < 10)
but why can’t we just write filter(x.price < 10)
and add a rule to the JS engine that says “when you encounter a ‘syntax error: undeclared identifier x’, rewrite the code to add `(x) => ` in front of where the syntax error occurred, if and only if this rewrite prevents the syntax error”.You might protest that reacting to syntax errors by inserting extra code and checking if the errors go away is an insane strategy, but I would note that JavaScript is actually a semicolon-terminated language in which most developers never write a semicolon, and the JavaScript engine is already using this insane strategy on nearly every line of modern JS to insert a semicolon whenever it encounters a syntax error, so it’s obviously practical.
filter(_.price < 10)
That may not work because plain underscore is already a valid identifier but another placeholder could potentially be used and no need for the parser backtracking / function insertion (which I don't like the idea of, there may be cases where an undeclared identifier was a bug and it shouldn't be turned into a function) grep(*.price < 10)
This is referred to as "Whatever-currying": https://docs.raku.org/type/Whatever*I think that the automatic semicolon insertion is a bad feature of JavaScript.
val numbers = listOf(20, 19, 7, 12)
val multiplied = numbers.map { 3 * it }
// [ 60, 57, 21, 36 ] data.store.book.grep(*.price < 10).map(*.title)
Although personally I would write that as: data.store.book.map: { .title if .price < 10 }
which combines the filter / map into a single operation.As an aside the js example above could be simplified to a reduce().