Show HN: LJSON: JSON extended with pure functions
github.com
github.com
Obligatory HN style comment: It might be wise to strengthen the description from "pure" functions to "pure and terminating". I've not had time to form an opinion on whether the restrictions are sufficient to ensure this (JavaScript's evaluation rules are...complex). However, the informal specification of this being a lambda calculus with multiple arguments isn't strong enough - that would allow non-termination on load, which is not something you really want in your data serialisation format.
f = (function (x){ return x(x); })
Now, if you call `f(function(f){return function(x){ return f(f(x)) }})`, it'll not halt. But there are good principled ways to work around that, such as adding a type system like System F. That would discard 100% of the programs that halt, at the cost of also losing some programs that do halt. LJSON.stringify(function(v) { return v; })
evaluates to the string (function(v0){return v0})
While LJSON.stringify(function(v) { return v(null) } )
Evaluates to the string (function(v0){return "v0"})
In the latter case, v(null) was able to examine the variable that stringify injects into the function. In this case, v(null) is the name of the injected variable.For example
LJSON.stringify(function(v) { if (v == 0) { return "zero"; } else { return "one"; } })
Evaluates to the string (function(v0){return "one"})
The only way to avoid both these issues, is to inspect the code of a function f using f.toString(). However you would need to consider the scope where f was defined.Thanks for figuring that out!
{
"math": {
"sin": function(x) {...}
},
"numerics": {
"sin2": function(x) {
return this.parent.math.sin(x)
},
"parent": [automatically inserted by parser]
}
}It reminds me of how JSON started - as just a string representation of a javascript object and parsing JSON was as simple as evaling it.
Once it picked up steam, it got a formal definition and a parser.
If serializing pure functions ever takes off, it will have to follow the same route - without using clever javascript tricks to make it work.
Still, very cool concept.
What would actually be cool is to add type hints to JSON. That way we could conceivably add things like date types to it fairly easily.
Something important that needs doing is strictly defining the JavaScript subset used. As simple as possible, ideally, in keeping with the JSON spirit. This would mean you could then safely implement that subset in other languages. Imagine serialising a PHP function into λJSON and running it from JavaScript, or vice-versa!
We are a step close to the YAML fiasco that has crippled rails.
/me grabs popcorns, and will likely enjoy to watch the world burns.