Skip to content

nijohando/deferable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deferable

Clojars Project CircleCI

Clojure(Script) deferred function library like golang's defer.

Rationale

Main motivations for writing this library are:

  • Providing deferred functions like golang's defer
  • Available on both Clojure and ClojureScript

Installation

Ligningen / Boot

[jp.nijohando/deferable "0.2.1"]

Clojure CLI / deps.edn

jp.nijohando/deferable {:mvn/version "0.2.1"}

Usage

Clojure

(require '[jp.nijohando.deferable :as d])

ClojureScript

(require '[jp.nijohando.deferable :as d :include-macros true])

Deferred functions

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"

Deferred functions in async

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"

License

© 2017-2018 nijohando

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.