Skip to content

Instantly share code, notes, and snippets.

@bhb
bhb / gist:66804e5fe2a3de728744a620cf0ccf27
Created June 25, 2018 14:39
Changes in printing alternatives
(require '[clojure.spec.alpha :as s])
(require '[expound.alpha :as expound])
(set! s/*explain-out* (expound/custom-printer {:print-specs? false :theme :figwheel-theme}))
;;;;; Old output (0.7.0) ;;;;;
-- Spec failed --------------------
(... (arg1 ...) ...)
^^^^
@bhb
bhb / gist:e8328ce1c15c3fc6f5f4ea71cabfa076
Created May 30, 2018 18:02
instrumentation with clojure.spec
➜ ~ clojure -Sdeps "{:deps {com.bhauman/rebel-readline {:mvn/version \"0.1.3\"}}}" -m rebel-readline.main
[Rebel readline] Type :repl/help for online help info
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (s/fdef foo :args (s/cat :x int? :y string?))
user/foo
user=> (defn foo [x y] (prn {:x x :y y}) {y x})
#'user/foo
user=> (require '[clojure.spec.test.alpha :as st])
nil
@bhb
bhb / expound_check.clj
Created April 27, 2018 12:57
Expound printing mimimized failing input
(require '[expound.alpha :as expound] '[clojure.spec.test.alpha :as st] '[clojure.spec.alpha :as s])
(s/fdef foo :args (s/cat :x pos-int?) :ret (s/int-in 0 5))
(defn foo [x] x)
(set! s/*explain-out* expound/printer)
(expound/explain-results (st/check `foo))
;;== Checked user/foo =========================
;;
;;-- Function spec failed -----------
;;
;; (user/foo 5)
@bhb
bhb / shell.txt
Created April 14, 2018 16:34
Using expound with lumo
> brew install lumo
...
> npm install @bbrinck/expound
...
> lumo
Lumo 1.8.0
ClojureScript 1.9.946
Node.js v9.2.0
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
@bhb
bhb / fn.clj
Created April 3, 2018 01:41
fdef example
;; This is a valid ordering:
;; first spec, then function defintion
(s/fdef my-add
:args (s/cat :x int? :y int?))
(defn my-add [x y]
(+ x y))
@bhb
bhb / ex.clj
Created March 23, 2018 19:41
Conform/unform to rewrite function bodies
(defn update-bodies
"Updates all function bodies in the destructured value.
args:
f - a function of two args. The first is the destructured value, the second is the body.
conformed-defn-args - the destructured value after conforming to :clojure.core.specs.alpha/defn-args
returns: a new destructured value where f has been applied to all function bodies in the
destructured value.
See http://blog.klipse.tech/clojure/2016/10/10/defn-args.html for more details on modifying
@bhb
bhb / examples.clj
Created March 20, 2018 00:06
Expound examples
;; There is still a ton of work to do wrt to formatting, testing, and improving examples,
;; but here's a proof-of-concept of Expound printing examples.
;; 'defn' examples are adapted from "Improving Clojure's Error Messages with Grammars"
;; https://youtu.be/kt4haSH2xcs?t=10m20s
;; Changing types
(defn "foo" [] 1)
;; -- Example ------------------------
;; (<f> foo [] 1)
@bhb
bhb / repl
Created March 2, 2018 17:28
`is` vs `explain` with expound
> brew install clojure
> clj -Sdeps '{:deps {friendly {:git/url "https://gist.github.com/bhb/2686b023d074ac052dbc21f12f324f18" :sha "cbc5b3c73d4788a25570f125e5f2de23a3d2bf5f"}}}' -m friendly
user=> (require '[expound.alpha :as expound])
user=> (require '[clojure.test :refer [is]])
nil
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (is (let [xs ["a" "b" :c "d"]]
#_=> (is (and (vector? xs)
#_=> (every? string? xs)))))
@bhb
bhb / gist:2f99d9dd04134cde50623e4f8764a00e
Created February 28, 2018 22:32
Example of rebel+expound REPL
> clj -Sdeps '{:deps {friendly {:git/url "https://gist.github.com/bhb/2686b023d074ac052dbc21f12f324f18" :sha "cbc5b3c73d4788a25570f125e5f2de23a3d2bf5f"}}}' -m friendly
[Rebel readline] Type :repl/help for online help info
user=> (let ["x" 1])
CompilerException clojure.lang.ExceptionInfo: Call to clojure.core/let did not conform to spec:
-- Spec failed --------------------
(["x" 1])
^^^
should satisfy
@bhb
bhb / README.md
Last active May 15, 2023 01:28
Clojure friendly mode, inspired by https://github.com/slipset/friendly