Conditional elements in -> / ->> pipelines

Viewed 3707

Given a ->> pipeline like so:

(defn my-fn []
  (->> (get-data)
       (do-foo)
       (do-bar)
       (do-baz)))

I wish to make the various stages conditional.

The first way of writing this that came to mind was as such:

(defn my-fn [{:keys [foo bar baz]}]
  (->> (get-data)
       (if foo (do-foo) identity)
       (if bar (do-bar) identity)
       (if baz (do-baz) identity))

However, as the ->> macro attempts to insert into the if form, this not only looks unfortunate in terms of performance (having the noop identity calls), but actually fails to compile.

What would an appropriate, reasonably DRY way of writing this be?

5 Answers
Related