randomcorp / thread-first-thread-last-backwards-question-mark-as-arrow-cond-arrow-bang

The exciting new ->->><?as->cond->! macro

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

->->><?as->cond->!

As seen in Every Clojure Talk Ever by Amperity

This is the bread and butter of randomcorp's Clojure code and we're excited to share it with the world. Have you been frustrated with always needing to compose the various types of threading macros?

(-> []
    (conj 1 2 3)
    (->> (mapv inc))
    (as-> v
      {:vec-of-numbers v})
    (cond->
      (some-pred) (assoc :other-key "other-val")))

It would be nice if there were one single macro to do all that for you. Well, now there is!

(->->><?as->cond->!
  []
  :-> (conj 1 2 3)
  :->> (mapv inc)
  :as-> v {:vec-of-numbers v}
  :cond-> (some-pred) (assoc :other-key "other-val"))

To short-circuit on nil values (a la some->), use :<?:

(->->><?as->cond->!
  []
  :-> not-empty
  :<?
  :-> (conj 1))

;; => nil

To make assertions about the values during the threading, use :!:

(->->><?as->cond->!
  2
  :-> inc
  :! even?)

;; => Error!

License

Free, as in beer.

About

The exciting new ->->><?as->cond->! macro


Languages

Language:Clojure 100.0%