Skip to content

Library for writing more flexible `let` forms, which may exit earlier with a custom value.

Notifications You must be signed in to change notification settings

filipencopav/short-circuit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

short-circuit let

Library for writing more flexible let forms, which may exit earlier with a custom value.

Install

Add the following dependency into your deps.edn file:

io.github.filipencopav/short-circuit {:git/sha "11b4d95"
                                      :git/tag "v1.1"}

Usage

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/let will return the value.

Examples

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)) ;=> 7

When 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"}

License

BSD 3 clause.

About

Library for writing more flexible `let` forms, which may exit earlier with a custom value.

Resources

Stars

Watchers

Forks

Packages

No packages published