Live data from Hacker News

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

news.ycombinator.com

111–120 of 394 posts

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

#111

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 dunno. The security ramifications of those few lines of code make me squirm. It's like looking at a very beautifully constructed foot gun.

I wonder how much damage that code has caused.

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

#113
Well, it's a C macro in Apache Thrift code base [1]

#ifdef __GNUC__

#define TDB_LIKELY(val) (__builtin_expect((val), 1))

#define TDB_UNLIKELY(val) (__builtin_expect((val), 0))

#else

#define TDB_LIKELY(val) (val)

#define TDB_UNLIKELY(val) (val)

#endif

This code is beautiful when it deal with CPU cache-line effects to speed up your program.

[1] https://github.com/apache/thrift/blob/647501693bd14256df8839...

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

#114
post #10

10 print "Hello World"; 20 goto 10; run

My first language was QBASIC and much of the first code I read (other than modifying PHP scripts I didn't really understand) was from Pete's QB Site. I'd hardly call it beautiful, but definitely nostalgic and cool.

This is from QBASIC help file. Composing from PC speaker was very fun:

PLAY "MBT180o2P2P8L8GGGL2E-P24P8L8FFFL2D"

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

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

Oh man, PC Logo takes me back down the memory lane. We were introduced to PC Logo in 4th grade, that's about 9 years ago. I instantly fell in love with it. The amount of power I had over the computer - ordering it to draw what I want, how I want was really exciting.

My father had installed Kubuntu on our home computer which had an amazing suite of educational applications, including KTurtle which was (almost) the same as PC Logo. I got so addicted to it, my father suggested me to create a blog and regularly update it with the drawings and the code for it. And so I did [1]! I used Google Blogpost (that was the thing back then). Good times :).

1: http://kturtlecommands.blogspot.com/

Edit: Just checked the blog after almost 6 years, it still gets 100+ weekly impression, haha :D

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

#117

  (define eval-expr
    (lambda (expr env)
      (pmatch expr
        [`,x (guard (symbol? x))
          (env x)]
        [`(lambda (,x) ,body)
          (lambda (arg)
            (eval-expr body (lambda (y)
                              (if (eq? x y)
                                  arg
                                  (env y)))))]
        [`(,rator ,rand)
         ((eval-expr rator env)
          (eval-expr rand env))])))
There's an amazing talk about it: https://www.youtube.com/watch?v=OyfBQmvr2Hc

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

#119

# Permutations of a list: - haskell: perms [] = [[]] perms xs = [ x:ps | x - js: (using https://github.com/tc39/proposal-slice-notation for conciseness) const perms = xs => xs.length === 0 ? [[]] : xs.flatMap((xi, i) => perms([...xs[0:i], ...xs[i+1:]).map(xsi => [xi, ...xsi]) # Cartesian product of 2 or n lists - haskell: cart2 xs ys = [(x,y) | x [[a]]; cartn [] = [[]] cartn(xs:xss) = [x:ys | x - js: const cart2 = (x…

Until now I didn't know about flatMap() and flat(). Thanks for this!
Post reply on HN