Live data from Hacker News

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

news.ycombinator.com

191–200 of 394 posts

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

#191

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…

the first C version looks the best to me, it is extremely clear and concise. For a slight improvement, declare the variable c inside the loop.

Yet, since 20! is the last factorial representable in a u64, there is not much a point for these functions, and you should definitely check for nIn practice you would want the logarithm of the factorial, that is computed by the "lgamma" function from the C standard math.h. Is such a thing available in rust?

Edit: if you use doubles (which is more reasonable for that use case), you can also do that:

     double factorial(double n) { return tgamma(1+n); }

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

#192
Recursive gem in PDP-11 assembler: convert binary value in R0 into ASCII:-

  CNV10: MOV R0,-(SP)    ;Converts binary value 
         CLR R0          ;in R0 to ASCII in buffer
  1$:    INC R0          ;pointed to R1
         SUB #10.,@SP
         BGE 1$
         ADD #72,@SP
         DEC R0
         BEQ 2$
         CALL CNV10
  2$:    MOVB (SP)+,(R1)+
         RETURN

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

#193
post #155

Implementing FizzBuzz with explicit monoidal operations has been one of those things that I go back to and marvel at the simplicity and elegance of: http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html It really inspired me to get better at seeking out the fundamental operations of whatever I was implementing.

That is great.

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

#195
This is very common in Elm but it blew my mind after years of programming in Python. In Python it is very easy to raise an IndexError by getting an element from a list by index that doesn't exist. eg.

    # python
    names = []

    names[0]  # 
In Elm you are forced to always consider this possibility.

    # Elm
    names = []

    case List.head names of
        Just name ->
            name
        Nothing ->
            "empty"

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

#198
post #197

Earlier quoted context omitted.

Watch out: what strncpy does is usually not what you'd want it to do.

That's what strlcpy() is for.

Or strncpy, if you'd like to stay within the standard and your strings are small.

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

#199
post #144

Earlier quoted context omitted.

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!

By "the appropriate comment" you mean, and I quote:

"// what the fuck? "

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

#200

Pick a random line from a file / stream without knowing how many lines there are to choose from in one pass without storing the lines that have been seen. perl -e 'while( ){$x=$_ if rand() For each line, pick that line as your random line if a random number (0 It hits my elegant bone. Only one line... rand < 1/1, pick it. Two lines, same as one, but the second line has a 1/2 change of replacing line one. Third line s…

Same number of keystrokes, but IMHO more idiomatic & readable:

    perl -ne '$x=$_ if rand()
Post reply on HN