Bash fork bomb (do not run, this will crash your system if it's not configured properly): :(){ :|:& };:
Ask HN: What is the most beautiful piece of code you've ever read?
121–130 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#122awk 'NR==FNR{A[$0]; next} $0 in A' file1.txt file2.txt
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#123Earlier quoted context omitted.
Quick sort comes with a steep penalty. Worst case is O(n^(2)). The reason quicksort is good is because it's in place. Once you throw away the in place aspect of quick sort, it's straight up bad. This implementation of quicksort is actually a great example of why functional programming sucks. It silently transforms an O(1) space algorithm into an O(n) space one, and adds an enormous constant time overhead. Algorithms…
> It silently transforms an O(1) space algorithm into an O(n) space one, and adds an enormous constant time overhead. Please tell me what imperative quicksort algorithm has O(1) space. All versions I've seen and could recall use recursion; although each recursive call uses O(1) space, in the worst case of bad pivot element selection each recursive call would only really sort one element resulting in a worst-case O(n)…
Like to "recurse" (not actually recurse, but pretend) you would increment depth, the update the start position, and calculate the new partition index based on the (depth, start) tuple?
And run that in reverse for going back up the stack.
edit: Hah. This is fun. There's a variant where you do tricks with the elements of the array to get constant space.
https://link.springer.com/chapter/10.1007/BFb0016252
The idea is that you partition the elements, but then instead of storing the bounds of the left and right sides, you switch the element from the start of the right side with the partition element of the "stack frame" above you. This later serves as a flag indicating the end of the right side, since the partition of the parent "stack frame" is greater than all elements on the right you know you've hit then end of the right side when you see a larger number than the parent "stack frame"'s pivot.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#124The 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…
Quick sort comes with a steep penalty. Worst case is O(n^(2)). The reason quicksort is good is because it's in place. Once you throw away the in place aspect of quick sort, it's straight up bad. This implementation of quicksort is actually a great example of why functional programming sucks. It silently transforms an O(1) space algorithm into an O(n) space one, and adds an enormous constant time overhead. Algorithms…
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#125I 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…
CALL -151
That was the entry point to Apple 2's "monitor" (ie bare bones assembler). Not the most beautiful, but very evocative, gave me a sense of power :-)
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#126http://www.literateprogramming.com/knuthweb.pdf
(actual "code" starts on section C)
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#127I'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?
#128John 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.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#129Bash fork bomb (do not run, this will crash your system if it's not configured properly): :(){ :|:& };:
%0|%0Re: Ask HN: What is the most beautiful piece of code you've ever read?
#130The SQLite source tree and DRH code in general are truly piece of art. Almost every line of code is carefully commented. Despite the complexity of the project, you'll learn a lot of practical concepts including expression tree generation, bytecode execution, how to test your code and so forth.
I love the SQLite guy. His own version control, his own DB engine; you can tell from the site that he’s an individualist!