Something super simple but that really entertained me when learning lisp: (loop(print(eval(read))) to have a REPL. (Just reverse the letters, easy enough to remember). That to me is elegance. It's simple yet powerful, and just 4 words really.
Ask HN: What is the most beautiful piece of code you've ever read?
301–310 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#302Earlier quoted context omitted.
> One could argue that if the user perceives a X, then it produces a X. This can be stretched to turn anything into anything else. For example, why can't this comment be a maze? In that case 10 REM; 20 END is my even shorter, more elegant maze program because I see a maze in it. Some constraints that are typically implied when people say "computer generated mazes": - They are solvable (have a start and end) - OR they…
So boring.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#303Something super simple but that really entertained me when learning lisp: (loop(print(eval(read))) to have a REPL. (Just reverse the letters, easy enough to remember). That to me is elegance. It's simple yet powerful, and just 4 words really.
Don’t you have the cart before horse; isn’t the acronym derived from this defintion?
I'm not saying the acronym came before or after the LISP code :P
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#304Earlier quoted context omitted.
I love the SQLite guy. His own version control, his own DB engine; you can tell from the site that he’s an individualist!
He mentioned on a podcast that he even uses his own text editor.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#305Re: Ask HN: What is the most beautiful piece of code you've ever read?
#306Earlier quoted context omitted.
Ok, I’ll bite. If having to think a bit harder about your sorting algorithm is why functional programming sucks, can I give examples of every bullshit concurrency problem I’ve had in Java as an example of why imperative programming sucks? Persistent data structures are a bit slower but largely become non issues if you deal with concurrency and avoid a lock that you would have otherwise required in C or Java. It’s not…
I say this as a die hard FP fan: I agree with your parent comment. I think FP is generally good enough or sometimes even quite close. But I don't know how many times I ended up writing C in Haskell or scheme to make an algorithm really fast (once all other algorithms have been tried or discarded). The quicksort example is a shitty quicksort, because it will be slow as molasses. A proper quicksort in Haskell will degr…
Maybe your use-case is different than mine, but I typically use Haskell (or another functional language) for network applications, which typically don't benefit from tight-loops since the network is almost always the bottleneck anyway. For these kinds of applications, having proper concurrency and IO handling matters a lot more than worrying about whether your loops result in a cache-miss.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#307Re: Ask HN: What is the most beautiful piece of code you've ever read?
#308Changed the way I think about code
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#309Earlier quoted context omitted.
So this would be more of a labyrinth than a maze?
No, a labarynth is a single twisting route without branches. A maze has branches and thus can have dead-ends. Both must have an entry and exit point (technically mazes may have more than one exit and entry, but most don't).
I was today years old when I learned this.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#310Earlier 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…
Ok, I’ll bite. If having to think a bit harder about your sorting algorithm is why functional programming sucks, can I give examples of every bullshit concurrency problem I’ve had in Java as an example of why imperative programming sucks? Persistent data structures are a bit slower but largely become non issues if you deal with concurrency and avoid a lock that you would have otherwise required in C or Java. It’s not…
Like everything else in life, those data structures come with tradeoffs. Accessing an array element is an indexing operation into a continuous block of memory. The indexing is built into the instruction set as an addressing mode, the array's memory is continuous (so better locality of reference for caching), and if you're traversing it predictably, prefetch hardware can being it into cache before you issue the instructions that do the read.
The same operation in a persistent vector is a function call, several pointer dereferences, and a more complex on-memory structure with more overhead. It works elegantly, but it's a completely different (and likely slower) level of abstraction from a simple in-memory vector.
A few years ago, I switched a toy search program away from ImmutableJS and over to naive copying of raw JS data structures. I don't remember the exact performance delta, but it was something like an x10-100 speedup, for code that was otherwise structurally identical:
https://github.com/mschaef/react-matchstick/commit/070802b69...
In this case, I had a goal to achieve, a timeline on which to achieve it, and it was the ability to switch to simpler, closer-to-the-metal data structures that made it happen. Had I needed more optimization, the most logical approach would have been to remove even more copying and rely even more heavily on mutation. This shouldn't come as a surprise, because at their core, these machines are fundamentally built on mutation, and there's a cost to pretending otherwise.
Now, in fairness to the persistent structures, this is something like a worst case scenario for their use - the algorithm is almost entirely dominated by manipulating relatively small data structures without much opportunity for sharing. It was also single threaded, small, and developed by a single developer in a small amount of time, so the organizational benefits of immutability were not apparent either.
If this was a huge redux state shared across a team of 100 developers and updated only a few times a second, I could probably tell a completely different story. It's easy to imagine (based on firsthand experience!) the kind of havoc that can be inflicted on a project with an erroneous update.
I get the enthusiasm for functional programming and persistant structures, etc., but at the end of the day it's just an engineering approach. One of many, and there's room to do the work to choose between them.