~ / track D / fp foundations

Composition and point-free style

Intermediate

Composing functions is the basic move of functional programming: you build big behaviors by combining small ones. Clojure ships three core combinators — comp, partial, and juxt — which together let you assemble new functions without naming intermediate arguments. Code written this way is called point-free or tacit: there are no "points" (argument names) cluttering the picture.

Minimal example

comp glues functions right-to-left: (comp f g) is "first g, then f":

loading sci
press ⌘/Ctrl-↵ or click ▶ run to evaluate

partial pre-fills the first arguments of a function and returns a new function:

loading sci
press ⌘/Ctrl-↵ or click ▶ run to evaluate

juxt runs several functions on the same input and returns a vector of their results — a tiny "fork":

loading sci
press ⌘/Ctrl-↵ or click ▶ run to evaluate

Practical example

Combine all three to build a small pipeline without ever naming the intermediate values:

loading sci
press ⌘/Ctrl-↵ or click ▶ run to evaluate

Reading right-to-left at the inner comp: pull :score, multiply by 2. juxt then forks each person into [name, doubled-score].

Point-free, with restraint

Point-free style is powerful but can become hard to read. A good rule of thumb: prefer point-free for short compositions of well-known functions; switch to a named lambda or let when the chain starts to obscure intent.

loading sci
press ⌘/Ctrl-↵ or click ▶ run to evaluate

Check yourself

? quiz

`(comp f g h) x` is equivalent to which expression?

Exercise

Without using fn or #(), build a single function summary that maps a person {:name ... :score N} to [name (* 2 N)]. Use juxt, comp, and partial.

loading sci
press ⌘/Ctrl-↵ or click ▶ run to evaluate
 status: new