Live data from Hacker News

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

news.ycombinator.com

311–320 of 394 posts

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

#312
post #23

For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...

while [ 1 ]; do for i in `seq 1 80`; do printf "\xE2\x95\xB$(( ( RANDOM % 2 ) + 1 ))"; done; printf "\n"; done

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

#313
post #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 r…

Sounds like a Nine Inch Nails song.

"I am in a REPL in a REPL in a REPL,

Running all my code in nested loops..."

https://youtu.be/pVB_DI4ajKA

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

#314

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…

> most of these data structures end up sharing a lot of data, and a “new” structure is often only the diffs from the previous version. Like everything else in life, those data structures come with tradeoffs. Accessing an array element is an indexing operation into a continuous block of memory. The indexing is built into the instruction set as an addressing mode, the array's memory is continuous (so better locality of…

> I get the enthusiasm for functional programming and persistant structures, etc., but at the end of the day it's just an engineering approach. One of many, and there's room to do the work to choose between them.

I don't disagree with this but that's not what you said initially. You said "this is why FP sucks".

I definitely think that if your work is doing tight-loops where every micro-second matters, an imperative language will usually be the correct approach. I don't think anyone is (or would) argue against that point, including most Haskellers.

However, functional languages do simplify things with network/concurrent applications. There's a reason that something like MapReduce is popular. Something like Spark or Onyx is just simpler to do correctly and work with than trying to achieve something equivalent using C or C++; maybe this is just me.

EDIT: My bad, I was responding to the wrong person, this person never said FP sucks. I apologize!

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

#316
post #82
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…

Quick sort comes with a steep penalty. Worst case is O(n^(2)). The reason quicksort is good is because it's in place. Once you throw away the in place aspect of quick sort, it's straight up bad. This implementation of quicksort is actually a great example of why functional programming sucks. It silently transforms an O(1) space algorithm into an O(n) space one, and adds an enormous constant time overhead. Algorithms…

> Quick sort comes with a steep penalty. Worst case is O(n^(2)). The reason quicksort is good is because it's in place. Once you throw away the in place aspect of quick sort, it's straight up bad.

That's why you shuffle the list before you sort it :)

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

#317

Earlier quoted context omitted.

> most of these data structures end up sharing a lot of data, and a “new” structure is often only the diffs from the previous version. Like everything else in life, those data structures come with tradeoffs. Accessing an array element is an indexing operation into a continuous block of memory. The indexing is built into the instruction set as an addressing mode, the array's memory is continuous (so better locality of…

> I get the enthusiasm for functional programming and persistant structures, etc., but at the end of the day it's just an engineering approach. One of many, and there's room to do the work to choose between them. I don't disagree with this but that's not what you said initially. You said "this is why FP sucks". I definitely think that if your work is doing tight-loops where every micro-second matters, an imperative l…

> You said "this is why FP sucks".

No, I did not.

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

#318
post #106
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.

This brings me to a very nice regular expression. It is the one being recommended in RFC3986 "Uniform Resource Identifier (URI): Generic Syntax" by T. Berners-Lee, R. Fielding and L. Masinter https://tools.ietf.org/html/rfc3986 to parse an URI. Having seen so many regular expressions, that try to match it all, this regex tries to match as little as possible, while, at the same time, matches any string, because it mat…

One of my favorite RFCs for sure, and it's a beautifully simple and useful regex. It's a shame RFCs seem to have fallen out of fashion, the WHATWG URL spec is an unreadable mess of a monster, not least the parsing section: https://url.spec.whatwg.org/#url-parsing

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

#319

Earlier quoted context omitted.

What is a maze? One could argue that if the user perceives a maze, then it produces a maze.

> One could argue that if the user perceives a X, then it produces a X. This can be stretched to turn anything into anything else. For example, why can't this comment be a maze? In that case 10 REM; 20 END is my even shorter, more elegant maze program because I see a maze in it. Some constraints that are typically implied when people say "computer generated mazes": - They are solvable (have a start and end) - OR they…

>>One could argue that if the user perceives a X, then it produces a X. >This can be stretched to turn anything into anything else. For example, why can't this comment be a maze?

Because unlike the one-liner nobody perceives it as a maze, so the "this can be stretched to turn anything into anything else" argument is tenuous...

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

#320
post #240

Earlier quoted context omitted.

No, it does not generate a maze. It generates a random sequence of \s and /s, that can trigger a maze being generated in a typical user's mind. This is the art of illusion.

Everything is an illusion. \s and /s are just liquid crystal pixels on the screen, filtering some light. However it's easier to say that if something has a name and you can identify it, then it's probably that thing. I see a maze it means it's a maze :)

>I see a maze it means it's a maze :)

I'd suggest to not go bar-hopping in Thailand with that mindset...

Post reply on HN