Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: What piece of code/codebase blew your mind when you saw it?
293 points by Decabytes on Oct 31, 2022 | hide | past | favorite | 284 comments
Mine was when I learned a subset of recursion called mutual recursion. It was for a pair of function to determine if a number was odd or even.

(define (odd? x)

  (cond


    [(zero? x) #f]


    [else (even? (sub1 x))]))

(define (even? x)

  (cond


    [(zero? x) #t]


    [else (odd? (sub1 x))]))



https://en.wikipedia.org/wiki/Duff%27s_device

  send(to, from, count)
  register short *to, *from;
  register count;
  {
      register n = (count + 7) / 8;
      switch (count % 8) {
      case 0: do { *to = *from++;
      case 7:      *to = *from++;
      case 6:      *to = *from++;
      case 5:      *to = *from++;
      case 4:      *to = *from++;
      case 3:      *to = *from++;
      case 2:      *to = *from++;
      case 1:      *to = *from++;
              } while (--n > 0);
      }
  }


Everybody mentions this, and what's great is that it is a pretty natural solution to a lot of problems. I remember coming up with a version of it while writing an optimized prime-sieve[1], and was surprised when I later learned that it was some named technique.

In addition to just the basic loop-unrolling (which I'm pretty sure you usually don't need to do by hand with modern compilers), it works really well when you need to jump into the middle of a pattern. Like if you're sieving primes in a wheel-factorized array.

[1] https://github.com/patricksjackson/primes-cpp/blob/master/pr...

  // Here we're only checking possible primes after wheel-factorization
  switch ((p.second/num)%30) do {
    case  1: target->set(n); n+=num*6;
    case  7: target->set(n); n+=num*4;
    case 11: target->set(n); n+=num*2;
    case 13: target->set(n); n+=num*4;
    case 17: target->set(n); n+=num*2;
    case 19: target->set(n); n+=num*4;
    case 23: target->set(n); n+=num*6;
    case 29: target->set(n); n+=num*2;
  } while(n <= high - range.first);


For a long time, I would at this about once a year and almost understand what it does, but never quite getting it. It felt like looking at a hypercube.

Then I read it was about loop unrolling, and suddenly it clicked.


And also the fact that C sometimes cares way less than you might think about what goes where. :)


Another good example of this flexibility in C is the inverse array notation (I think that’s what it’s called, turns out it’s impossible to search because all the results are about reversing arrays).

arr[index] is equivalent to index[arr]

This works because arr points to the memory location of the zeroth index of the array and index is the offset from that memory location. Since addition is commutative so too is the array notation.


Right, arr[index] is sugar for *(arr+index) which is then also *(index+arr) and index[arr]

edit: fixed escaped asterisks :-)


That sounds soo dirty though


It is[0]. Duff's device, whether you like it or hate it, was a performance optimization at least, saying "idx[arr]" instead of "arr[idx]" does not help in any way and will make some people angry or sad.

Contestants in the International Obfuscated C Code Contest will (and probably already have) find delightful ways to use and abuse this syntactical curiosity.

[0] My one-track mind always wants to jump to pirate jokes.

EDIT: If you like to explore this area, I highly recommend "Expert C Programming - Deep C Secrets" (get the pun? There's a fish on the cover...) by Peter Van Der Linden. It's pretty old (it talks about DOS memory models, among other things), but it is a fun deep dive into the murky corners of C. I wish there was an updated version and/or books covering other languages in that style.


> Contestants in the International Obfuscated C Code Contest will (and probably already have) find delightful ways to use and abuse this syntactical curiosity.

The OCCC is where I learned about this in the first place!

Surely it counts as useful if it could be used to confuse The Enemy. ;)


Afterwards, I started to like Duff's description of his device as being a strong argument in the debate on switch's fallthrough behavior, but that he did not know if it was pro or con.


Yeah the duffs device did it for me too. I remember reading it in some game programming book but shocked to see it used in qt/embedded for it's software rendering.

This is up there for me or there's also that amazing recursive template class for n-dimentional vectorspaces written by Tim Sweeney. I can't find the page anymore but there is reference to it in the archive https://www.flipcode.com/archives/Unrolling_Loops_With_Meta-...


I encountered this technique and I learned the concept of coroutines, it was quite something.


I remember something about that too. Was this the blog you read? https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html


Hehe. yeah, exactly. And Putty was the ssh client I had used for a while on Windows.


That was an excellent read. Thanks for sharing.

Always love blog posts that cite Knuth.


Shouldn’t it be

*to++ = *from++;


In the original code, actually, no, incrementing "to" was not desired. The "to" was pointing to a hardware memory-mapped register, and it needed to copy to the same memory location each time.


Didn’t know that. Thanks


amen. seems like back in the day that "reg1 = *pc++;" would have been the fastest op in a computer.


The Legendary Fast Inverse Square Root with "Voodoo Black Magic"

https://medium.com/hard-mode/the-legendary-fast-inverse-squa...


That one still kills me. By Carmack's comment, surprised him too.


this is not just source code, it is much better to be called magic.

also love the story behind it.


Here’s a paper that explains how to derive that magic constant and others depending on what measure of error to minimize in the sqrt approximation.

http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf


a tangential question is whether ID software actually had any right to license Q3A under GPL if they had not authored this piece of code


10/10


Quine Relay, "a Ruby program that generates Rust program that generates Scala program that generates ...(through 128 languages in total)... REXX program that generates the original Ruby code again."

https://github.com/mame/quine-relay

