conjunctive / dust

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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)

License:MIT License


Languages

Language:Clojure 99.3%Language:Shell 0.7%