💾 Archived View for idiomdrottning.org › conditional-transformation captured on 2024-05-12 at 15:49:11. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-07-16)

-=-=-=-=-=-=-

A conditionally transforming combinator

?-> is a combinator that takes a predicate test and a transformer, and returns a unary procedure that transforms its argument if the predicate applies.

That’s a mouthful. Let’s break it down with some examples:

You can make a car that only cars if the list is not null:

(map (?-> (o not null?) car) '((a b c) () (1 2 3) () ()))
⇒ (a () 1 () ())

Or a reverse that only reverses if it receives a list:

(map (?-> list? reverse) '(a (l u f r e d n o w) "summer's" (y a d)))
⇒ (a (w o n d e r f u l) "summer's" (d a y))

The default when the predicate doesn’t apply is to just pass through the argument, but you can set a different default with a keyword argument:

(map (?-> (o not null?) car default: #f) '((a b c) () (1 2 3) () ()))
⇒ (a #f 1 #f #f)

As for the name, I know I said no new ASCII art names, but ? for predicates and -> for transformation are both grandfathered in.

?-> is available in brev-separate from version 1.52.

Naming philosophy (for Lisp stuff)