(HN thread: https://news.ycombinator.com/item?id=6048761)


They were so preoccupied with whether they could, they didn't stop to think if they should.


What a strange loop!


How does one come up with this? Like where do you start?


Presumably with a version using two languages.


This is just unbelievable. wow.


I've read and re-implemented a lot of stuff by Inigo Quilez. Much of it is ingenious but eventually straightforward once you get the hang of it. I still find this particular snippet magical:

https://www.shadertoy.com/view/MdB3Dw

Well, the code itself is simple GLSL. The math is (and I have not verified this) not that complex either, as I believe it's all about integrating the raytracing equation for a moving sphere over the time variable. I guess what I find most impressive is just the fact that it works and that someone even thought of doing that. I also think this is currently the only example of this method being used anywhere, and it might mostly stay that way because the method might not be super generalisable.


He has a YouTube channel as well: https://youtu.be/8--5LwHRhjk

Also you may like Marble Marcher made by Code Parade, it's a game that uses a similar approach to rendering: https://youtu.be/9U0XVdvQwAI


It sounds similar to a technique we used in video games to solve collisions so you didn't miss the interval - using quadratics to solve the swept sphere.

In fact so much of what is on shadertoy looks very familiar to video game 'game physics' programmers because a lot of it is related to collision detection. See the Orange book on real-time collision detection (Christer Ericson) for a lot of good code.


Thanks, your description of the technique is simpler and probably more accurate than what I guessed - no integration required. The author gives a similar hint in the comments.


His “Painting a Character with Maths” video is also incredibly mind-blowing.

https://www.youtube.com/watch?v=8--5LwHRhjk


> I believe it's all about integrating the raytracing equation for a moving sphere over the time variable

In all of the samples there is some iteration constant you can adjust. Take this below a certain threshold and things get really interesting. You can almost reason about how it's all working mathematically by plugging in different iteration counts.


He's amazing.


https://github.com/chrislgarry/Apollo-11/blob/a13539c7c5c482...

My mind was blown that I could read Apollo 11 source code. It was additionally blown that it included comments like, "# TEMPORARY, I HOPE HOPE HOPE"


Not mind blowing, but calling a subroutine BURNBABY in your moon launch code is just great.

https://github.com/chrislgarry/Apollo-11/blob/a13539c7c5c482...


Also finding out the source was similar to open source quad copter code with perhaps one the first usages of a Kalman filter to integrate various instruments to maintain level flight was both mind blowing and very satisfying.


Not only can you read it, you can also run it!

https://www.ibiblio.org/apollo/index.html


Years ago, fresh out of training, I got a job that involved image recognition, and I spent a couple of weeks reading the code dealing with image processing. I was confused, until I realized that all the operations were basically the same, the only difference was the matrix of weights applied to the pixels.

That same week, I had a chat with a friend who at the time was dabbling in audio processing, and we tried to understand how lowpass and hipass filters worked. When we did understand it, I realized they worked basically the same as the image processing operations, just with a temporal aspect to it.


Once you learn that everything can be looked at in the frequency domain, things can get pretty spicy.

DSP was easily the most impactful coursework I ever took at university. It applies literally everywhere. Information theory is at the heart of it all, and once you start to see how it all fits together, entire realms of possibilities open their doors to you. In our course we did a lot of image and audio work to help demonstrate that this concept applies to any domain. Discrete/continuous time domain, Some array of bytes on disk, doesn't really matter. You just need some rational way to quantize & sample your data.

Also, as you get into control theory... more matrices! You can use state space representation (a matrix) to represent the entire valid control space of an N-dimensional system (albeit one that is LTI and where N < infinity).


Do you have any textbooks, course materials, or other references for these topics you'd recommend?


When I was an EE student (~2010), the book "Digital Signal Processing" by Steven W. Smith was considered to be the bible of DSP.

You can read it for free online on his website here: https://www.dspguide.com/ or get a physical copy here https://www.amazon.com/Digital-Signal-Processing-Practical-S...


The textbook that we used in the course was this one:

https://www.amazon.com/Signal-Processing-First-James-McClell...

I don't think this is considered a fantastic book. The professor was simply amazing.


I remember the moment my brain went "So that's what some people need a fast multiply-accumulate for!"


If you want to play with DSP, I highly recommend just playing around with GNU Radio, it works well with audio frequencies, and can use your sound devices for I/O.

Finally understanding what a negative frequency really meant was an interesting shift in thought.


yep, I'm currently taking a systems/signals course and it was incredibly illuminating how a lot of common image and sound filters -- edge detection, low and high pass filtering, equalization, etc -- boil down to the convolution of the signal with the impulse response of the filter.


This is a decent portion of MRI image processing, and the progress being made by software engineers at the moment is nothing short of spectacular.


I would focus on the differences between IIR and FIR filters if you really want to get your hands dirty. Pros and cons of each. That kind of thing.


Thanks for the pointers! I'm very much a noob, this is really the first formal class I've taken on the topic. Are there any books, papers, or websites that stand out as particularly useful for grasping these topics?


I've found most elucidation come out of labs and practical work.

I learned a lot about FIR filters by trying to do room audio correction on my own. That was where the latency tradeoff made itself apparent to me in a very obvious way.

Reading it in a textbook is one thing, seeing/hearing live signals pass through your own filters is another. I think audio lends itself better to experimentation. You can actually construct passive filters and validate your assumptions with live equipment and not spend a dime. You likely already have all of the required gear in your office, short of some spare inductors, capacitors, etc.

Image DSP work can be done entirely on a MacBook on an airplane, but I have personally found the added dimensionality to be less intuitive than with a time-domain signal.


> boil down to the convolution of the signal with the impulse response of the filter

Not to be "that guy" but any linear, translation-invariant operator can be written that way. This is the essence of harmonic analysis.


Another cool thread here is the Linear Canonical Transform[1], a generalization of the Fourier and other transforms.

[1]: https://en.wikipedia.org/wiki/Linear_canonical_transformatio...


I really don’t like that example. I get that it’s just demonstrating mutual recursion and not intended to be practical, but I feel like there could be other ways to demonstrate that concept that aren’t so inefficient.

My answer would be the jQuery code base, back in the day when that was popular. At the time I didn’t know a whole lot about Javascript or the DOM. I remember it feeling like the kind of code you could just read top to bottom and it was almost like reading a novel. Really well-organized, well-commented, and effective. I don’t know if it’s still like that, but at the time it was transformative for me.


I remember similarly reading the jQuery codebase in awe, back before the MVC rush and the thousands of posh libraries. It intimidated me that someone could write code so polished, and was a big driver for me years later when I published my first popular open source library.


Similarly, backbone.js!


I was mind blown in college when learning recursive programming and being introduced to the recursive solution to the Tower of Hanoi problem:

- https://en.wikipedia.org/wiki/Tower_of_Hanoi#Recursive_imple...

I remeber thinking "where's the rest of the code? It surely can't be just those lines!"


Recursion finally clicked for me after learning induction where you assume you already have a working solution for all cases below n and your only job is to implement the nth case


I remember figuring out by myself the reclusive solution to tower of Hanoi back in high school AP comp sci class as extra credit. It was a shock that the solution is so simple and elegant.


Wow this is beautiful. I love it when a complicated process can be simplified into such logical steps, that you know work in isolation, so you know the strategy will also work overall. Thx for sharing!


I had the exact same experience too. Really blew my mind.


Fascinating how many of these comments are about recursion. My mind was blown when I realized that recursion is almost never the right solution in practical applications due to memory and stack exhaustion concerns (and that introductory CS/programming courses usually do a disservice to students by treating it as a core concept without mentioning that caveat).

My mind is always blown by the tiny demoscene demos.


> never the right solution in practical applications due to memory and stack exhaustion

This is not true with a good compiler that does, e.g., tail call optimization, or is it?


TCO is not supported in most languages that are practical for large scale software engineering projects today. It's also not applicable in many recursion scenarios that they teach in school.

Even in languages where TCO is supported, it's usually not safe to rely on from an engineer's perspective because there is usually no way to reliably assert that TCO is actually applied to any given function.

And again, when they teach recursion in school, they don't normally tell you that it can be unsafe unless your compiler happens to apply TCO or your language explicitly supports tail recursion.


I see your point about education. Everything has its place and a for loop can sometimes be easier to read. But some algorithms are more naturally expressed recursively.

A lot of languages do support TCO. For example, Scala (used by Twitter), OCaml (used by Jane Street for everything, used as the host language for Coq, originally used to implement the Rust compiler), Kotlin, Haskell, Clojure, Lua (used as an embedded language in many places), Elixir, Perl. Not necessarily your popular bread and butter languages but definitely used in production and available if necessary.

Your point that it is generally difficult to know whether TCO is applied is also well-taken. But in OCaml, you can annotate the recursive function call with `[@tailcall]` to verify that the compiler performs TCO.[1] Likewise, you can annotate your functions in Scala.[2] In languages without such annotations, one can get a sense by memory profiling (possibly not emphasized enough in those intro CS courses).

[1] https://v2.ocaml.org/manual/tail_mod_cons.html

[2] https://www.scala-lang.org/api/2.12.1/scala/annotation/tailr...


Good points, hopefully this is educational to people reading this. I'd love to see more ergonomic TCO in more languages.


The only time I've ever ran into a stack limitation was real-time embedded systems with a tiny stack.

For modern computers/tablets I have never experienced an issue. Granted, what really matters is your data/recursion level, but even hundreds of recursive calls are not a problem for most applications.

Recursive solutions are usually so much easier to understand than stack/array based looping solutions that they are my go to for things like tree traversals or searching.


> for most applications

A very subjective statement. Yes, your data matters. Also, the scale and security model/trust boundary of your application matters.

Some actual problems with recursion based algorithms:

* They will break unexpectedly as you scale them up.

* They are not memory efficient, so you won't be able to process much in parallel if your data is non-trivial.

* They can make your service trivially DoSable, so now you have to worry about either sanitizing your input or monitoring your stack instead of just timeouts/rate limits.

I've lost track of the number of times I've had to tweak JVM settings, write up security issues, or just straight up tell people things won't work because an academic researcher decided to implement an algorithm with recursion, and then engineers were asked to productize it.


Not code or a codebase - but reading Eloquent Ruby every morning on the DC Metro made the commute to work fly by in an instant and kinda blew my mind. I will try to illustrate the stuff that blew my mind below:

Quick backstory: a feature of Ruby (that you tend to either love or hate) is that you do not need to use parentheses to call a function.

if you have a function like so

    def prefix_hello(name)
      "hello, #{name}!"
    end
you can call it like this

    prefix_hello "fred"
    => "hello, fred!"
After learning about some of the spiritual ancestors of Ruby (like Smalltalk), message passing, learning everything is just an object and function calls are just messages being sent to objects, a lot of things clicked immediately for me. It was kind of a mind blown moment.

Take the `+` (add) symbol in `5 + 3` - in essence, it's just a regular method that is literally caled `def +(number)`, which belongs to the int/number object.

In laymans terms this code is saying, hey object (number) '5', call your + method with '3' as the argument.

You can simulate this in an `irb` shell:

    irb(main):001:0> x = 10
    => 10
    irb(main):002:0> x.send(:+, 10)
    => 20
It's all right there! `objectA.send(methodName, arg, arg, arg)`

Then you realize that this is true of many languages, Python included (__add__)... but I do think Ruby does this in a particularly, ahem, eloquent way that really impressed me.

This moment in my career/learning was almost like a springboard/catalyst and unlocked a lot of additional learning down the road, like getting involved with Erlang and Lisp/Clojure.


Handmade Hero.

Before warching Casey program, it had not occurred to me you could sit down with a compiler and write an entire video game from scratch.

Then, I found out Jonathan Blow sat down and wrote his own compiler, followed by an engine, and now has a game in flight.

It's a truly incredible what these guys (and their teams) get done.


I'll see your Handmade Hero and raise you "But without a compiler".

Specifically, Roller Coaster Tycoon, hand-written in x86 assembler by Chris Sawyer.


Damn is the source code for that available somewhere? I loved that game.

Edit: guess I could just download it and disassemble ;)


