Live data from Hacker News

Ask HN: What is the most beautiful piece of code you've ever read?

news.ycombinator.com

261–270 of 394 posts

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#261
post #57

Duff's Device: https://en.wikipedia.org/wiki/Duff%27s_device Very elegant use of the fall-through behavior of the swtich statement.

For those unaware, * do not use this on modern systems. * Normal for-loops are much faster than they were when Duff's Device was invented, since they take advantage of modern branch prediction.

Another thing to note, is that loop unrolling can be harmful to performance, as it requires more instruction cache. Beyond branch prediction, modern compilers can also convert loops into vector / SIMD instructions, and other magic.

The Duff's device is still useful for creating co-routines though; a handy way of yielding, then returning to the yield point.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#262

A fully graphical calculator app in Rebol is just a few lines of code. The formatting looks pretty legible at the below link if you scroll down a bit. REBOL [title: "Calculator"] view layout [ origin 0 space 0x0 across style btn btn 50x50 [append f/text face/text show f] f: field 200x40 font-size 20 return btn "1" btn "2" btn "3" btn " + " return btn "4" btn "5" btn "6" btn " - " return btn "7" btn "8" btn "9" btn "…

Code formatted:

  REBOL [title: "Calculator"]
  view layout [
      origin 0  space 0x0  across
      style btn btn 50x50 [append f/text face/text  show f]
      f: field 200x40 font-size 20 return
      btn "1"  btn "2"  btn "3"  btn " + "  return
      btn "4"  btn "5"  btn "6"  btn " - "  return
      btn "7"  btn "8"  btn "9"  btn " * "  return
      btn "0"  btn "."  btn " / "   btn "=" [
          attempt [f/text: form do f/text  show f]
      ]
  ]

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#263

Bit tricks. Like public static int bitCount(int i) { i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); i = (i + (i >>> 4)) & 0x0f0f0f0f; i = i + (i >>> 8); i = i + (i >>> 16); return i & 0x3f; } It was like magic for me when I encountered it first time.

Check out the Othello implementation by Hans Wennborg with a bitboard at https://www.hanshq.net/othello.html

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#264

This is very common in Elm but it blew my mind after years of programming in Python. In Python it is very easy to raise an IndexError by getting an element from a list by index that doesn't exist. eg. # python names = [] names[0] # In Elm you are forced to always consider this possibility. # Elm names = [] case List.head names of Just name -> name Nothing -> "empty"

Idiomatic python:

  v = names[0] if names else "empty"

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#266

Something super simple but that really entertained me when learning lisp: (loop(print(eval(read))) to have a REPL. (Just reverse the letters, easy enough to remember). That to me is elegance. It's simple yet powerful, and just 4 words really.

Nice. I immediately had to try the same in PHP. To make a working repl with newlines in the output etc, this is what I a came up with:

    while(1) {eval(fgets(STDIN));echo "\n";};
I then tried it on the command line like this:

    php -r 'while(1) {eval(fgets(STDIN));echo "\n";};';
Hurray, it prompted me for input! So I typed:

    for ($i=0;$i
Which got me:

    0123456789
So far so good.

I wondered: Can we now run the repl in the repl? So I typed:

    while(1) {eval(fgets(STDIN));echo "\n";};
It kept prompting me for input. Am I in a REPL in a REPL now? I typed:

    echo "Hello from a REPL in a REPL!";
And the reply was:

    Hello from a REPL in a REPL!
I'm not totally sure if I believe it though.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#267
I'll nominate this pseudocode from a paper of mine currently under review. Treat it like a koan to be meditated upon to gain enlightenment about AGI. The paper introduces an ordinal notation system in which ordinals are notated by computer programs. This pseudocode notates the ordinal omega^2. `, ⌜, and « are progressively higher-level opening quotation marks. ', ⌝, and » are the corresponding closing quotation marks.

LEFT = «X=⌜»;

RIGHT = «⌝; While(True) {Print(X); X=⌜Print(`⌝+X+⌜')⌝}»;

X = «Exit()»;

While(True) {

  X = LEFT + X + RIGHT;

  Print(X);

}

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#268
post #253

Earlier quoted context omitted.

REBOL and Red are so so cool. I wonder why hackers are not embracing them.

Rebol was closed source back when open source was really starting to take off with Perl and Linux. It is seriously cool (I agree), but the interpreter is a little slow. Red still has a little ways to go, but could be a game changer some day. The project is insanely ambitious, but I'm optimistic.

The main (IMO sad) reason REBOL sits in a weird half-life position of "not quite dead, but..." is that, while REBOL Core is open, the GUI parts (which are crossplatform, and run on Linux) are still closed source.

It's a bit like .NET Core. Great to base a business on... not so great to tinker around with. Arguably the second (tinkering, learning) comes before the first (using what you've learned to build a business).

Mildly infuriating...

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#269

Something super simple but that really entertained me when learning lisp: (loop(print(eval(read))) to have a REPL. (Just reverse the letters, easy enough to remember). That to me is elegance. It's simple yet powerful, and just 4 words really.

Don’t you have the cart before horse; isn’t the acronym derived from this defintion?

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#270

Exploit codes are often the most beautiful code I read, they are usually small and take some dazzling brilliance to push the computer and make it do what it wasn't. I can remember the first code that showed how to exploit IFS, race conditions via symlink, the classic "smashing the stack", RTM's worm. Beauty of a code to me has nothing to do with the formatting, comments, documentations, but everything to do with the…

I have similar admiration of hardware hacks.

My current favorite is the one that dumped the SecureROM out of the iPhone 6 via PCI-e: http://ramtin-amin.fr/#nvmedma, http://ramtin-amin.fr/#nvmepcie

I'm partly impressed by the tooling used; most of the coolness (to me) is the author's self-confidence in his hunch that the SoC _didn't_ have its MMU set up properly, and the way he followed his nose in determining that he was probably right.

I still wonder exactly how much was sunk into the project, before it was possible to determine that the MMU was indeed broken. Heh.

Post reply on HN