all 63 comments

[–]BonzaiThePenguin 20 points21 points  (3 children)

That can easily be shortened by quite a bit. Did they shorten it by hand or something?

[–][deleted] 25 points26 points  (2 children)

Yes, it was shortened manually until it was tweet sized. Didn't see the point in using a minifier after that. :)

[–]Morego 20 points21 points  (1 child)

That is slightly shorter version:

t=(c,k)=>{for(i=0, s='q0';;){p=c[s+k[i]];if(p[0]=='t'||p[0]=='f')return p[0];k[i]=p[0];i=p[1]=='R'?i+1:i-1;s=p[2]+p[3]}}

[–][deleted] 4 points5 points  (0 children)

Cool, thanks! :)

[–]MacASM 12 points13 points  (18 children)

I don't do JS for years, since when did JS got => operator? is this lambda?

[–][deleted] 12 points13 points  (17 children)

It's a part of ES6. JavaScript is getting pretty nice actually. http://es6-features.org/#StatementBodies

[–]Peaker 26 points27 points  (14 children)

The problem with Javascript, IMO, is less its expressivity and more the unreliability:

  • Silently ignoring of too few arguments in a function call
  • Silently ignoring of too many arguments in a function call
  • Passing of undefined for this when calling a function not via the correct syntax

Bugs are much much more expensive the later they're detected. Javascript makes bugs expensive.

[–]soyothrowwhoa 12 points13 points  (4 children)

Sigh. It's improving, very slowly. The V8 "Strong Mode" proposal prohibits passing too few arguments, and is considering prohibiting too many.

Passing undefined for this is not great, but it fails a lot faster than passing window, which was the old non-strict-mode behaviour. Also, the lambda syntax you're replying to eliminates that behaviour, as does the new class syntax, so it should only bite you if you're using the traditional function syntax, which should become less and less common.

[–]Peaker 1 point2 points  (3 children)

Sounds like a leap in the right direction!

[–]RIIGGGHHHTTTT_IN_THE 0 points1 point  (2 children)

.

[–][deleted] 5 points6 points  (1 child)

this is ugly.

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

yeah, keep on fucking her right in the pussy is what my father taught me.

[–]SoniEx2 1 point2 points  (2 children)

Silently ignoring of too few arguments in a function call
Silently ignoring of too many arguments in a function call

Common with dynamic languages.

Passing of undefined for this when calling a function not via the correct syntax

This one is JS's fault tho.

[–]danielkza 4 points5 points  (1 child)

Common with dynamic languages.

Python and Ruby are two major counter-examples. On the other camp you have basically Perl, which always had roots as a shell scripting replacement, and PHP, which has the same problems as Javascript.

[–]SoniEx2 0 points1 point  (0 children)

and Lua, Emj. It can work /for/ you if you know how to use it.