Not really. It's actually more difficult than disassembling a C codebase. Chris used a macro assembler and wrote _a lot_ of macros. You'd only see what the assembler expanded, but you'd have no idea how the code _actually_ looked like.


Right, thats what I assumed. TIL!


Assembler is low-level C. Also probably easy to learn compared to Rust. ;-)

What impresses me though are old Atari games that were implemented in hardware without a microprocessor.


I've been thinking a lot lately about those two examples related to experience in programming, domain knowledge and producing good work. Jon had written 12 compilers by the time he got to Jai, and many more games before his Sokoban, and he started making games as a contemporary to Abrash, in an era where you had to really learn what was going on to produce something good. That knowledge from constrained environments adapted really well to the increasing power of computers (both in terms of processing power and ergonomics), whereas coming at it from a modern programmer's POV you can get away with very little in terms of know-how (which is both good and bad)

I'm working on trying to reach their level (it is reachable) but I sometimes feel like I started with a handicap of comfort from modern OSs and tools


Reminds me of the author of the K programming language, Arthur Whitney, wrote is own language, then database, and now he's working on his own OS.


Speaking of K, a way to generate the first N+1 Fibonacci numbers is what made me take notice of the language:

    N(|+\)\1 1
I'm not sure where I first saw this snippet (maybe in the Kona documentation) but it stuck with me enough that I was able to recreate it from memory.


Hey, could you recommend an episode of Handmade Hero to get started with? Should you just start at the very beginning or somewhere later in the process?


Its a pretty big comittment at this point, but if youve got the time I'd recommend starting at the beginning. There's also an annotated episode guide if you wanted to learn about a specific topic in a more targeted way


Big fan of both Casey's and Jon's streams.


Did Blow really finish the compiler? Got a link?

Last time I checked (a couple of months ago) there wasn't even a formal language spec yet.


It's been in closed beta for a couple years now IIRC. His approach is very different from what's prevalent in the industry today - he prefers to only release a product that's reasonably well done, instead of frustrating his audience with a buggy moving target. That's why the language is in its seventh/eight year of closed development at this point.


AFAIK it is done enough to be in closed beta


Definitely this. Lots of good practical advice in his videos too.


Actually Portable Executables / Cosmopolitan

https://justine.lol/ape.html https://github.com/jart/cosmopolitan


qmail was the first codebase I read that blew a lot of assumptions I had about "good code" and "clean design" out of the water.

It went its own way on a lot of things compared to GNU projects and achieved an enormous level of success for 10-15 years despite some really weird decisions. And even the "wrong" decisions, with hindsight, stemmed from a clear desire to keep creative control and a small codebase. I'd rather read well-documented "bad" code any day of the week.

Since then I've felt a bit of rebellion against standard practice is often a valuable if it's 100% aligned with the goal of your project - reinventing a wheel, ignoring portability, picking a weird language, a new storage format, that kind of thing.

For an engineer it's so easy to get sucked into assuming you should make standard, well-supported, well-understood choices that you suppress your more creative & judgmental instincts.


Ken Thompson's self-perpetuating hack on the c compiler and login.c to create a back door that is invisible in the compiler source code and the source code of login.c. Described in his Turing Award acceptance speech, "Reflections on Trusting Trust".


Which apparently was also implemented!


In python the inverse of the zip function is zip. You call it with with * to make list elements into function arguments. So a, b = zip(*c) is the inverse of c = zip(a, b).


I think it only works out that way when you're unpacking the same number of variables as there are elements in c, which is the same as (a,b) = c and c = (a,b).

If you tried something like c = (('d','e','f'),('g','h')) and then a,b = zip(*c) you'd get a = ('d','g') and b = ('e','h'). The 'f' that was originally at c[0][2] is dropped so you can't zip back to the original value of c, unless that's how inverses work for this scenario and my math is rusty.


It works only because zip drops extra items. I explained how it works in the reply below.


The expansion of c is a tuple of pairs, zip then creates an iterator (iirc zips get exhausted so iterator and not iterable); the unpack then calls dunder iter of the iterator. The iterator returns itself; and the destructing expands the iterator to exactly two items and binds them to a,b


destructuring, not destructing.


A binary rate-monotonic scheduler. It was in the Ada realtime executive for an avionics box (there was no OS). It executed tasks at 5 rates, each half as fast as the previous one. It was dead simple, something like (pseudocode)

    counter = 1

    do highest priority stuff
    case counter
        when 1, 3, 5, 7, 9, 11, 13, 15
            do high priority stuff
        when 2, 6, 10, 14
            do medium priority stuff
        when 4, 12
            do low priority stuff
        when 8
            do lowest priority stuff (1st half)
        when 16
            do lowest priority stuff (2nd half)
    end

    counter++
    if counter > 16
        counter = 1
    end
This was called on a timer interrupt.

I was like "what the heck does this do?" followed by "that sure reminds me of binary trees" and finally "whoa! cool!"


This is quite close to how it would be done in hardware. I find that after writing Verilog/VHDL you tend to write code like this.


For me it was the "Make a Lisp" project. Reading the architectural diagram of a Lisp interpreter, and browsing its implementation in many (~87) programming languages.

https://github.com/kanaka/mal

Especially where the guide explains how tail-call optimization works, my mind was blown.

https://github.com/kanaka/mal/blob/master/process/guide.md#s...

Studying the project changed the way I understand code. Since then I've created my own little Lisps in about three or four versions/languages. Next I'd like to write one in WebAssembly Text format, which is already in a Lisp-shaped syntax.


I had the same thought, https://github.com/pollrobots/scheme


I went through and commented the Perl tokeniser (toke.c) back in the late 90s/early 2000s, and my mind was blown by the "is this regular expression syntax involving $ or is it a variable interpolation involving $"? logic which was basically "here are some things we might have seen, make a weighted sum of which were actually seen and then if it's greater than x, let's assume it is variable interpolation".

You'll note that we were busting that clean separation between tokeniser and parser. Yup, sometimes it's hard to DWIM and still stay in your lane.

