Earlier quoted context omitted.
This is O(n^2) though
All of JS's list processing functions are pretty inefficient, since at the bare minimum each one creates and copies to a new array (as opposed to, say, Rust iterators). You use them when elegance is more important than performance; N^2 is fine when N is eight.
Ask HN: What is the most beautiful piece of code you've ever read?
71–80 of 394 posts
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#72Earlier quoted context omitted.
Personally I like this one, although it only works for an array of primitive types. const unique = [...new Set(arr)];
The other advantage of the filter one is it can be stuck in the middle of a bunch of other list operations - map(), sort(), slice(), other filters(). Also, you can use findIndex() instead of indexOf() to match any arbitrary predicate, instead of exact equivalence.
The one I posted just blew my mind a little the first time I saw it, so I love to share it. I guess I never really considered spreading a set.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#73 universal_server() ->
receive
{become, F} ->
F()
end.
[0] https://joearms.github.io/published/2013-11-21-My-favorite-e...Re: Ask HN: What is the most beautiful piece of code you've ever read?
#74The 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…
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#75For 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...
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#76I felt it was sort of like poetry. I unfortunately no longer have a link to it, and once looked very hard but couldn't find it.
I would be extremely appreciative of someone else saw it and had a link. If I recall correctly it was a type of interpreter, I remember it having code for parsing.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#77For 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...
What is the probability that it prints a completable[0] maze? Expressed in terms of line width (w) and number of lines (n). [0] Where there's a valid path from the first line to the last.
Edited for clarity and accuracy.
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#78 fibs = 0 : 1 : zipWith (+) fibs (tail fibs)Re: Ask HN: What is the most beautiful piece of code you've ever read?
#79For 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...
There’s actually an entire book on this at: https://10print.org/
Re: Ask HN: What is the most beautiful piece of code you've ever read?
#80For 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...
There’s actually an entire book on this at: https://10print.org/