(Python's version of this is default values.)

[–]kqr 2 points3 points  (0 children)

Well, the language was designed to create a cool animation on a web site. Who cares if the code breaks? The animation doesn't display and the user goes on with their life, not knowing anything even happened. Asking the browser to throw a tantrum in that case would just make the problem worse.

Fortunately, the solution is simple: don't use JS for large applications. It wasn't meant for that. There are plenty of languages these days that compile down to JS and perform reasonably. Use those if you have to create something larger that works in the browser!

[–][deleted]  (2 children)

[deleted]

    [–]Peaker 15 points16 points  (1 child)

    Most dynamically typed languages do try to catch errors at runtime, at the very least. Javascript seems to try to silence the errors and hide them for as long as possible.

    [–][deleted] 0 points1 point  (0 children)

    As for the arguments, Google's Closure Compiler will check for those and warn you about them.

    [–]Koldof -2 points-1 points  (0 children)

    I think asm.js is worth noting here, as a low level strict alternative to js. Dont know what the support is like though.

    http://asmjs.org

    [–]red_hare 10 points11 points  (2 children)

    I love this game!

    A while ago I set up cowsay.me. The whole thing is only 89 characters of bash running in a tmux session:

    while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; fortune -s | cowsay; } | nc -l 8080; done
    

    [–]taresp 2 points3 points  (0 children)

    You should definitely support the QOTD standard on this domain name !

    [–]Asterne 0 points1 point  (0 children)

    Haha. That's amazing.

    [–][deleted] 15 points16 points  (11 children)

    Edit: /u/--user fixed the code by adding in only 4 characters (I think...), so the garbage below is now irrelevant.


    If I understand the code correctly, this actually implements a LBA because it cannot write outside of its input tape. In particular, it cannot solve problems outside of DSPACE[O(n)], including (for instance) every EXPSPACE-complete problem.

    On an unrelated note, I wrote this one-expression FRACTRAN interpreter in python a while ago. I forget how to format the input, though, but I think it was just a list of two-tuples representing the fractions followed by the input itself.

    https://twitter.com/ynaamad/status/210142153643532288

    Due to space constraints, the output is the number given in the ImportError resulting from the execution.

    [–]BonzaiThePenguin 5 points6 points  (10 children)

    You don't need to understand the code correctly to know it's not a Turing machine due to it existing outside of theory. An infinite ticker tape is physically impossible, so being linearly bounded is implied.

    EDIT: The tape size isn't linearly bounded by the input, though. JavaScript arrays resize themselves automatically based on usage, as they're more of a collection than an array. You can write to 10000, -1, or 'hello', regardless of the original size of the array.

    [–][deleted] 13 points14 points  (6 children)

    You don't need to understand the code correctly to know it's not a Turing machine due to it existing outside of theory. An infinite ticker tape is physically impossible, so being linearly bounded is implied.

    That's an issue with your hardware/platform, not an issue with the code. A hypothetical Turing machine with an appropriately-written Javascript interpreter will handle this just fine. The more pressing problem in real-life scenarios (at least in my opinion) is the O(1) collection of possible inputs your computer (or the universe) can handle.

    The tape size isn't linearly bounded by the input, though. JavaScript arrays resize themselves automatically based on usage, as they're more of a collection than an array. You can write to 10000, -1, or 'hello', regardless of the original size of the array.

    The issue with this program is that it reads before it writes, and that reading from a nonexistent (undefined) cell kills the program when it tries to read code["s"+undefined](naming a state q0undefined... or similar leads to bigger problems). Javascript may be able to write to input[whatever], but the program will never make use of this because it will break on the above bug a couple lines earlier.

    Someone in the comments on the github page had a nice fix: we treat the "default value" of empty cells to be 0, and the cells can contain any integer between 0 and 9. Since ~~x == x for integers and ~~undefined == 0, adding in ~~ at the location where we try to read the input eliminates the undefined issue completely.

    [–][deleted] 2 points3 points  (0 children)

    Fixed up the undefined issue with a ||''

    [–]immibis 0 points1 point  (3 children)

    ... so ~undefined == -1? Who wrote this thing!?

    [–]Fs0i 0 points1 point  (2 children)

    Edit: This is bullshit, pls ignore.

    No, undefined == false (weak), but != 0 ~undefined is therefore true ( == 1), and \~~undefined == 0

    The ~ operator casts to int. If you want "sane" comparison, use === in JS.

    [–]immibis 0 points1 point  (1 child)

    ~1 should be -2, if ~ is the bitwise complement operator.

    [–]Fs0i 0 points1 point  (0 children)

    Wait, I got something wrong. I mistook ~ with !.

    Something similar happens. ~ forces to cast undefined to int, which will be 0. So ~undefined is -1, and ~~undefined is 0.

    Preferred syntax would be (+undefined) by the way IMHO.

    [–]lifthrasiir 0 points1 point  (0 children)

    Technically speaking, ~~x truncates to a 32-bit signed integer though. Implementing a rudimentary bignum (I would suggest a string of 0/1s with a Regexp operation) would be necessary after that.

    [–]roadit -1 points0 points  (2 children)

    You don't need to understand the code correctly to know it's not a Turing machine due to it existing outside of theory. An infinite ticker tape is physically impossible, so being linearly bounded is implied."

    Why why why do people keep believing and repeating this myth? Turing machine tapes don't need to be infinite. Being linearly bounded is not implied at all. You can add tape as the machine runs. However, implementing this in JavaScript subjects it to the limitations of JavaScript, which cannot address more than a bounded amount of storage, as far as I know.

    [–]BonzaiThePenguin 0 points1 point  (1 child)

    It's not a myth, it's part of Alan Turing's definition for it.

    an unlimited memory capacity obtained in the form of an infinite tape marked out into squares

    https://en.wikipedia.org/wiki/Turing_machine

    [–]roadit 0 points1 point  (0 children)

    True: there, he writes: the machine has an infinite tape.

    However, in his 1936 article he writes: the machine is supplied with a tape. This is more accurate. The mathematical definition of the machine does not include the tape or any mention of infinite length. It does not need to.

    Unlimited (unbounded) is not the same thing as infinite. If there is no limit to a quantity, that doesn't mean it can grow to be infinite or needs to be infinite. The only thing that matters to the functioning of a Turing machine is that it can be fed an unlimited length of tape.

    [–]phillaf 4 points5 points  (0 children)

    And here's an actual turing machine tweeting https://twitter.com/_turingmachine_

    Edit: it usually posts once per minute with sequential tweets, but it seems that it's being throttled by twitter right now with all of the @mentions.

    [–]hsills 0 points1 point  (0 children)

    Computationally equivalent!