Just checked and, thanks to the hard work of far smarter people than myself, the whole thing has been refactored and now the better code has better comments. Progress is wonderful! (I couldn't find the weighted guess, so maybe they found a way to work around it)


Here's the first that blew my mind, from the early 1980s: It was a BASIC "AI" program that would guess what animal you were thinking of just by asking a few questions. "That's crazy," I thought.

And, it learned more each time you ran it - even more crazy! It was only when I looked at the source code and realized that it was using self-modifying code that I went, "Wait a minute - THAT'S A THING?"

I had so many of those moments. The very first was realizing you could use an iterator variable to do other things within the loop. That's the exact moment when programming clicked for me. My perception of it changed from being a glorified way to write out steps (like a hair-washing algorithm on a shampoo bottle) to realizing it could be very expressive and flexible.


Well, .NET's garbage collector certainly springs to mind. Not exactly in a good way. You know you have something special on your hands when GitHub refuses to show you a highlighted version of a hand written C++ file (https://github.com/dotnet/runtime/blob/98984ccad55362a66b7fd...).

Other than that, I remember my mind was blown a few times while watching Jim Weirich's presentation on functional programming all those years ago (https://www.youtube.com/watch?v=FITJMJjASUs).


You say hand written but actually, it wasn’t exactly. The original version was apparently in LISP and then translated to C++ [0].

[0] - https://learn.microsoft.com/en-us/archive/blogs/patrick_duss...


The GC code does look like a collection of garbage


Sometimes the garbage takes out itself.


Many MS libraries are just total garbage, it's crazy. Reading the source and looking at the bugs (features just flat out not working, must have never been tested) is mind opening.


The GC is the only thing I strongly dislike about the .NET ecosystem. I would really like some option to turn it off altogether (like what Java offers).

I've got several short-lived applications that are extremely latency sensitive and would work flawlessly this way.


Out of curiosity, do TryStartNoGCRegion and/or GCLatencyMode.SustainedLowLatency not meet your needs?


I've not had luck with either. The application may allocate more than a single GC segment worth, so the best workaround is to detect an imminent GC and migrate the workload. Unfortunately, migration would incur even more severe penalties than a typical gen2 collection.


The OPs example blows my mind but not in the way originally intended. Wow, it managed to make a trivial constant-time operation into an exponential-time operation. This is a mind-blowingly bad example of recursion.

It's like trying to enter a house by demolishing it and rebuilding it around you instead of opening the door and walking in.


How is it exponential time? It should be linear as it loops through at most `n` times. My guess is that it would also be optimized for tail recursion so it wouldn't clobber the call stack either.

That said - you're right that it's still slower than constant time


Because it only takes log(n) digits to represent n.

9 is 1 digit, takes 9 steps.

99 is 2 digits, takes 99 steps.

999 is 3 digits, takes 999 steps.

An input d decimal digits long takes O(10^d) steps.

The runtime is exponential in the "size of the input", because the size of the input is usually taken to be the length of the binary or decimal representation of the number rather than the magnitude of the number.


Thanks to Haskell's laziness, the following implementation of `minimum` is O(n):

  import Data.List (sortBy)

  minimum :: Ord a => [a] -> a
  minimum = head . sortBy compare
`sortBy` is O(n log n). But `head` only needs the first element which can be found in linear time. So `sortBy` does not compute the rest and `minimum` is O(n).


Arthur Whitney's compiler for a language "isomorphic to c": https://kparc.com/b/.

Amazing that someone can write correct, fast code like that.


Arthur Whitney is like the Ramanujan of programming. This quote from Hardy about Ramanujan could easily apply to Whitney as well:

"The formulae... defeated me completely; I had never seen anything in the least like them before. A single look at them is enough to show that they could only have been written by a mathematician of the highest class. They must be true because, if they were not true, no one would have the imagination to invent them."


The Peter Norvig spellchecker: https://norvig.com/spell-correct.html

I like it because it's so clearly coded - there are no crazy syntax tricks or hacks - it's just obviously the right way to write it. And yet it is compact, elegant and non-obvious if you are writing it yourself.


The original ADVENT game in FORTRAN. It was way, way beyond my pedestrian programming. It even had OOP in it: "a troll is a modified dwarf". And it did the marvelous thing of once it had gone through the world-building initialization, which took some time, it would patch its own executable with the results!

I used that trick for years until the virus scanners shut down that sort of activity. Sigh. This is why we can't have nice things.


LFSR's are mind blowing; the fact that you can generate a randomish sequence that covers an entire ^2 range visiting each number once, and only once, seems like a lucky piece of magic.

And in just a couple of lines, excuse my quickie untested code:

  #include <stdio.h>
  #include <stdint.h>

  // 5-bit maximal LSFR taps to get pseudo-random sequence of 31 unique numbers in range 1-31
  // 0x12, 0x14, 0x17, 0x1B, 0x1D, 0x1E, 0x18, 0x17
  int main(int c, char **v) {

    uint8_t taps = 0x17; // could be any of the taps above
    uint8_t seed = 0x7;  // start, could be 1-31
    uint8_t lfsr = seed;

    do {
      printf("%d\n", lfsr);
   
      lfsr = (lfsr & 1)
             ? (lfsr >> 1) ^ taps
             : (lfsr >> 1);
    } while ( lfsr != seed );

    return 0;
  }


well, almost every number ....


All except that phony made-up one...lol


Swap two variables using XOR:

    x ^= y;
    y ^= x;
    x ^= y;
Check if the number is even:

    x & 1 == 0


... unless x and y are the same.


For me it was as simple as when I could start doing functional programming with javascript.

I was so surprised, since I had learned the other way first that it is actually easier to teach functional approaches with teenagers.

arr.map(x => x2) was abundantly clearer than

const newArr = [] for(let i, i < arr.length, i++) { newArr.push(arr[i]2) }

And to be perfectly honest, I might have goofed the for loop.


> I might have goofed the for loop.

Yeah, you did! It should have used semi-colons. (And HN has lost the carets …)

And I agree completely with you.


Something I've started doing along with the functional is fallback assignments and relying on Set operations to be idempotent. This can tidy up by removing a lot of excess conditionals at the cost of some extra operations that are actually VERY fast so in a most cases prob won't be a problem.


Have you used fp-ts?


this is a huge milestone in my coding. i remember refactoring a bunch of my code as soon as i learned how to use map functions lol


The javascript array functions and arrow functions are a gift from heaven.

And to be perfectly honest, I might have goofed the for loop.

You did, the commas must be semicolons :)


Functional JavaScript has saved me so much. And I'm not talkin Ramda, just functional principles.

I can't tell you how much time I spent chasing a bug when it was a date that was passed by reference.

Rather than trying to keep track, I just return copies of arrays and objects and it's way easier.

Even a simple

``` function yeppers(input) => ({...input, x: 7}) ```

(An overly simple example)

has made things smoother.


I miss that ability to just pass whatever around easily in JS.

Ok… it’s Footgun prone and callback hell is real.

But I still try to obtain that in other more boring language.


It has long since been cleaned up, but the history of Lucene's Levenshtein automatons are a fascinating mix of pragmatism, hackery, grit, and engineering. Using a Python library to generate crazy-looking Java code, which you can't (yet) understand, but whose tests pass and benchmarks look amazing.

- https://blog.mikemccandless.com/2011/03/lucenes-fuzzyquery-i... - https://www.youtube.com/watch?v=4AbcvHKX8Ow

