Library for writing more flexible let forms, which may exit earlier with a custom value.
Add the following dependency into your deps.edn file:
io.github.filipencopav/short-circuit {:git/sha "11b4d95"
:git/tag "v1.1"}Require it:
(require '[filipencopav.short-circuit :as sc])sc/let: Use it to create a let binding that can short-circuit when a certain value is bound.sc/exit: Use it to mark a value as a short-circuiting one.sc/letwill return the value.
When all goes well, functions as normal let
(require 'filipencopav.short-circuit :as sc)
(def m {:key1 1})
(sc/let [a (:key1 m)
b (if a
(+ a 5)
(sc/exit {:error "provided map doesn't have :key1"}))]
(+ a b)) ;=> 7When receives a value wrapped in sc/exit (it’s a reduced value under the hood), short-circuits and returns the value early.
(def m2 {})
;; same form as above
(sc/let [a (:key1 m2)
b (if a
(+ a 5)
(sc/exit {:error "provided map doesn't have :key1"}))]
(+ a b)) ;=> {:error "provided map doesn't have :key1"}BSD 3 clause.