Clojure(Script) deferred function library like golang's defer
.
Main motivations for writing this library are:
- Providing deferred functions like golang's
defer
- Available on both Clojure and ClojureScript
[jp.nijohando/deferable "0.2.1"]
jp.nijohando/deferable {:mvn/version "0.2.1"}
Clojure
(require '[jp.nijohando.deferable :as d])
ClojureScript
(require '[jp.nijohando.deferable :as d :include-macros true])
do*
block works similarly to the clojure do
and defer
can be used inside the block.
defer
function is scheduled to be evaluated before the block returns value.
(d/do*
(d/defer (println "2"))
(println "1"))
;;=> "1"
;;=> "2"
;;=> nil
Multiple defer
forms are evaluated in LIFO order.
(d/do*
(d/defer (println "3"))
(d/defer (println "2"))
(println "1"))
;;=> "1"
;;=> "2"
;;=> "3"
do**
block is similar to do*
but designed for async programming like core.async.
Difference withdo*
is that Deferred functions are evaluated when calling done
function passed to the first argument of the do**
block.
(require '[clojure.core.async :refer [go]])
(d/do** done
(d/defer (println "3"))
(go
(println "2")
(done))
(println "1"))
;;=> "1"
;;=> "2"
;;=> "3"
© 2017-2018 nijohando
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.