(Levenshtein distances are used as a measure between fuzzy/misspelled matches, and was ironically misspelled initially - https://lucene.apache.org/core/7_4_0/suggest/org/apache/luce...)


Lots of good responses here, so I’ll give an example that blew my mind in a negative way:

Fortran IV code, thousands of lines, no functions, only GOTOs jumping everywhere randomly. Mostly one or two letter variables, lots of GOTOs that turned out to be loops, with lots more jumping into the middle of them and jumping out of them into other loops. No documentation, I was supposed to modularize it. Failed.

But it successfully calculated thermodynamic properties for engineering designs, so go figure.


Hashlife.

I'm pretty sure that if something of the sort had occurred to me before I came across it, I would have thought "that'll never work" and moved on without taking it seriously.


I'm partial to this snippet from SICP (4.2.3 Streams as Lazy Lists):

    (define (cons x y) (lambda (m) (m x y)))
    (define (car z) (z (lambda (p q) p)))
    (define (cdr z) (z (lambda (p q) q)))
This implements cons-style linked lists in terms of only functions. In other words lists need not be the fundamental construct of a Lisp/Scheme—you can use functions instead.


I love learning programming, and also math, because I feel like my mind is blown every time I come across a new concept.

However, it's rare for that feeling to obtain just by programming in a language. One language that (so far) has consistently had that feeling even when simply programming in it is Dyalog APL.

To think that it's so expressive that naming things is often unnecessary, the name being the function itself.... It feels like I'm playing with raw essence.

+/÷≢ is the classic example, of course. That's four characters -- the "tally" character at the end tends to render wrong in browsers, but I assure you it really is only one character. Four characters! What is the name of the function? Either "Mean", or "Average", one of which is four characters itself, and the other longer. Of course, you could just say, "Avg", but why bother?

And this contains in itself another example. +/ being "Sum", which is longer. And "Product" is an even higher contrast: ×/

This language also has the added mind-blowing feature where the glyphs are often visually related when they are functionally related.

So "Scan" is \. Therefore, "Running Sum" aka "Plus Scan" is +\. Likewise "Running Product" aka "Multiply Scan" is -- you guessed it! -- ×\.

And there is so much more. I've been doing APL for a while now (IDR if it's a full year yet or not, but it's getting close I think), and it keeps blowing my mind over and over again. It's gotten to the point where when writing APL I feel like I'm just spelling out my thoughts for the most part. I've never gotten that feeling in any other language, and it's a mind-blowing feeling.


A snippet of pandas code in a notebook I saw when learning pandas, it looked like this:

  def to_minutes(df, col):
    df[col] = df[col].dt.minutes
    return df

  (
    pd
    .read_csv('data.csv')
    .assign(dur=df.finish-df.start)
    .pipe(to_minutes, col='dur')
    .query('dur < 30')
    .sort_values(by='dur')
    .pipe(
      seaborn.relplot,
      x='dur',
      y='distance',
      hue='city'
    )
  )
It's a simple snippet but the code structure is clever in many ways. The enclosure of the whole expression, starting with pd, into () makes it possible to split method chaining into multiple lines without backslashes and start each line with ".". Now we can treat DataFrame methods similarly to verbs from dplyr data grammar and "." similarly to %>% operator. Its now easy to rearrange, add and delete whole lines without thinking if we forgot to add "." to the line above or below. Everything is immutable, no side effects, it's safe to copy the code in a new notebook cell and tweak it a little without worrying about tens of temporary df2_copy1 variables. The .pipe method allows to avoid "df = df[...]" junk, to reuse code fragments and even to use plotting libraries in ggplot fashion. If the chain becomes huge, just put it in a function and apply in to df using pipe method, etc... I wonder if it's possible to add memorization to pipe method to avoid recalculating the whole construction every time when rerunning the cell during debugging.


Can you please give a link to original source?


How about, that C program which produces random dot (well, character) stereograms, whose source code is such a stereogram?

Winner of 2001 IOCCC:

https://www.ioccc.org/2001/herrmann2.c


Function pointers

I was a self-taught programmer as a teenager so there was a lot of theory and mental models I was missing. I viewed code as something static I wrote into an editor and later interpreted (QBasic) or compiled and ran (C).

Learning about function pointers in C was transformational. I started thinking about code as data that can be called and passed around and even changed at runtime and suddenly things like cooperative multi tasking made sense to me.


Computed gotos are similarly mind-expanding :)


PPR - Pattern-based Perl Recognizer. It's basically a giant regex that matches Perl code.

The regex itself: https://metacpan.org/dist/PPR/source/lib/PPR.pm#L65

Documentation: https://metacpan.org/pod/PPR


That's one impressive Perl file. And so visually clean, especially considering what it does. I was wondering who would even make such a thing, then noticed the author - D. Conway. One of my programming heroes.


Rob Pike’s lexer was very nicely done and struck a chord with me. It’s not nearly as magical as some of the other things here. It’s just great engineering:

https://m.youtube.com/watch?v=HxaD_trXwRE


> The IOCCC Flight Simulator was the winning entry in the 1998 International Obfuscated C Code Contest. It is a flight simulator in under 2 kilobytes of code, complete with relatively accurate 6-degree-of-freedom dynamics, loadable wireframe scenery, and a small instrument panel.

> IOCCC Flight Simulator runs on Unix-like systems with X Windows. As per contest rules, it is in the public domain.

https://blog.aerojockey.com/post/iocccsim

Oh, and the code is shaped as a plane. Pretty awesome.


This .NET Dictionary<Type,T> that allow very cheap lookup when your key is a type. https://github.com/asynkron/protoactor-dotnet/blob/dev/src/P...



For codenbases... Objectweb ASM blew my mind.

It's a library to read/write java class files. The core at that time would fit into 2 java files.

It was a reader and a writer built around a visitor pattern. At that time apache bcel was also big, a library to build in-memory class file representations which you would then manipulate. ASM allowed you to do many transformation without ever holding the whole file in memory.

ASM also had an in-memory representation as an add-on. It was all nicely layered and the core was just a marvelous small piece of code, with incredible low memory consumption.


Arthur Whitney’s J Incunabulum: https://code.jsoftware.com/wiki/Essays/Incunabulum


CPS comes to mind (Continuation passing style). Apparently continuations really do it for me. One of the many gains from picking up functional programming is stumbling upon little "pearls" like these that I wouldn't have otherwise.

https://en.wikibooks.org/wiki/Haskell/Continuation_passing_s...


The Idris codebase is quite beautiful. Given some understanding of how it works underneath, I find that there’s a succinct clarity in everything.

https://github.com/idris-lang/Idris2


Mine was super basic, one of many, that a 0 DAY can push an excel date a day behind, so DATE(2022, 10, 0) means Last Day of October. No need to keep an array of which month has 30, 31 or 28/29 days. Simple DATE(2022,3,0) gives the last day of February.


Lenses, and going from lenses to optics more generally. You start with what looks like a bad reinvention of "dot notation" to deal with updates on nested records, but then as you generalise, it turns into a deep well to draw from. You get an incredibly powerful toolkit to query/summarise/transform/rewrite data structures, and it's the only library I know where compositionality is _enhanced_ by _exposing_ its inner workings:

Here is SPJ giving a talk on them: https://www.youtube.com/watch?v=k-QwBL9Dia0


Definitely agree on lenses being one of the more mind blowing things I've encountered and eventually grokked in programming. I would also recommend https://www.youtube.com/watch?v=QZy4Yml3LTY as a good video on lenses in part because the latter portion has some pretty far-out examples of that deep well you mentioned.


A book called "Astronomical Algorithms" was written back in the 80's or 90's. It was very exciting and inspirational for me that the sun and moon and the position of planets - nature itself - could be predicted by code. It still is an inspiration for me today.


In a similar vein, Calendrical Calculations by Reingold and Dershowitz.


My first job in 1987 was porting all of Oracle's source code (entirely in C for 32 bit architectures) to a 64 bit Control Data mainframe.

So, that.


Was it particularly well written or optimized in very clever ways? Bob Miner was supposedly a beast of a coder from what I've read, so wouldn't surprise me.


Oddly enough, I never met any of the coders. The entire code base turned up on mag tapes from Belmont, CA. I was working in Toronto. I was very much a newbie, so I didn't form an educated opinion on code quality. There were very few bugs, I do remember that.

There was a lot of code, about 1.5 million lines over two version of Oracle, which included a bunch of applications as well as the RDMS.


More of a shell snippet, but removing duplicate lines with awk is one that comes to my mind:

awk '!seen[$0]++'


The python code for a NN to learn addition.

I know it seems trivial, but back then, it seemed everybody talked about it, there was that coursera course about driving car, the self promotion of towarddatascience.com, video on youtube... but not a single line of code which made sense.

I mean : "Shut the fuck up and show me the actual code !".

I finally found it on github.

I was like : https://www.youtube.com/watch?v=xYHJRhRym1U


IOCCC taught me that form is just as important as function, and often a lot more fun:

https://www.ioccc.org/


