
\documentclass[12pt]{article}
\usepackage[T2A,OT1]{fontenc}
\usepackage[default]{cantarell}
\usepackage[a4paper, top=20mm, bottom=20mm, left=20mm, right=20mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[russian, english]{babel}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage[normalem]{ulem}
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}
\usepackage{setspace}
\onehalfspacing
\author{Artyom Bologov \href{mailto:threading@aartaka.me}{(email)}}
\date{\today}
\title{Functional Threading “Macros”}
\makeatletter
\def\endenv{\expandafter\end\expandafter{\@currenvir}}
\makeatother
\begin{document}
\maketitle

\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{./assets/threading.png}

I love Common Lisp.
But my dayjob is in Clojure (and TypeScript, ugh.)
I can’t help but notice the convenience of threading macros.

\section*{Threading Readability} \label{readability}

Compare these two pieces of code:

\begin{figure}[h!]\begin{verbatim}
;; No threading
(* (1+ (* significand (flt (/ (expt 2 significand)))))
   (expt 2 (- exponent (flt bias)))
   sign)
;; Threading
(->> significand
     (expt 2)
     /
     flt
     (* significand)
     1+
     (* sign (expt 2 (- exponent (flt bias)))))
\end{verbatim}\caption{Threading vs. non-threading Lisp code}\end{figure}

Threading code is much more readable.
It shows the sequence of actions in the order they happen in.
It omits the obvious parentheses.
It highlights the patterns in function applications.

One problem with threading macros though: they are macros.
Lisps are good at sophisticated syntax transformations.
Other languages—not so much.
So we need other ways to thread functions together.
Like... combinators in pure Lambda Calculus?

\section*{Threading Combinators} \label{combinators}

The idea is simple: we need several functions that’d pass closures around.
Bubbling outward and storing the inner functions for until the outer ones run.
(Reversing the applicative inner->outer evaluation order, thus the need for extra closures.)
Must look something like:

\begin{figure}[h!]\begin{verbatim}
piping 3 : pipe (* 2) : pipe 1+ piped .
;; or, abbreviated
--> 3 : -> (* 2) : -> 1+ >-- .
;; or, with colons expanded to parens
--> 3 (-> (* 2) (-> 1+ >--)) .
\end{verbatim}\caption{Hypothetical threading syntax}\end{figure}

\paragraph{Note on Lamber syntax} \begin{quote}
The language I’m using in this post is
\href{https://github.com/aartaka/lamber}{Lamber}, my Lambda Calculus compiling language.
It features a minimalist syntax with only functions, values, \verb|if|-s, and operators like
\href{run:wisp}{Wisp’s colon nesting operator (section inline-colon)}
and terminating period (similar to Lua’s \verb|end|.)
\end{quote}

First, let’s add a function that’ll initiate the piping.
Nothing fancy, just take the initial value and a curried function.
And then apply the function to the value.

\begin{figure}[h!]\begin{verbatim}
def piping fn (x f)
  f x .
;; also known as T combinator
alias piping T .
\end{verbatim}\caption{Simple piping wrapper}\end{figure}

Now to the workhorse \verb|pipe| function:

\begin{figure}[h!]\begin{verbatim}
def pipe fn (f g x)
  g : f x .
;; Also known as Queer Bird Combinator
alias pipe Q .
\end{verbatim}\caption{pipe combinator}\end{figure}

The way this magic works is:

\begin{itemize}\item We take a function to pipe and close over it
\item Then we take a “continuation” to apply to this function
\item And then we do the closed-over action on the value and “continue” the computation
\end{itemize}

So \verb|pipe (* 2) : pipe 1+ piped| means

\begin{itemize}\item closing over \verb|(* 2)| and
\item taking a function closed over \verb|1+|
\item and then applying this \verb|1+| function to result of \verb|(* 2)| applied to the data.
\end{itemize}

Nice reversal, huh?
But what does this \verb|piped| thing does?
It acts as a piping terminator, essentially returning what’s passed to it:

\begin{figure}[h!]\begin{verbatim}
def piped fn (x) x .
;; or
alias piped identity .
\end{verbatim}\caption{Simple piped definition/alias}\end{figure}

We accept second function into \verb|pipe|.
And then apply it to the result of the first one.
And the best way to stop the computation is to simply return the data passed into this second function.
Thus \verb|identity|.

Not sure if I explain it well enough.
So here’s an expansion process:

\begin{figure}[h!]\begin{verbatim}
piping 3 : pipe (* 2) : pipe 1+ piped .
piping 3 : pipe (* 2) : pipe 1+ identity .
piping 3 : pipe (* 2) : (fn (f g x) g : f x) 1+ identity .
piping 3 : pipe (* 2) : (fn (g x) g : 1+ x)  identity .
piping 3 : pipe (* 2) : (fn (x) identity : 1+ x) .
piping 3 : pipe (* 2) (fn (x) identity : 1+ x) .
piping 3 : (fn (f g x) g : f x) (* 2) (fn (x) identity : 1+ x) .
piping 3 : (fn (g x) g : (* 2) x) (fn (x) identity : 1+ x) .
piping 3 : (fn (x) (fn (x) identity : 1+ x) : (* 2) x) .
(fn (x f) f x) 3 (fn (x) (fn (x) identity : 1+ x) : (* 2) x) .
(fn (f) f 3) (fn (x) (fn (x) identity : 1+ x) : (* 2) x) .
(fn (x) (fn (x) identity : 1+ x) : (* 2) x) 3.
(fn (x) identity : 1+ x) : (* 2) 3 .
(fn (x) identity : 1+ x) : (* 2 3) .
(fn (x) identity : 1+ x) : 6 .
identity : 1+ 6 .
identity 7 .
7 .
\end{verbatim}\caption{Meticulous expansion of piping ensemble}\end{figure}

This threading is still relatively wordy and noisy, even when using \verb|->| aliases.
But that’s mostly due to Lamber’s minimalism and colon reliance.
Other languages might even introduce special operators behaving this way.
And it’ll work just fine without colons and nesting!

The implementation in this post is thread-last, which rhymes well with Lamber’s philosophy:
functions should be tail-heavy, putting the data to act on as last argument.
So I only need thread-last.
I leave thread-first combinator (and multi-arg ones) as an exercise to the reader.

\par\noindent\rule{\textwidth}{0.4pt}

It is a nice coincidence that Marvin Borner came to the same
\href{https://bruijn.marvinborner.de/samples/aoc_2022_01_solve.bruijn}{shape for his “→” infix operator},
and it seems it’s called Q/queer combinator!


\par\noindent\rule{\textwidth}{0.4pt}
\href{https://creativecommons.org/licenses/by/4.0}{CC-BY 4.0} 2022-2026 by Artyom Bologov (aartaka,)
\href{https://codeberg.org/aartaka/pages/commit/a91befa}{with one commit remixing Claude-generated code}.
Any and all opinions listed here are my own and not representative of my employers; future, past and present.
\end{document}
