Composition with pipe and flow
Every webhook Relay receives is cleaned before anything reads it: trim the source, lowercase it, drop hop-by-hop headers, cap the body. Written as one function, that is a block of statements where the rules are tangled together. Written as four small functions, each rule has a name, a test, and one job.
A typed pipe and flow showed how to type pipe and flow. This lesson is about using
them as a style. The typing is not repeated here: pipe(value, f, g) runs g(f(value)), and
flow(f, g) returns that chain as a function.
Data-last functions
Section titled “Data-last functions”A step in a pipeline takes one argument, the value flowing through. A step that needs settings gets them first and returns the step. The data comes last:
interface RawWebhook { readonly source: string readonly headers: Readonly<Record<string, string>> readonly body: string}
const trimSource = (w: RawWebhook): RawWebhook => ({ ...w, source: w.source.trim() })
const capBody = (max: number) => (w: RawWebhook): RawWebhook => w.body.length <= max ? w : { ...w, body: w.body.slice(0, max) }
declare function pipe<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): Cdeclare const incoming: RawWebhook
const clean = pipe(incoming, trimSource, capBody(1024))capBody(1024) is a configured step. capBody(1024, w) would not fit in pipe, because pipe
passes exactly one argument. This is the curried shape from higher-order functions,
and it is the reason library functions put the data last.
Each step returns a new object and leaves its input alone (lesson 2). When the
body is already short, capBody returns the same object, not a copy, so callers can tell nothing
changed with ===.
flow names a pipeline
Section titled “flow names a pipeline”pipe needs the data in hand. When the pipeline is something Relay reuses, name it with flow:
interface RawWebhook { readonly source: string readonly headers: Readonly<Record<string, string>> readonly body: string}declare const trimSource: (w: RawWebhook) => RawWebhookdeclare const lowercaseSource: (w: RawWebhook) => RawWebhookdeclare const dropHopByHop: (w: RawWebhook) => RawWebhookdeclare const capBody: (max: number) => (w: RawWebhook) => RawWebhookdeclare function flow<A extends unknown[], B, C, D, E>( ab: (...a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E,): (...a: A) => E
const normalize = (maxBody: number) => flow(trimSource, lowercaseSource, dropHopByHop, capBody(maxBody))// (maxBody: number) => (w: RawWebhook) => RawWebhookRead the definition as a list of rules. Adding a rule is one more line, and removing one cannot
break the others. The pipeline is also a plain function value, so you can pass it to map or hand
it to the part of Relay that does I/O.
Point-free style and where it hurts
Section titled “Point-free style and where it hurts”Point-free means writing a pipeline without naming the data. flow(trimSource, lowercaseSource) is
point-free. It reads well when every step is already a named function with an obvious meaning.
It goes wrong when you pass functions that take more than they look like they take:
const retryCounts = ["10", "10", "10"].map(parseInt)// [10, NaN, 2]: map passes (value, index, array), so parseInt gets the index as its radixmap calls its callback with three arguments, and parseInt reads the second as a radix. Writing
(s) => parseInt(s, 10) costs a few characters and removes the bug. Point-free is safe only when
you know the exact arity of both sides.
Three more costs:
- Anonymous chains.
flow(a, b, c, d, e, f)with inline arrows tells you what happens but not why. A step with a name explains itself. - Debugging. There is no variable to inspect between steps. Insert a step that logs and returns
its input (
const tap = <A>(f: (a: A) => void) => (a: A) => { f(a); return a }) while you debug, then remove it. - Error messages. When types stop matching in the middle of a chain, the error points at the
step, but the message talks about the generic parameters
A,B,Cofpipe. Split the chain and annotate the middle value to see what each side expects.
Name the intermediate steps
Section titled “Name the intermediate steps”The fix for most of these is to give a long chain a few named pieces:
interface RawWebhook { readonly source: string readonly headers: Readonly<Record<string, string>> readonly body: string}declare const trimSource: (w: RawWebhook) => RawWebhookdeclare const lowercaseSource: (w: RawWebhook) => RawWebhookdeclare const dropHopByHop: (w: RawWebhook) => RawWebhookdeclare const capBody: (max: number) => (w: RawWebhook) => RawWebhookdeclare function flow<A extends unknown[], B, C>(ab: (...a: A) => B, bc: (b: B) => C): (...a: A) => Cdeclare function pipe<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): Cdeclare const incoming: RawWebhook
const normalizeSource = flow(trimSource, lowercaseSource)const sanitize = (max: number) => flow(dropHopByHop, capBody(max))
const clean = pipe(incoming, normalizeSource, sanitize(1024))Two levels of names beat one flat list of six. Stop at the point where a reader could describe each name in a sentence.
Exercise
Section titled “Exercise”Write the four rules and normalize. The rules are small, but each must be data-last, must not
change its input, and capBody must hand back the same object when nothing was cut.
- A pipeline step takes one argument. Settings go first and the data goes last, so
capBody(1024)is a step. piperuns a chain on a value now.flownames a chain you reuse.- Point-free is safe only when you know both sides’ arity:
["10"].map(parseInt)is a bug. - Name a few middle pieces instead of one long anonymous chain.
- Each step returns a new value and does not mutate, so steps can be reordered and tested alone.
Further reading
Section titled “Further reading”- parseInt(): the radix argument.