Stolen from 4chan. Not malicious, just prints out a string, but in terms of code density is simultaneously some of the smartest and dumbest code I've ever seen. Bonus points if you can actually figure out exactly how it works.

  #include <stdint.h>
  #include <stdio.h>
  int main()
  {
     //the following code should be self-explanatory
     int format = 684837;
     uint64_t freqs[16] = {4557704611105873944,6293041952211349568,7232032755832676440,9836271091686599272,11342418091143829394,7666925796986299039,8289648};
     uint64_t phases[16] = {2745812699376323861,4477710776532278558,4107622644641439798,4062343665106426382,4257685111635071841,2598073413584315427,1713226};
     uint64_t composite[32] = {0};
     int i = 0;
     unsigned char frequency = i[(char*)(freqs)];
     unsigned char phase = i[(char*)(phases)];
     while (frequency > 0){
       for (int j=phase;j<2048;j+=frequency){
         (j/64)[composite] |= ((uint64_t)1 << j%64);
       }
       frequency = (i%64)[(char*)(freqs+i/64)];
       phase = (i%64)[(char*)(phases+i/64)];
       i++;
     }
     printf((char*)&format, composite);
     return 0;
  }


a completely blind guess: FFT with encoded "hello world"?


Not exactly, what's happening is vaguely fourier-like but I'm not even sure there's an actual name for what it's doing.


Not programming per se, but: mathematical induction. It feels like cheating to get an infinite number of true statements by cleverly setting up a deductive argument.


The actual y combinator (for recursive anonymous functions) took me a while to understand, and I'm pretty sure that by now I've forgot how it works and would need to take a second look at it.

On the less functional side, Linux linked lists are super smart.


A flight simulator called fly8 had an interesting C structure made of pointers to functions. It was a way to declare a device driver in C.

Years later I could tell my colleagues how a device driver/plugin actually worked (before COM/etc) just with this.


I’ve first seen this in the Quake 2 source as a way to dynamically load the render code, e.g. software/gl: the structs exchanged can be seen here https://github.com/id-Software/Quake-2/blob/372afde46e7defc9...


uefi's boot services are pretty much implemented this exact way.


The doom square root trick. Reducing an expensive function to almost nothing with low error. Many writeups on the web far better than I could give.


I think you mean quakes fast inverse square root? Not aware doom had a clever sqrt routine, but I would be happy to be wrong!


You’re right. Doom used fixed-point math; at the time, hardware floating-point units were far from ubiquitous on the computers people wanted to use to play it.


obligatory //what the fuck?


Doom redefining a circle to be 256 degrees, so that the angle calculations fit into one byte.


*Quake


In Ruby, all of the following statements are true:

    Class.ancestors.include?(Object)
    Object.ancestors.include?(Class)
    Class.is_a?(Object)
    Object.is_a?(Class)
It's beautiful.


Reads like poetry.


Haskell:

    primes = filterPrime [2..]
      where filterPrime (p:xs) = p : filterPrime [x | x <- xs, x `mod` p /= 0]


A version that should confuse people not familiar with Haskell even more:

    primes = 2 : filter isPrime [3..]
    isPrime x = all (\p -> x `mod` p /= 0) $ takeWhile (\p -> p * p <= x) primes
primes is a list of prime numbers. It is defined as number 2 and numbers 3,4,5... for which isPrime is True.

isPrime is a function that checks that x is a prime number by taking numbers whose squares don't exceed x from... primes list... and then making sure they all divide x with a reminder.

It only works because isPrime never touches an unevaluated element of primes list. Otherwise, the program would loop.


This uses trial division to find primes, which is not efficient, and I would not recommend using this implementation.

Instead you should use the Sieve of Eratosthenes algorithm. Unfortunately this algorithm is easier to implement in an imperative, eager fashion than in a lazy, functional fashion.

See the paper "The Genuine Sieve of Eratosthenes" for more details: https://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf


Binary Lambda Calculus [1] (produces an infinite list of booleans instead):

    λ(λ1 (1((λ1 1)(λλλ1(λλ1)((λ4 4 1((λ1 1)(λ2(1 1))))(λλλλ1 3(2(6 4)))))(λλλ4(1 3)))))(λλ1(λλ2)2)
[1] https://tromp.github.io/cl/cl.html


Haskell is one of those that I want to get into, but don't have the bandwidth. I'm getting into elixir and even that has had to require a shift of perspective


The codebase that has most blown my mind is the one I currently work on. It is the biggest steaming pile of garbage I've ever seen. It's a Rails app developed by two guys who had never worked as developers before. They built an MVP that found immediate traction in their market and so they hired some slightly more experienced contractors to add features. It is shockingly poorly designed. We've hired an army of talented people to try to fix things so we can actually get to the point where we can add new features but I'm not sure we'll ever get there.

Until this job I hadn't realized how blessed I had been in my career to work at companies that took software development seriously. I can safely say that every instance of "bad code" I had run into previous to this one was really nothing.

I'm not sure how much longer I'll stay here tbh. The place is toxic for a variety of reasons not just the software.


Data from functions:

(define (cons x y) (lambda (m) (m x y)))

(define (car z) (z (lambda (p q) p)))

(define (cdr z) (z (lambda (p q) q)))


If you like the lambda cons, I recommend checking out Church numerals, representing numbers as functions in lambda calculus. I don't remember all the operations off the top of my head, but iirc calling one number with another is equivalent to multiplication, which I always found beautiful.

Then if you really want to go down the rabbit hole, there's SKI, which is lambda calculus without a lambda operator, just 3 named, curried functions(S, K and I, but I is techically definable in terms of S and K). Any function that can be expressed in lambda calculus can be expressed in terms of calling these various functions with eachother as arguments.

Assuming LAMBDA is automatically curried, to save myself some typing:

  (define (k x y)
    x)
  (define (s x y z)
    ((x z) (y z)))
  (define (i x)
    x)
Alternatively, I can be defined as follows:

  (define i (s k k))
SKI is implemented in the Unlambda esolang, with some additional functions defined for IO and convenience


This really surprised me, until I implemented a lisp.

If variable environments are implemented as hash tables, you're just making a pair out of a hash table. In other words, it's basically equivalent to:

    function cons(x, y) {
        return { "first": x, "second": y };
    }

    function car(x) { return x["first"]; }
    function cdr(x) { return x["second"]; }


Swapping two variables without a temporary using xor.


humble brag. I had that on an interview once and I'd never heard of the technique but somehow a inspiration from the heavens and I was able to invent it on the spot. Sometimes knowing something is possible is a great motivator (plus the time pressure of the interview)

But I guess what really happened is that when you come up writing assembly code at a low level it's a lot more obvious when you've been neck deep in bits, shifts, and logic ops all day.



C# as of 7.0 makes this possible:

  (a, b) = (b, a);


It's been possible in other languages for ages, and some of them don't even require parentheses or a semicolon!


How is it implemented?


Generally speaking it will get compiled to something like SSA form and the outcome depends on the overall dataflow and what optimizations happen.


Aw, that's an unsatisfying answer!

I'm a total .NET noob, but this attempt in Compiler Explorer shows it not using the xor trick.

https://godbolt.org/z/jhzqxEKzT

Maybe I need some extra compiler flags or something.


> I’m a total .NET noob, but this attempt in Compiler Explorer shows it not using the xor trick.

It better not. The CLR trick introduces a data dependency that slows down the code on modern CPUs.

Compilers sometimes also can compile this down to zero instructions. Nothing says the variable-to-register mapping has to be constant in a single function.


That would just load each global variable into registers and write them back to memory, swapping them.

If the compiler had decided to put two locals located already in registers, and had the swap in a branch, like so:

    if (foo()) {
        bar();
        (a, b) = (b, a);
    }
Then the registers might get swapped with an xchg instruction or something, I don’t know. The compiler’s goal is to have pipelined execution be as fast as possible, so there’s no way it’s going to use bit-mangling operations that would get in the way of that.


The xor trick has no practical value. Sorry.


It was useful in single accumulator processor architectures since it could avoid one memory store.


I agree it isn't normally helpful on a modern computer, but that's a strong statement. What if you need to swap large storage spaces with no extra memory? Isn't there still old hardware out there especially in aerospace that could benefit?


Also: zeroing a register by XORing it with itself.


Setting a register to all ones by comparing with itself. Much less needed but I've done it a few times.


APL. I didn't care for the particular symbols, but the concept blew my mind, and got me interested in "table oriented programming" (TOP) and later the SMEQL query language (what SQL should have been). I'd often make table-driven logic using dBASE/xBASE back in its day because it was so nimble to edit meta-attributes AND put code in tables with it.

