News for hackers. Stuff that matters.
Re:How to Use JSON Path by fwlr on Friday May 03, 2024 @09:34AM (#40247507)
I’m a fan, but let’s go even further. JavaScript has pleasant definitions of functions with

    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.

Re:How to Use JSON Path by chuckadams on Friday May 03, 2024 @11:23AM (#40248737)
The problem with your approach is that `filter(x.price < 10)` is perfectly valid syntax, it's filter with a single boolean arg. You need something else to trigger the magic: change `x` to `it` and you have Kotlin and Groovy's shorthand syntax -- you just can't define a variable called `it` anymore. If you want closure semantics on arbitrary undefined variables, I think I might have to slap you on general principle ;)
Re:How to Use JSON Path by tubthumper8 on Friday May 03, 2024 @11:15AM (#40248653)
Some languages do this already but with a designated placeholder, like

    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)
Re:How to Use JSON Path by lizmat on Saturday May 04, 2024 @05:50AM (#40256270)
That's exactly what the Raku Programming Language does. Except that the "x" is represented by "*", so it reads:

    grep(*.price < 10)
This is referred to as "Whatever-currying": https://docs.raku.org/type/Whatever*
Re:How to Use JSON Path by zzo38computer on Saturday May 04, 2024 @03:00PM (#40259583)
I dislike that syntax, but it might be better to instead use a special syntax that can't be the name of any variable, like the asterisk in Raku as mentioned below. In such a case, perhaps the function that would call it should check if the value is already the correct type and use that instead of calling it as a function, since then it would be possible for the filter condition to be a constant true or false that does not depend on the values being filtered (this is not usually useful, but it is more consistent and sometimes it is useful).

I think that the automatic semicolon insertion is a bad feature of JavaScript.