Live data from Hacker News

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

news.ycombinator.com

251–260 of 394 posts

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

#251
post #6

For me, the answer is - The code that never existed. Not to sound cheeky but eliminating code, is a beautiful thing. Less code is easier to maintain, understand, and faster to run. So the less code you can achieve, the better overall the software will be.

The original `true` was a 0-byte executable file [0] (well, perhaps 1 byte for the newline character). Sadly, that is not longer the case.

[0] https://unix.stackexchange.com/questions/419697/why-are-true...

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

#252
post #24

The one that blew my mind when I was in college was a simplified version of quicksort in Haskell. It's just so elegant and clean. quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) where lesser = filter ( = p) xs Now surely someone may come along and point out how this isn't a true quicksort[0] because it doesn't partition the elements in place, but…

The problem with this piece of code is not that it doesn't do it in-place. The problem is, if the input list is (almost) constant, then this code will (almost) certainly take quadratic time to sort it, even if it is shuffled before being fed to this function.

However, I get what you mean. It really is beautiful!

EDIT: grammar.

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

#253

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 "…

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

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

#254

Earlier quoted context omitted.

Great. In 90 minutes I may be able to know what you're talking about.

Is it a complete lisp interpreter, written in lisp?

Yes, it uses pmatch behind the scene to match your expr against the evaluator.

https://github.com/webyrd/quines/blob/master/pmatch.scm

I have to agree, this like rank #1 in my book!

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

#255
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.

That depends on how large the misprediction penalty is and how many times the branch executes. Branch prediction isn't magic. It never makes branches beneficial. The best it can do, with perfect accuracy, is bring the amortized misprediction penalty down to zero. The key is that Duff's Device also relies on branching, and it's a less predictable branch that's more likely to be mispredicted. On the other hand, one BP miss is still cheaper than several hits plus one miss at the end. On the other other hand, there are issues to consider like code size and cache turnover (favors a loop), handling of special cases where larger loads/stores can be used (favors DD), processor extensions, etc. If you're really trying to write an optimal memcpy-like function, you'll probably end up using a hybrid of several approaches - including Duff's Device.

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

#256
post #253

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 "…

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.

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

#257
post #243

Earlier quoted context omitted.

Ok, I’ll bite. If having to think a bit harder about your sorting algorithm is why functional programming sucks, can I give examples of every bullshit concurrency problem I’ve had in Java as an example of why imperative programming sucks? Persistent data structures are a bit slower but largely become non issues if you deal with concurrency and avoid a lock that you would have otherwise required in C or Java. It’s not…

I say this as a die hard FP fan: I agree with your parent comment. I think FP is generally good enough or sometimes even quite close. But I don't know how many times I ended up writing C in Haskell or scheme to make an algorithm really fast (once all other algorithms have been tried or discarded). The quicksort example is a shitty quicksort, because it will be slow as molasses. A proper quicksort in Haskell will degr…

Is the level of my English lacking? The comment you're agreeing to clearly seems to argue that fp sucks as a whole because it's not as fast for tight looped algorithms, not that it sucks only for them.

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

#258

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 "…

[deleted]

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

#259
post #152
post #7

John Carmack's Fast Inverse Square Root: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overv... . The first time I really and truly felt that people approach problems differently from how I, by default, go about them.

I came here to post this. Remember back in the day when it was wildly discussed. Still impressive to this day.

And it also made the guys a ton of money :) They found a way to take something useful in theory and other practical applications and use it to make something never before seen for the massmarket. That one magic number alone is responsible for a good portion of his net worth and of course his Ferrari
Post reply on HN