read


read
or learn more

Code Riffs

Jan 6, 2023

Once upon a time I was deep into the MD/DC/VA area punk scene, and believe it or not I played in my share of bands and participated in my share of punk shows — both in the crowd and sometimes even on stage. I look back on this time fondly, but to be honest I can’t say that I ever contributed any song to the universe of music that was worth listening to. That said, while I can’t claim to have been a talented song-writer, like many aspiring musicians I did discover my fair share of riffs along the way.

A riff is an interesting little musical phrase that one often comes across whilst playing around with a guitar in a casual fashion. Very often riffs form the seeds of what becomes fully-realized songs. Sometimes these songs are good and sometimes they’re not, but in most cases they are grown like crystals from the original musical fragment found on the fret-board. For musicians, both practicing and aspiring, the riff represents a universe of potential out of which any number of potential works of musical art may spawn.

A few years ago I devised a phrase that I called Code Painting describing source code that: told a story, was usually not generally useful, was beautifully abstract, and created in the spirit of exploration. Unlike a code painting, a code riff is more atomic and often addresses a singular notion. If I were to characterize the attributes of a code riff then perhaps the following will suffice:

  • A code riff exists independent to a project narrative
  • A code riff need not be useful
  • A code riff is often “found” during the act of playful programming or sprung forth from one’s mind
  • A code riff should be beautiful, abstract, and as amusing if possible
  • A code riff should invoke Huh? A ha! Ha ha! 1

In my time I’ve created my share of code riffs; some that inspired something more and some still ripe with potential. If you’re interested in some code riffs then a few are available on Github as Tori Lisp, Evalive, and Unfix and additionally the break macro in chapter 17 of The Joy of Clojure.

Here’s a Clojure code riff from a presentation that I gave many years ago called “The Magnificent Seven”:

(def NIL ((fn [x y] (if (= x y) x)) = (= = =)))

(def CAR (fn [[h & _]] h))
(def CDR  (fn [[_ & t]] t))

(def CONS
  (fn [h t]
    (fn ([] h)
        ([_] t))))

(def FIRST (fn [s] (s)))
(def REST (fn [s] (s NIL)))

(FIRST (CONS 1 (CONS 2 NIL)))
;;=> 1

(FIRST (REST (CONS 1 (CONS 2 NIL)))
;;=> 2

Do you have any code riffs to share?

:F


  1. Rich Hickey, the creator of Clojure, is a master of code riffs. 

5 Comments, Comment or Ping

  1. Nice post. I like the idea using the word “riff” here, it fits nicely into my brain. I, err, riffed on some similar “pattern” ideas in this presentation https://docs.google.com/presentation/d/1xgOHBFNOjg6dFz1qHkSpB9VVjKxgjkugoxo13wAZYU0/edit#slide=id.p and in this post https://qmacro.org/blog/posts/2017/02/19/the-beauty-of-recursion-and-list-machinery/

  2. So, last year I tried to illustrate Clojure and its standard library, using FizzBuzz as a prompt (the childrens’ counting game framing, which is more general than the programmer interview framing).

    The creative constraint was/is this: any FizzBuzz, however terrible or hilarious, must also be useful. It should have reason to exist and should reveal some real-world Clojure thinking.

    Much teasing apart of the FizzBuzz problem space ensued, along many axes (destructive vs/ nondestructive, concrete v/s abstract representations, calculation v/s effects v/s sequence generation v/s specification).

    And among other versions, these fun ones fell out of the riffing:

    ;; defined by construction, under modulo arithmetic

    (def mod-cycle-buzz (let [n identity f (constantly “Fizz”) b (constantly “Buzz”) fb (constantly “FizzBuzz”)] (cycle [n n f n b f n n f b n f n n fb])))

    ;; defined as abstract representation, under peano encoding

    (def S “The PeanoBuzz number system starting at [0 0] is closed under this definition of Successor.” (comp (juxt identity get-rem15-buzz) inc first))

    (def all-peano-buzzes (iterate S [0 0]))

    https://www.evalapply.org/posts/n-ways-to-fizzbuzz-in-clojure/

    B-)

  3. @Aditya

    A worthy riff indeed!

  4. During Advent of Code, I found a fun way to find less-than-N unique items in a sliding window of size N, using bitmasks and xor:

    https://www.mattkeeter.com/blog/2022-12-10-xor/

    No one quite believes it works at first, including me.

  5. John

    Dude, we probably crossed paths at a fugazi show :)

    I feel like most of my libs are code riffs. I liken it to finding a rare flower or mushroom in the woods. It’s just aesthetically pleasing. It might not be edible – it might even be poisonous. But look at how pretty it is! lol

    This one is delightfully useless: https://github.com/johnmn3/mq/blob/master/src/mq/core.cljc

Reply to “Code Riffs”