Live data from Hacker News

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

news.ycombinator.com

271–280 of 394 posts

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

#271
Here’s one i’m really proud of, i crafted it when learning js a while back. Its a one liner that flattens arbitrarily nested array. Its recursive though, so i guess it has its limits. I remember commenting something along the lines of “dark magic functional prog” (thats how i found it a second ago in GH actually):

const flatten = arr => ((flat = [].concat(...arr)) => flat.some(Array.isArray) ? flatten(flat) : flat)()

Its simple, but i was - and still am - way to proud of it ;p

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

#272

Lexical Scanning In Go is a charming piece of programming: https://m.youtube.com/watch?v=HxaD_trXwRE A good debuggable piece of code explains what it is trying to do by being clearly written and conforming to a consistent and logical model. I don’t think I would ever have invented this type of lexer pattern in Go by myself, but would be very grateful to come across it in a code base I had to fix. Like Duff’s Device m…

  2 * (1..12).reduce(&:+)
You could also use the closed summation formula: (n^2 + n) / 2. In our case that shortens to just 12*13=156.

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

#273
post #201

Earlier quoted context omitted.

I disagree strongly. This code is very maintainable: it does not have dependencies, it is trivial to test, it is wickedly short, and with the appropriate comment there is no confusion regarding its purpose. Also, its field of applicability is clear from the context: replace this code with a call to fsqrt if you happen to have a fast hardware implementation of it. It is the most easily maintainable code, ever!

the one maintainability metric, and i think is the most important one, is whether it's easy to modify (to make it do something slightly different). You'd be hard pressed to write an inverse square cube without basically rewriting the whole function. There's nothing that can be reused. The only saving grace is that it is side-effect free, so replacement is trivial, unlike a lot of other code that's not maintainable.

Your metric is only applicable within the context of the problem domain. In 3-d graphics, the inverse square root (1 divided by the square root of x) is an extremely common operation (such as for normalizing vectors), but the inverse cube root pretty much isn't used, so it's safe to assume you will not need to extend it to arbitrary powers.

As the designer of the code, you would understand that the inverse square root is a standalone problem.

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

#274
This number:

  4856507896573978293098418946942861377074420873513579240196520736 6869851340104723744696879743992611751097377770102744752804905883 
  1384037549709987909653955227011712157025974666993240226834596619 6060348517424977358468518855674570257125474999648219418465571008 
  4119086259716947970799152004866709975923596061320725973797993618 8606316914473588300245336972781813914797955513399949394882899846 
  9178361001825978901031601961835034344895687053845208538045842415 6548248893338047475871128339598968522325446084089711197712769412 
  0795862440547161321005006459820176961771809478113622002723448272 2493232595472346880029277764979061481298404283457201463489685471 
  6908235473783566197218622496943162271666393905543024156473292485 5248991225739466548627140482117138124388217717602984125524464744 
  5055834628144883356319027253195904392838737640739168912579240550 1562088978716337599910788708490815909754801928576845198859630532 
  3823490558092032999603234471140776019847163531161713078576084862 2363702835701049612595681846785965333100770179916146744725492728 
  3348691600064758591746278121269007351830924153010630289329566584 3662000800476778967984382090797619859493646309380586336721469695 
  9750279687712057249966669805614533820741203159337703099491527469 1835659376210222006812679827344576093802030447912277498091795593 
  8387121000588766689258448700470772552497060444652127130404321182 610103591186476662963858495087448497373476861420880529443
This is a 1811-digit illegal prime number, which when unpacked into binary, becomes a compressed ELF executable which will decrypt DVDs. This was one of many ways used to enable people who had legally purchased a DVD with copy protection to play it on Linux. Here's the story behind finding the prime: https://web.archive.org/web/20070223075434/http://asdf.org/~...

Aside from that, the qrpff perl scripts that became t-shirts (https://web.archive.org/web/20011221024307/http://www.copyle...) were another fun and illegal way to point out the stupidity of the DMCA. But they aren't so pretty ;)

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

#275

Any of Norvig's notebooks, http://www.norvig.com/ipython/README.html , especially his Concrete Introduction to Probability https://nbviewer.jupyter.org/url/norvig.com/ipython/Probabil... .

Oh my God, his Lisp interpreter in Python: amazing. At the time I was just getting a grip with Python and starting in Lisp, it came at just the right moment in my autodidaction. Really really good, although for a lot of people here it might be a little elementary (but then, the best code always feels elementary even when doing something advanced!)

https://norvig.com/lispy.html

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

#276
post #37
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...

In action: https://youtube.com/watch?v=m9joBLOZVEo

...or in a live emulator :)

https://floooh.github.io/tiny8bit/c64.html?input=${wait:200}...

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

#277
post #237
post #66

I 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…

The true beauty of Logo comes in the realization that you can add your own words to it, and that all these specialist words you devise are themselves first-class citizens of the language, of syntax and status equal to its generic builtins. Hence: TO CIRCLE REPEAT 360 [FD 1 RT 1] END TO FLOWER REPEAT 20 [CIRCLE RT 18] END and so on ad infinitum, until you arrive at a complete custom vocabulary that concisely and preci…

Thanks for writing this. I think you've explained why I've always liked the ability to invoke functions without parentheses. I always thought it simply appealed to my sense of aesthetics, and I've had somewhat of a hard time defending it but now that you mention it, building my own first-class primitives is a big part of it.

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

#279
post #66

I 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…

For all the Logo fans in this thread, I have created a Slack workspace here for us to get together: https://bit.ly/fd100slackinvite

Please do join it even if you don't remember Logo anymore. The intention here is not to discuss Logo but to share the joy of computing that we discovered through Logo and has remained in our lives. I hope to see you all there. :-)

By the way, there is also #fd100 channel on Freenode IRC but I am not sure whether most HN users prefer IRC or Slack.

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

#280
post #81

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

Oh? What podcast?
Post reply on HN