I still believe in data-dictionaries to drive CRUD instead of crap like C#/Java "annotations" on model classes, but our current tools and languages are not TOP-friendly. I'm convinced TOP is the future, I just don't know when. Some say "you lose type checking", but relational integrity can do much the same thing if set up right. I do personal TOP R&D, but the job is too big for 1 hobbyist.



Sleep sort is certainly not O(1), even if there's some disagreement of the correct way of describing it. See https://stackoverflow.com/questions/6474318/what-is-the-time... for some discussion.

If you want to call it O(1), by the same approach you could call any finite algorithm O(1).


RIP /prog/. One of the most brilliantly idiotic communities to grace the web.


A more advanced example of mutual recursion: it's possible to perform an exhaustive search over the "Cantor space" of all infinite sequences of bits that either finds a sequence satisfying a (total, computable) predicate or determines that such a sequence doesn't exist. That is to say, there's a function `search`,

    type Cantor = Natural -> Bit
    search :: (Cantor -> Bool) -> Maybe Cantor
that returns in finite time if its argument does.

Source: https://math.andrej.com/2007/09/28/seemingly-impossible-func...


Euclidean algorithm as a one-line function:

    function gcd(a,b){ return b ? gcd(b, a%b) : a; }


It also has the nice property that it's tail-recursive.


That's brilliantly obnoxious or obnoxiously brilliant. I can't decide which.

If A is less than B it calls itself with the terms reversed.


Same formula in ngn/k: *(*|:)(|!/)/


An oldie but a goodie.


...none. Not because I've never seen mindblowing code, but because I've never understood it for what it was when first seeing it.

I'm a slow learner.


The Unreal Engine 4 code, when it first went open-ish source. I was blown away by how huge it was, but yet, how clean and modular everything was.


Ray Casting Algorithm is neat because it's immediately understandable efficiency https://en.wikipedia.org/wiki/Point_in_polygon


I remember that back to childhood when I simply couldn't comprehend how it worked and it made me sad.


The `@inlineCallbacks` decorator from Twisted. Not the implementation, even, just what it does. Understanding it was what finally made async click in my head.


Richard Bird's Haskell program to solve Sudoku, both the final product and the process of deriving it from a series of less efficient versions, as described in his functional pearl paper [1]

[1] https://www.cs.tufts.edu/~nr/cs257/archive/richard-bird/sudo...


I learned programming informally via scientific project work. When I learned about hashing and O(1) lookup the power of it truly blew my mind.


Any specific books or resources you have in mind?


Yes, I learned this and a number of other nice topics in the coursera algorithms courses by Tim Roughgarden, which I highly recommend.


C++'s "Curiously Recurring Template Pattern"

     template <typename T>
     class Foo {};

     class Bar : public Foo<Bar> { ... };

https://en.cppreference.com/w/cpp/language/crtp


Good answer. OP didn’t specify “blew your mind in a good way”.


If "not in a good way" counts, my nominee would be discovering the hard way that a Windows C function declared as type BOOL can actually return 1,0, or -1.

Now, if it had been declared int, I would probably have been on the lookout for it using something other than 1 or 0 as an error condition, but why bother to explicitly invent a BOOL type and then abuse it that way?



A year or so ago I stumbled across the A-normalization code discussed here:

https://matt.might.net/articles/a-normalization/

I've got a fairly good grasp on recursion, but it took some effort to get a mental model of what was going on (k is what to do with the inside thing), because it's doing both recursion and continuation passing at the same time. It feels like a technique that could come in handy someday.

Not code/codebase, but I also want to mention the transform behind bzip:

https://www.hpl.hp.com/techreports/Compaq-DEC/SRC-RR-124.pdf

I understand how it works, but it seems magical that you can sort all of the rotations, take the last column, and still be able to recover the original text.


Fast inverse square root. Commonly attributed to john carmack, it actually predates his application of it by several years


"... Initial speculation pointed to John Carmack as the probable author of the code, but the original authors were much earlier in 3D computer graphics. Rys Sommefeldt concluded that the original algorithm was devised by Greg Walsh at Ardent Computer in consultation with Cleve Moler, the creator of MATLAB. Cleve Moler learned about this technique from code written by William Kahan and K.C. Ng at Berkeley around 1986." - https://en.wikipedia.org/wiki/Fast_inverse_square_root


I interned at a web agency a long time ago and none of their technical people were more than integrators for WordPress and Drupal but sometimes they had to dable in custom modules and stuff like that, which was clearly not their strong suit.

Anyway, one of the modules had a piece of code that was checking what was in a mailbox, and the name of that function was literally "butWhatIsItThatThereIsInThatMailbox()".

For another project I used Laravel to make a website, and they needed to add a feature after it went to production but I was not working there anymore. I later learned that they just did a "mysql_connect" and ran their query directly in the view's template because they didn't know how to use the framework to make a DB query and pass the data to the view.

And they had HUGE clients that everyone has heard of.

So yeah, mind blowing.


Although I have not seen the source code, I was mind blown when first heard of and saw the demo of the Sketchpad, a program created by Ivan Sutherland in 1963.

http://scihi.org/ivan-sutherland-computer-graphics/

Ph.D. thesis: - https://dspace.mit.edu/handle/1721.1/14979 (1963 original), - https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-574.{html,pd... (2003 electronic edition)


My first real software engineering job.

I was put in charge of the company's email program. This was in 1987-8.

It was a single file, with over 100 KLoC of 1970s-vintage FORTRAN IV code.

Not one single comment.

Variables written like INTEGER A1, for a buffer index.

No subroutines.

GOTOs galore.

That was some trauma that convinced me to write good, structured, well-documented code, from then on.


Almost forgot - Lisp In Life. Pretty sure I first saw it here at HN. IMO the most amazing part is the Life-based processor - once you have that, the rest is "merely" a matter of adding layers. But it's one thing to know someone could do it, quite another to see it in action.

https://github.com/woodrush/lisp-in-life

Run it in your browser here. Definitely worth zooming all the way in to get a sense of scale.

https://woodrush.github.io/lisp-in-life/


When I was a student, I went to a programming lab one very hangover morning, new teacher. He was a bit ambitious and he had us program a visitor pattern in smalltalk, it really openend my mind on using the vtable as a flow control system.


The Elixir |> (pipeline / nose) operator. Makes functional programming much cleaner without brackets or intermediate variables. I wish more programming language has this. JavaScript comes close with chained calls of map.


Ocaml also has `@@` which gives `'a -> ('a -> 'b) -> 'b = <fun>` and `|>` for `('a -> 'b) -> 'a -> 'b = <fun>` so you can also do:

```ocaml

  type point = { x: float; y: float};;

  let euclidean a a' =
    let xd = (a'.x -. a.x) in
    let yd = (a'.y -. a.y) in
    sqrt ((xd*.xd) +. (yd*.yd))
  ;;

  let is_in_unit_circle x =
    x <= 1.

  let quadsolve iterations hits =
    ((4. *. hits) /. Float.of_int iterations)

  let estimate iters =
    let origin    = { x = 0.; y = 0. } in
     Array.init iters (fun _ -> { x = (Random.float 1.); y = (Random.float 1.) })
     |> Array.map (euclidean origin)
     |> Array.map is_in_unit_circle
     |> Array.fold_left (fun x y ->  if y then x +. 1. else x ) 0.
     |> quadsolve iters
;;

```

  utop # estimate 1000000;;
  - : float = 3.141432

Edit: formatting


Markdown fence blocks don't work. For monospace, indent 2 or more spaces.


This is common in the functional paradigm, although syntax and semantics varies in sometimes important ways among languages.

There's a long-standing TC39 proposal to add it to JS. It's interesting to read it to see examples of the aforementioned differences.

https://github.com/tc39/proposal-pipeline-operator


The Kickstarter iOS Swift code also used this, from what I remember. It was my first time ever seeing something like it, so I was also mindblown.


F# has this too (maybe had it first? I don't know) and it's wonderful. So does Elm.


the rotating donut written in C shaped like a donut, also a lot of early demoscene written by hand in assembly


