Shell ain’t a bad place to FP: part 2/N: Functions as Unix Tools

Or, the one in which we hand-craft nano Unix tools using Bash functions.

As we saw in the previous post, functions obey stdio and we can mix and match them with built-ins (grep, sed, cat etc.) and other installed tools (like jq, pandoc, babashka etc.). We used functions to name parts of Douglas McIlroy’s pipeline and mess around a bit.

I tend to make libraries of pure functions that I can source in shell sessions and use just like any other shell tool, complete with tab-completion. e.g. bash-toolkit and shite.

Now we step back and try to build good intuitions about

  • what functions are
  • how to design good functions
  • how to design programs with functions
  • how to name them :)
Read more →

n ways to FizzBuzz in Clojure

FizzBuzz is everywhere. Every programmer passes through its rite of passage, or at least bears witness to another. Over the years, many gentlenerds have taken it upon themselves to discover ever new ways to incant those hoary symbols.

I hereby enjoin these few drops of Clojure to that roiling ocean of FizzBuzzery.

The brainwave here is to (mis)use the feature set of Clojure and its standard library to cook up as many ways to encode FizzBuzz as one can muster (or steal). If all goes well, this post will receive many updates. If it goes really well, all sorts of bad ideas and clever foot-guns will be discovered and used.

The creative constraint 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.

That means no FizzBuzzEnterpriseEdition but also no Dylan Beattie’s brilliant FizzBuzz in RockStar. So sorry!

Read more →

Shell ain’t a bad place to FP: part 1/N: McIlroy’s Pipeline

Or, the one in which we “take apart” Douglas McIlroy’s pipeline from 1986.

Doing so teaches an object lesson about the essence of modular, composable, functional architecture. And things start to really heat up when it dawns on us, how a good set of standard parts can be used to express totally different ideas just by composing them in different ways.

(Edit 2022-03-10: speaking of lessons, how about the one in the appendix?!)

“Designing is fundamentally about taking things apart. It’s about taking things apart in such a way that they can be put back together. i.e. Separating into things that can be composed.”

tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed 10q

Previously: Shell ain’t a bad place to FP: part 0/N

Read more →

Dismal Arithmetic in Dyalog APL and Clojure

Throwback: Jan 2018.

I learned of Dismal Arithmetic from @rdivyanshu who posed it as a programming problem in a Slackroom of local gentlenerds.

As the linked paper describes it:

Dismal arithmetic is just like the arithmetic you learned in school, only simpler: there are no carries, when you add digits you just take the largest, and when you multiply digits you take the smallest. This paper studies basic number theory in this world, including analogues of the primes, number of divisors, sum of divisors, and the partition function.

I thought it might be fun to implement it in APL for kicks, but I wrote it in Clojure first, because I wasn’t sure of my APL-fu. And I’m glad I wrote the Dyalog APL version because I learned something about trains, and also because I stumbled on the idea of “inverse of a function” which melted my mind a bit.

Read more →

Shell ain’t a bad place to FP: part 0/N: Introduction

Or, Supremely Functional Bash Programming, an exploration in N parts…

Once upon a time, while fiddling with a log processing shell pipeline, it hit me that the UNIX Way is a Surprisingly Functional Way, and so Functional Programming (FP) and Bash must be a natural fit. They fit. The world was never the same again.

Now I believe it so much, that I will go on a limb and assert that it is highly inappropriate to write imperative-style Bash when we can just as easily write supremely functional Bash. Why? Because it makes for supremely better Bash (more reusable, more composable, more scalable, more enjoyable).

Yes, I truly believe.

Yes, I’m sane.

No? Well OK, humour me…

Read more →

What makes Functional Programs and Systems “Functional”?

In which we ponder the Functional Nature of Life, The Universe, and Everything. Please feel free to follow through the weeds, or jump straight to the bottom for my 2 nano BTC on the matter. (Or my current state of mind, at any rate.)

Disclaimer: I live in the Land of Lisp, meditate in the Church of Alonzo, and am ever-wary of The State. Only converts might find some entertainment value.

“For the love of State is the root of all evil: which while some coveted after, they have erred from Lambda the Ultimate, and pierced themselves through with many sorrows."

Read more →

Poor man’s Reader App with Pandoc and Bash

Throwback, June 27, 2020.

Every so often, I want to avoid opening a website in a browser, for … reasons.

Curl alone presents too much html. I want to try and read stuff.

Today, I was playing with Igor Chubin’s awesome terminal services (, etc.), and it hit me:

“WAIT, there’s pandoc, what if I just … "

Read more →