(define (odd? x)
(cond
[(zero? x) #f]
[else (even? (sub1 x))]))
(define (even? x) (cond
[(zero? x) #t]
[else (odd? (sub1 x))]))
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);
}
}
https://medium.com/hard-mode/the-legendary-fast-inverse-squa...
https://github.com/mame/quine-relay
(HN thread: https://news.ycombinator.com/item?id=6048761)
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.
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"
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.
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.
- 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!"
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.
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.
https://justine.lol/ape.html https://github.com/jart/cosmopolitan
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.
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!"
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.
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)
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.
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).
It's like trying to enter a house by demolishing it and rebuilding it around you instead of opening the door and walking in.
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).
Amazing that someone can write correct, fast code like that.
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.
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.
And in just a couple of lines, excuse my quickie untested code:
#include
#include
// 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;
}
x ^= y;
y ^= x;
x ^= y;
Check if the number is even: x & 1 == 0
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.
- 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...)
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.
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.
(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.
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.
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.
Winner of 2001 IOCCC:
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.
The regex itself: https://metacpan.org/dist/PPR/source/lib/PPR.pm#L65
Documentation: https://metacpan.org/pod/PPR
> 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.
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.
https://en.wikibooks.org/wiki/Haskell/Continuation_passing_s...
Here is SPJ giving a talk on them: https://www.youtube.com/watch?v=k-QwBL9Dia0
So, that.
awk '!seen[$0]++'
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
#include
#include
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;
}
On the less functional side, Linux linked lists are super smart.
Years later I could tell my colleagues how a device driver/plugin actually worked (before COM/etc) just with this.
Class.ancestors.include?(Object)
Object.ancestors.include?(Class)
Class.is_a?(Object)
Object.is_a?(Class)
It's beautiful.
primes = filterPrime [2..]
where filterPrime (p:xs) = p : filterPrime [x | x <- xs, x `mod` p /= 0]
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.
(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)))
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.
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...
function gcd(a,b){ return b ? gcd(b, a%b) : a; }
I'm a slow learner.
[1] https://www.cs.tufts.edu/~nr/cs257/archive/richard-bird/sudo...
template
class Foo {};
class Bar : public Foo { ... };
https://en.cppreference.com/w/cpp/language/crtp
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.
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.
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)
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.
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.
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.
The basic Haskell function definition,
factorial 0 = 1
factorial n = n * factorial (n - 1)
Frankly, it still does. Low-mid level graphics code is incredible wizardry.
The guy is a literal genius, I hope to forget half the stuff he knows.
Also Fabrice Bellard (ffmpeg/qemu) and Inigo Quilez.
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.
And I feel I’ve worked on that codebase …
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
https://github.com/elixir-lang/elixir/blob/main/lib/elixir/l...