Live data from Hacker News

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

news.ycombinator.com

151–160 of 394 posts

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

#151
post #23

For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...

No, it does not generate a maze. It generates a random sequence of \s and /s, that can trigger a maze being generated in a typical user's mind. This is the art of illusion.

What is a maze? One could argue that if the user perceives a maze, then it produces a maze.

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

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

I came here to post this. Remember back in the day when it was wildly discussed. Still impressive to this day.

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

#154

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…

Does this algorithm guarantee uniform probability for all lines? Seems like the original ordering of the lines is a factor.

If you want to monte carlo it, https://gist.github.com/patio11/546c64c927c749c69964b18527fa... ; feel free to adjust the constants upwards and/or do a chi squared test after doing so.

It didn't sound plausible to me (because I misunderstood what the algorithm was; yay perl), hence quickly testing it, but it seems to work, and after appreciating what the algorithm actually is (see sibling replies), it not only works but it _should_ work. (There is a CS versus engineering joke in here, somewhere.)

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

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

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

#156
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-1));
  }
In any case, I thin Rust looks better in every way with its cleaner syntax.

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

#157
post #23

For me it's a BASIC one-liner which generates mazes 10 PRINT CHR$ (205.5 + RND (1)); : GOTO 10 I found this specific one via slashdot[1], but something similar, which I've never managed to find/replicate, was used to generate mazes on the Atari 800 XL at my school when I was a kid. [1] https://developers.slashdot.org/story/12/12/01/1847244/how-d...

This, as a WebGL shader in shadertoy: https://www.shadertoy.com/view/ld23DW

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

#158

Earlier quoted context omitted.

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.

That’s what strncpy() is for

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

#159
I think this was the first line of the forth in the library of an older version of jonesforth (https://rwmj.wordpress.com/2010/08/07/jonesforth-git-reposit...), but now I can only find it in my port:

  : \ IMMEDIATE
        #IB @ >IN !
  ; \ We can now comment!
Implementing a forth system is unbelievably fun because of gems like this.

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

#160
I once had to find all lines which did not contain a specific word. As back then i was absolutely new to regex stackoverflow came to the rescue - and i still have that link saved to this day - https://stackoverflow.com/a/406408

Regex for that is just great

  ^((?!word).)*$
Post reply on HN