Live data from Hacker News

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

news.ycombinator.com

241–250 of 394 posts

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

#241

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…

I believe your third criterion is actually met by this generator. The grid is just tilted 45 degrees.

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

#243
post #82

Earlier quoted context omitted.

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…

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 degrade you to writing C in Haskell (with the benefits of a large standard library)and then you have lost. C in C will always beat C in Haskell or any other language.

Is it a worthwhile tradeoff? I believe so. The few times I am limited by speed in that way are few and far between, and most often it was because I thought something like O(n2) would be good enough which is easily fixable. Sometimes I just need to make optimal code faster. By then I always wish I would be using an imperative language instead.

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

#244
post #227

Earlier quoted context omitted.

For Python, we don't need to supply the optional initializer 1. And reduce resides in the functools namespace (much like mul is in operator). I assume we're not talking about that peculiar dialect of Python that is no longer supported a few months from now.

Not only that but in python 3.8, it can be written: def factorial(n): return math.prod(range(1,n+1))

Nice!

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

#245

I think c4 (a C compiler in four functions, hence the name) is pretty neat. https://github.com/rswier/c4/blob/master/c4.c

That's my choice too. It's a C-subset, but more than a compiler; it includes a bytecode VM interpreter too. The best part about it is that it's tiny, yet very readable for its size and functionality.

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

#246

TeX

FYI, there's an effort at the moment to translate XeTeX and dvipdfmx into Rust. Started with c2rust, now we have a test suite checking regressions against the entire arXiv archive. Contributors welcome. https://github.com/crlf0710/tectonic

Why? It works fine. Does _everything_ need to be in Rust, or whatever the new language-of-the-day is?

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

#247

Haskell allows you to define the full list of prime numbers in just two lines: module Primes where primes = 2 : filter isPrime [3..] isPrime x = all (\y -> mod x y /= 0) $ takeWhile (\y -> y * y The beauty of this is that it's self-referential. The list `primes` is built by filtering all the natural numbers for prime numbers, using the predicate `isPrime` which itself uses the list `primes`. The only caveat is that w…

It is also dirt slow. This is just trial division, done in a lazy fashion.

At least it is better than the one some haskellers call the sieve of erathostenes (it isn't the sieve of erathostenes), which is so god-awful that I almost vomit every time I see it.

The real lazy sieve of erathostenes is a thing of wonder! There is a paper describing it called "the genuine sieve of erathostenes" and it can be found by googling.

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

#248
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 " * " return btn "0" btn "." btn " / " btn "=" [ attempt [f/text: form do f/text show f] ] ]

https://easiestprogramminglanguage.com/easiest_programming_l...

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

#250

Earlier quoted context omitted.

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

Maze has a starting point and a goal. The goal must be reachable from the start.

Only if you want to give the occupant of the maze a chance to escape :-)
Post reply on HN