Live data from Hacker News

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

news.ycombinator.com

61–70 of 394 posts

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

#61
post #59

I like this bit of JavaScript for uniquifying an array: array.filter((item, index, arr) => arr.indexOf(item) === index) It works because indexOf returns the index of the first occurrence of the item, so you're asking whether this occurrence is the first occurrence.

This is O(n^2) though

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

#62
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…

I saw a similar version in erlang (in Joe Armstrong's Programming Erlang) and agree it beautifully illustrates the concept of quicksort. Apparently, Tony Hoare came up with it when he took a class teaching him recursion (i.e. he was a student).

But the flaw you note unfortunately undermines performance... the "quick" in "quicksort".

Thus, IMHO it's a compelling illustration of the profound strengths and weaknesses of fp.

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

#64
post #59

I like this bit of JavaScript for uniquifying an array: array.filter((item, index, arr) => arr.indexOf(item) === index) It works because indexOf returns the index of the first occurrence of the item, so you're asking whether this occurrence is the first occurrence.

This is O(n^2) though

All of JS's list processing functions are pretty inefficient, since at the bare minimum each one creates and copies to a new array (as opposed to, say, Rust iterators). You use them when elegance is more important than performance; N^2 is fine when N is eight.

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

#66
I began coding in IBM/LCSI PC Logo. The first line of code I ever wrote was:

  FD 100
That's the "hello, world" of turtle graphics in Logo. While probably not as beautiful as the several splendid examples posted in this thread, that simple line of code changed my world. I could make stuff happen in an otherwise mostly blank monochrome CRT display. Until then I had seen CRTs in televisions where I had very little control on what I see on the screen. But now, I had control. The turtle became my toy and I could make it draw anything on a 320 x 250 canvas.

The next beautiful piece of code I came across in the same language was:

  REPEAT 360 [FD 1 RT 1]
The code above draws an approximation of a circle by combining 360 short line segments. It showed me how control flow can be used elegantly to express complex ideas in a simple expression. And then I came across this:

  REPEAT 20 [REPEAT 180 [FD 1 RT 2] RT 18]
The above code draws 20 overlapping circles. The output looks like this: https://susam.in/files/blog/dosbox-logo-1.png .

At an impressionable age of 9, reading and writing code like this, and using simple arithmetic, geometry, logic, and code to manipulate a two-dimensional world had a lasting effect on me. I like to believe that my passion for software engineering as well as my love for writing code, sharing code, and open source development are a result of coming across these beautiful code examples early in my life.

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

#67
post #59

I like this bit of JavaScript for uniquifying an array: array.filter((item, index, arr) => arr.indexOf(item) === index) It works because indexOf returns the index of the first occurrence of the item, so you're asking whether this occurrence is the first occurrence.

Personally I like this one, although it only works for an array of primitive types.

    const unique = [...new Set(arr)];

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

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

https://github.com/kelseyhightower/nocode/blob/master/README... you're welcome

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

#70
post #59

I like this bit of JavaScript for uniquifying an array: array.filter((item, index, arr) => arr.indexOf(item) === index) It works because indexOf returns the index of the first occurrence of the item, so you're asking whether this occurrence is the first occurrence.

Personally I like this one, although it only works for an array of primitive types. const unique = [...new Set(arr)];

The other advantage of the filter one is it can be stuck in the middle of a bunch of other list operations - map(), sort(), slice(), other filters(). Also, you can use findIndex() instead of indexOf() to match any arbitrary predicate, instead of exact equivalence.
Post reply on HN