Earlier quoted context omitted.
What is a maze? One could argue that if the user perceives a maze, then it produces a maze.
> 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…
Ask HN: What is the most beautiful piece of code you've ever read?
241–250 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#242 fibonacci = 1 : 1 : [x + y | (x, y) Re: Ask HN: What is the most beautiful piece of code you've ever read?
#243Earlier 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…
The quicksort example is a shitty quicksort, because it will be slow as molasses. A proper quicksort in Haskell will degrade you to writing C in Haskell (with the benefits of a large standard library)and then you have lost. C in C will always beat C in Haskell or any other language.
Is it a worthwhile tradeoff? I believe so. The few times I am limited by speed in that way are few and far between, and most often it was because I thought something like O(n2) would be good enough which is easily fixable. Sometimes I just need to make optimal code faster. By then I always wish I would be using an imperative language instead.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#244Earlier quoted context omitted.
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.
Not only that but in python 3.8, it can be written: def factorial(n): return math.prod(range(1,n+1))
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#245I think c4 (a C compiler in four functions, hence the name) is pretty neat. https://github.com/rswier/c4/blob/master/c4.c
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#246TeX
FYI, there's an effort at the moment to translate XeTeX and dvipdfmx into Rust. Started with c2rust, now we have a test suite checking regressions against the entire arXiv archive. Contributors welcome. https://github.com/crlf0710/tectonic
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#247Haskell allows you to define the full list of prime numbers in just two lines: module Primes where primes = 2 : filter isPrime [3..] isPrime x = all (\y -> mod x y /= 0) $ takeWhile (\y -> y * y The beauty of this is that it's self-referential. The list `primes` is built by filtering all the natural numbers for prime numbers, using the predicate `isPrime` which itself uses the list `primes`. The only caveat is that w…
At least it is better than the one some haskellers call the sieve of erathostenes (it isn't the sieve of erathostenes), which is so god-awful that I almost vomit every time I see it.
The real lazy sieve of erathostenes is a thing of wonder! There is a paper describing it called "the genuine sieve of erathostenes" and it can be found by googling.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#248REBOL [title: "Calculator"] view layout [ origin 0 space 0x0 across style btn btn 50x50 [append f/text face/text show f] f: field 200x40 font-size 20 return btn "1" btn "2" btn "3" btn " + " return btn "4" btn "5" btn "6" btn " - " return btn "7" btn "8" btn "9" btn " * " return btn "0" btn "." btn " / " btn "=" [ attempt [f/text: form do f/text show f] ] ]
https://easiestprogramminglanguage.com/easiest_programming_l...
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#249Something completely different: the Factor language, in the beginning.