Live data from Hacker News

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

news.ycombinator.com

101–110 of 394 posts

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

#101
post #92

Earlier quoted context omitted.

You don't need the list comprehension for python - just 2 * sum(range(1, 13))

And drop the 1, too

I prefer the explicit example for being explicit, I find it more pythonic.

GPs example is perfect Python; every character has meaning, and it's extremely readable even for non-programmers.

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

#102
post #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 contro…

Logo was the best!

I still remember my father ‘explaining’ me Pythagoras theorem when I was around 5 to show me how to draw the roof of a house on our hand-soldered Philips Apple II clone.

I’ve been hooked ever since.

The best thing was that 25 years later I opened a Logo emulator again and when faced with having to clean the screen somewhere deep, deep from my muscle memory the right command sprang forward: CLEAR

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

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

Unfortunately it's also incorrect! It discards NaNs, as they are neither =.

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

#105
I really appreciated and missed Scala's `Some`/`None`/`Option` [1] and `lazy` constructs, so much so that I brought them with me to TypeScript.

Option provides an elegant way to handle parameters or results that may or may not be defined.

Here's a simple Option implementation:

https://gist.github.com/mceachen/75598510275865b8cf88bb2ef80...

With this you can write something like

Opt(possiblyNullResult).flatMap(ea => functionThatRequiresANonNullResultAndReturnsUndefinedOrDefined(ea)).getOrElse(() => someDefaultValue)

And here's a (very) simple implementation of lazy:

https://github.com/photostructure/exiftool-vendored.js/blob/...

The idea of lazy is to allow deferment of expensive operations until they are actually needed.

The above implementation allows for something like:

const service = lazy(makeService)

Which will ensure makeService is only called once, and the first caller will have to wait for the result of makeService(). All subsequent callers re-use the first result.

It's a simple construct, but extremely handy.

[1] https://github.com/scala/scala/blob/v2.13.1/src/library/scal...

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

#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 matches no string. It does not define what to match, but what not to match and making every match optional.

According part from the spec:

    ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
     12            3  4          5       6  7        8 9

    The numbers in the second line above are only to assist readability;
    they indicate the reference points for each subexpression (i.e., each
    paired parenthesis).  We refer to the value matched for subexpression
     as $.  For example, matching the above expression to

      http://www.ics.uci.edu/pub/ietf/uri/#Related

    results in the following subexpression matches:

      $1 = http:
      $2 = http
      $3 = //www.ics.uci.edu
      $4 = www.ics.uci.edu
      $5 = /pub/ietf/uri/
      $6 = 
      $7 = 
      $8 = #Related
      $9 = Related

    where  indicates that the component is not present, as is
    the case for the query component in the above example.  Therefore, we
    can determine the value of the five components as

      scheme    = $2
      authority = $4
      path      = $5
      query     = $7
      fragment  = $9

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

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

IIRC Carmack didn't write that code, as brilliant as he is.

It even says that he didn’t write it in that wikipedia page.

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

#108
NEW

10 PRINT "HELLO"

20 GOTO 10

30 END

RUN

I remember typing this on an ASR-33 and being amazed. I made a computer do that. Then I hit Ctrl-C and learned how to make a paper tape with a "here is" leader, turning the paper punch off, then typing "LIST" and turning it back on before hitting RETURN.

A PDP-11/10 with 16K and 3 20mA TTYs connected, no disks, no storage except paper tape.

I've still got that paper tape somewhere, it's 42 years old now.

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

#109

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…

>RTM's worm I had absolutely no idea until now that RTM co-founded y-combinator. I remember him from mentioned in Bruce Sterling's The Hacker Crackdown and Clifford Stoll's The Cookoo's Egg as well as an occasional Phrack article. https://en.wikipedia.org/wiki/Robert_Tappan_Morris

He was one of PG's Viaweb cofounders as well. He also introduced PG to the third cofounder, Trevor Blackwell.

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

#110

I'd have to think long and hard for the most beautiful code I've ever read, but I think the classic K&R "strcpy" comes pretty close: void strcpy(char *s, char *t) { while (*s++ = *t++); } It's short, elegant, and quite readable to the trained eye–a bit sharp too, but if you use it right it's quite functional.

I've got a better code snippet: your exact same code snippet but for a language which will short circuit based on the lvalue of the assignment expression. Then t would never be able to overflow s (nor even eat its null terminator)

The size of the buffer pointed to by s may be larger than the current string it holds. It may also be uninitialized.
Post reply on HN