A Javascript journey with only six characters
jazcash.com
jazcash.com
But it's not without its warts, and this is one of the worst. Although it's sometimes fun to mess with, nonetheless.
To see this taken to one of its logical extremes, check out If Hemmingway Wrote Javascript's entry for Douglas Adams:
// Here I am, brain the size of a planet, and they ask me to write JavaScript...
function kevinTheNumberMentioner(_){
l=[]
/* mostly harmless --> */ with(l) {
// sorry about all this, my babel fish has a headache today...
for (ll=!+[]+!![];ll<_+(+!![]);ll++) {
lll=+!![];
while(ll%++lll);
// I've got this terrible pain in all the semicolons down my right hand side
(ll==lll)&&push(ll);
}
forEach(alert);
}
// you're really not going to like this...
return [!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]];
}https://existentialtype.wordpress.com/2011/03/19/dynamic-lan...
A string is a Boolean is a number is a function, and braindead conversions can happen without anyone noticing. How does one keep their sanity using a language like that?
Hey, at least when JS implicitly converts your types, it actually does type conversion, rather than merely casting them, so you often get what you want (looking at you, C).
Of course, a language that forces all conversions to be explicit is preferable.
EDIT: Supplement: The notion of “type” in C is best translated as “memory layout plan”. Languages of the ML family (including Haskell), e.g., have a notion of “type” closer to mathematical concepts.
These properties are separate issues. A language is either statically typed or dynamically typed, and it is also either strongly typed or weakly typed.
In a statically typed language, types are attached to variables. In a dynamically typed language, types are attached to values. Do note that many languages don't fit 100% into either category. (Note that the blog post you linked to calls types attached to values classes, but that distinction often isn't made so clearly.)
A weakly typed language performs implicit type conversions as it deems necessary, while a strongly typed language does no implicit type conversions. Most languages don't fit 100% into either category. Usually languages that are considered to be strongly typed still allow you to add integers to floating point numbers, for example.
It is possible for a dynamically typed language to be so strongly typed that it won't do any implicit type conversion, ever. Such a language would not allow you to, say, add an integer and a floating point number without explicitly converting one to the other.
It is also possible for a statically typed language to be so weakly typed that it implicitly converts types everywhere. Such a language might do the exact same things the OP uses, like converting a function to a string of its source code when adding it to a string.
More precisely, expressions may be typed. Of course, free variables are one particular of expression.
> In a dynamically typed language, types are attached to values.
More precisely, tags are attached to objects. An object is a “physical” entity that exists in space (computer memory) and time (from its construction to its destruction or garbage collection). A value is an abstract and atemporal entity that only exists in the language's semantics.
> Such a language would not allow you to, say, add an integer and a floating point number without explicitly converting one to the other.
But it does allow you to add integers and floating points! The result just happens to be an exception, which is a very well defined operation in the semantics of most high-level languages.
If it weren't allowed, it simply wouldn't happen.
How does your dynamic language tell its user an operation is invalid, based on argument type?
It doesn't! That's exactly the point. If something is invalid, then it can't happen. At all.
It seems that explanatory articles of this sort often use this style, as it is a great way to get the idea across.
I remember similar strange and interesting stuff in Perl, like the spaceship operator.
Used it a few years ago. It was a little finnicky to get working right but I was impressed at the time. No idea where the project is at nowadays.
Some possible mitigations but I don't know if they ever implemented them.
edit: i guess there is this: https://github.com/substack/vm-browserify
I'd be really interested to hear about other similar implementations of JavaScript in JavaScript. I stopped working on https://github.com/thomasballinger/hotswapping-js-interp#js-... partially because copying the state of the interpreter was relatively slow. I'd be interested in implementations that would handle this better by using a more bytecode-like VM or immutable data structures.
Wondeful! A typo six words in.
And Perl codebase is huge. Perl is used quite heavily at Amazon, Booking.com and Yahoo, among others.
Say what you want about Perl, but it is fun and hacker-friendly. I enjoyed it for many years.
Maybe space isn't a concern in your use case though...
only worth it for long enough code, but then the browser is having a lot of trouble parsing the obfuscated version...
Still: "The js code of this page has been encoded (see source). Compression reduced it from 288k to 37k characters."
-> {anything goes here}
or
->(a,b,c) {anything goes here}
The problem with Ruby is that you then have to .() the lambda vs. (), so that is more verbose than just calling the function.
If browsers were to embrace a language that was more Ruby-like and less clunky than JS, I'm sure I'd use it more.
Or (a, b, c) => a * b ^ c
JS:
let f = (a,b,c) => a * b ^ c;
f(2,3,4);
vs. Ruby: f = ->(a,b,c) { a * b ^ c }
f.(2,3,4)
Ruby's shorter and clearer. But, when you use the Ruby lambda more than once in the code, you lose the brevity advantage, because of the "extra" dot. But, in Ruby I use methods more than lambdas, which would be: def f(a,b,c) { a * b ^ c }
f(2,3,4)f = (a,b,c) => (a * b ^ c)
I would say the different is negligible here and claiming one is more clear than the other is purely preferential. Coming from a JS background the JS is more clear but not substantially enough for me to claim it is outright more clear a language syntax. Someone coming from a ruby background may argue the other direction.
Some languages are quite different but you are splitting hairs here and trying to be conclusive about it.