The Lambda Calculus. So simple. So powerful. Still studied today.


Indeed; see the above example of a prime number generates in just 167 bits of (binary) lambda calculus.


Learning that I could pass a -function- as a value blew my mind for sure.

I'd been programming for quite a while at that point, though mostly it was self-taught or procedural/OOP things I had learned in school.

That made a lot of impacts on how I thought about programming; basically until tha point I had never encountered functional programming, and boop it pushed me off a big ol' cliff.


Reverse Polish Lisp (on the HP 49G) for starters.

The basic Haskell function definition,

    factorial 0 = 1
    factorial n = n * factorial (n - 1)


A lot of VGA/SVGA paging blew my mind 20-25 years ago when I saw it.

Frankly, it still does. Low-mid level graphics code is incredible wizardry.


A lot of code from The Art of Prolog that used some form of accumulator data structure as a third argument to predicates.


Do you mean something like how you might define `append/3`

  append([], L, L).
  append([H|T], L2, [H|L3]) :- append(T, L2, L3).
Such that the computation can be run in any direction? Where it will produce the result of appending two lists, or the prefixes and suffixes of a list if the result (third parameter) is known, and so on?


Not code but I always marvel at the relational databases of creaky Enterprise behemoths - SharePoint, SAP, etc...


Clojure transducers (https://clojure.org/reference/transducers). It's clearly a functional construct, but somehow I haven't seen it being used in other functional languages.


https://github.com/bitemyapp/learnhaskell/blob/master/dialog...

> a transducer is recognizing that the signature of foldl splits

> type Transducer a b = forall r. (r -> b -> r) -> (r -> a -> r)

> they compose like lenses


It would have to be an HID interface for some controller I ran into. It was essentially a large switch that used bitwise operations against some magic byte as the cases to determine which button was pressed. Avoided using variables or an enum which I thought was interesting


I do not have access to a C compiler at the moment so cannot verify this with latest compiler but one thing I remember seeing long time ago was that you can interchange an array name and it's index, something like this: a[i] can also be written as i[a]


YOLO v3's paper is hilarious. The model was state of the art at the time, yet it's written in a tongue-in-cheek way:

https://arxiv.org/pdf/1804.02767.pdf


I think groupcache and singleflight are surprisingly simple for what they do: https://go.dev/talks/2013/oscon-dl.slide


notcurses: https://github.com/dankamongmen/notcurses

The guy is a literal genius, I hope to forget half the stuff he knows.

Also Fabrice Bellard (ffmpeg/qemu) and Inigo Quilez.


messagepack implementations.

made me realize how much knobs are there in writing a data to bytes encoding function.. you get to pick a trade off between readability, ease of implementation (in other language), encoding speed, encoding output size, and more.


I saw the title, came to post the exact code that OP posted. It's from Dybvig's Scheme programming language book. I remember going through that book and just being blown away by the elegance of Scheme.


Its been a while since I worked with this lib but I always thought Pygments was pretty.

https://github.com/pygments/pygments


A recursive version of depth first search that I coded in python while practicing for interviews with leetcode. Recusion and DFS is so elegant.


I feel the SCP foundation has something about a Cthulhu codebase that literally explodes the brains of all who read it.

And I feel I’ve worked on that codebase …


Reminds me of that one special SCP entry, where:

1. The entry does not contain any words

2. The entry contains knowledge that can be easily understood

3. The knowledge inside the entry cannot be transliterated into words (even here)

The entry: https://scp-wiki.wikidot.com/scp-2521


That is indeed special. Probably shouldn't have stayed up all night before checking it out. Getting some real Lovecraft vibes... Or Illuminatus! Trilogy. Hopefully it'll make more sense after some sleep.


CKEditor, formerly known as the much more accurate "FCKEditor" inspired me to combine its logo with that of one of the Call of Cthulhu board games. I definitely lost sanity points every time I went in there. And I definitely said, "FUCK" every single time.

Prior to the rewrite it had a bunch of global state and someone on the team I contracted on had decided to use it anyway but patch things up so it could work with multiple edit panes in the same UI.


it isn't rocket science, but https://github.com/fkling/astexplorer significantly raised my standard of what a readable, modifiable, nontrivial react/redux codebase could look like. bonus that astexplorer is basically used by everyone in the js tooling ecosystem


SQLite’s codebase - the test to code ratio is something like 1 to 20000. It’s the most tested codebase in existence.


Sadly I cannot share the code. But some COBOL code used for validation of financial transactions. It was crazy!


I once saw a 20k line Java source file, being used in production... no it wasn't generated. that blew my mind.


Now that's job security!


Maybe they really liked inlining?


And loop unrolling. Performance must have been mindblowing.


We have that in C#, it's call LINQ. In the wrong hands it kills kittens.


Most legacy ML models I’ve had to move to production aka not on a laptop. Oh you meant in a good way…


Hash tables are something I learned in college and was just mind-blown, so simple and so useful


There was a youtube video a few years back that showed a guy livecoding...I think an ECMA interpreter, or maybe a WASM interpreter from scratch using some very easy to follow code. I can't remember what it was or the video, but it was gobsmacking. So instead here's another story.

I once worked with an old Perl greybeard, he had written some kind of code that could parse an almost impossible large set of string formats that a large collection of automated systems produced except they all had some sort of non-standard/non-guaranteed formatting or semantics. Little bits and pieces (some semi-atomic bits) of them were semi-reliable, but the aggregate strings were just a huge mess, things came in all kinds of orders, had unreliable separators (if any) some things came in backwards, some forwards, and so on.

You might imagine them to be things like IP addresses that sometimes had system names attached, sometimes locations, sometimes, no address, sometimes the address was separated by dots or commas, sometimes, pieces of equipment were listed. All legacy of decades of people slowly accruing bits and pieces of the larger system and naming things for local convenience rather for global understanding. As the systems rolled up these strings would sometimes get concatenated in different ways and in different orders.

His code looked like total gibberish, a library of a bunch of code that sort of looked like Perl but also sort of looked like a custom rule language, it had something like 1200 rules in it based on the kinds of "atomics" he'd observed. The rules were sort of pieces of regexes, and then a string with embedded functions in it.

And then some bizarre code elsewhere I also didn't get at first.

It turned out the code was clever way of writing a meta program that would assemble new code on the fly that was capable of processing every possible string format -- potentially billions of them. At the core of the code was simple lexer that atomized the strings based on a few heuristics (nonstandard of course), and then would apply the rules to each lexical piece.

It turns out the rules were regexes that emitted code snippets on match. The snippets were often templated to take pieces of the atomic bits and the regex would parse the atom and put the pieces into the snippet.

There was some simple logic to assemble all of the emitted code into a "program" that when eval()'d would generate an output object that had all of the components of the original string, but also decoded, enriched, normalized, and ready for downstream work.

In the rare case an "atom" was encountered the system didn't have a "rule" for, it would throw an error and dump the string out to a file where he'd take a look at it, and in about 30 seconds slap in a new "rule" to handle the case forever more.

The entire codebase was maybe the rules and a couple hundred more lines of code -- maybe 1400-1500 lines. Written in a few weeks, processing tens of millions of strings per day, silent and forgotten in the bowels of some giant enterprise automation system.

It, running on a Pentium II or III of some kind, had replaced a rack of Sun Pizza boxes, and an old codebase setup by a consulting company that went down or garbled the processing of the strings at least once a week and required two full-time people to keep running. When the company had a downturn they laid off that team and in a fit of desperation the greybeard had come up with this little monster -- the beauty of which was under 5 minutes of maintenance per week to check logged errors and write a new rule if needed.

edit ahh here's the video https://youtu.be/r-A78RgMhZU


Iterating through the set bits in an integer in O(# of set bits) time.


This is bit dated but when in my university days I reas code of xterm


10 PRINT "HELLO HELLO" 20 GOTO 10


Is this code supposed to work?


Comonadic User Interface design.


Scalaz.


Discrete cosine transform.


profunctor optics.


In the Elixir kernel, defmacro is defined by calling the defmacro macro about itself

https://github.com/elixir-lang/elixir/blob/main/lib/elixir/l...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: