Live data from Hacker News

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

news.ycombinator.com

181–190 of 394 posts

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

#181
post #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

This is exactly what came to mind when seeing the question.

I was just recently (re-)reading an article that goes in depth:

Lisp as the Maxwell’s equations of software

http://www.michaelnielsen.org/ddi/lisp-as-the-maxwells-equat...

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

#183

Earlier quoted context omitted.

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.

At the time the code was written, the security ramifications were not quite the same as they are now. Even now, I would suggest that there are times where such a construction would be just fine.

I would say it’s clever but not beautiful code.

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

#185

For me it's a function that calculates factorial using iterators in Rust: fn factorial(i: u64) -> u64 { (1..=i).product() } In almost every other language this code would look messy or use some terrible recursion. For example in C it would look something like this: long factorial(int n) { int c; long result = 1; for (c = 1; c Or with recursion: long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n…

> In almost every other language this code would look messy or use some terrible recursion. Nah, lots of other languages can do this. Python: def factorial(i): return reduce(operator.mul, range(2, i+1), 1) Ruby: def factorial(i) (2..i).reduce(1, :*) end Haskell: factorial n = foldl (*) 1 [2..n]

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.

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

#186
post #64

Earlier quoted context omitted.

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.

That's always such a dangerous proposition though. Sure _you_ remember it's only fine when N is 8, but then the next guy comes along, or the input constraints change, or future you forgets because it _is_ fairly elegant looking.

I try not to leave grenades laying around too often, myself.

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

#187
post #144
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.

it's the antithesis of maintainable code. But given that one is unlikely to want to change from calculating the inverse square root to inverse cube root or any other variation....

I disagree strongly. This code is very maintainable: it does not have dependencies, it is trivial to test, it is wickedly short, and with the appropriate comment there is no confusion regarding its purpose. Also, its field of applicability is clear from the context: replace this code with a call to fsqrt if you happen to have a fast hardware implementation of it. It is the most easily maintainable code, ever!

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

#188
Unification of the terms X and Y in the environment E in LISP (part of a PROLOG interpreter):

    (defun unify (x y e)
      (let ((x (look-up x e))
            (y (look-up y e)))
        (cond ((eq x y) e)  
              ((variable-p x) (cons (list x y) e))  
              ((variable-p y) (cons (list y x) e))  
              ((or (atom x) (atom y)) nil)  
              (#t (let ((ne (unify (car x) (car y) e)))
                    (and ne (unify (cdr x) (cdr y) ne)))))))
LOOK-UP looks up X or Y in E, VARIABLE-P returns truth, if X or Y is a variable.

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

#189

For me it's a function that calculates factorial using iterators in Rust: fn factorial(i: u64) -> u64 { (1..=i).product() } In almost every other language this code would look messy or use some terrible recursion. For example in C it would look something like this: long factorial(int n) { int c; long result = 1; for (c = 1; c Or with recursion: long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n…

Clojure:

(defn factorial [n] (reduce *' (range 1 (inc n))))

Post reply on HN