Skip to content

Pattern matching for destructured values (core.match & spec)

License

Notifications You must be signed in to change notification settings

conjunctive/dust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dust

Pattern matching for destructured values. Powered by spec and core.match.

Getting Started

Declare your dependencies in a deps.edn file.

{:deps {conjunctive/dust {:git/url "https://github.com/conjunctive/dust"
                          :sha "d9eb81beac4c5daea81cb4865522374586310d3d"}}}

Start a Clojure REPL.

$ clj

Require spec.

(require '[clojure.spec.alpha :as s])

Require the pattern matching macros.

(require '[dust.core :as dust])

Define a spec of a sum type.

(s/def ::sum (s/or :k keyword? :s string? :n number?))

Match against the conformed data.

(dust/spec "data"
  ::sum
  [:k key] (symbol key)
  [:s str] (seq str)
  [:n num] (inc num))
;; => (\d \a \t \a)

Throw an exception when a case is missing at compile time.

(dust/sum "Uh-oh"
  ::sum
  [:k key] (symbol key)
  [:n num] (inc num))

Throw an exception when a case is missing at runtime.

(try (dust/rune "Uh-oh"
       ::sum
       [:k key] (symbol key)
       [:n num] (inc num))
     (catch Exception e
       (dust.spec/explain (ex-data e))))

Short-circuit on invalid data.

(dust/sum {:x :y}
  ::sum
  [:k key] (symbol key)
  [:s str] (seq str)
  [:n num] (inc num))
;; => :clojure.spec.alpha/invalid

License

This project is licensed under the MIT License.

About

Pattern matching for destructured values (core.match & spec)

Topics

Resources

License

Stars

Watchers

Forks