Skip to content

Instantly share code, notes, and snippets.

Ben Brinckerhoff bhb

Block or report user

Report or block bhb

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bhb
bhb / tempids.clj
Created May 13, 2019
Dependent effects with tempids
View tempids.clj
[{:op :create-company
:name "Foobar Company"
:id "tempid/company"}
{:op :create-employee
:name "Alice"
:company_id "tempid/company"
:id "tempid/user1"}
{:op :create-employee
:name "Bob"
:company_id "tempid/company"
View gist:d2fd7413a5d68f404b871526c58b4bc3
Run this to launch REPL
rlwrap clj -Srepro -Sdeps '{:deps {speculative {:mvn/version "0.0.3"} expound {:mvn/version "0.7.2"} org.clojure/test.check {:mvn/version "0.9.0"} org.clojure/clojurescript {:mvn/version "1.10.520"}}}' -e "(require '[expound.alpha :as expound] '[cljs.core.specs.alpha]) (set! clojure.spec.alpha/*explain-out* (expound/custom-printer {:print-specs? false :show-valid-values? true :theme :figwheel-theme}))" -m cljs.main -re node
Then run this in REPL
(require '[expound.alpha :as expound] '[cljs.core.specs.alpha] '[speculative.instrument :as i]) (i/instrument) (set! cljs.spec.alpha/*explain-out* (expound/custom-printer {:print-specs? false :show-valid-values? true :theme :figwheel-theme}))
@bhb
bhb / cljs_1_10_339.cljs
Created Feb 9, 2019
Custom explain-out with different versions of CLJS
View cljs_1_10_339.cljs
;; Note this uses clojure 1.9, not 1.10 like other examples
;; clj -Srepro -Sdeps '{:deps {org.clojure/test.check {:mvn/version "0.9.0"} org.clojure/clojure {:mvn/version "1.9.0"} org.clojure/clojurescript {:mvn/version "1.10.339"}}}' --main cljs.main --repl
in cljs.main --repl
(require '[cljs.spec.alpha :as s])
(require '[cljs.spec.test.alpha :as st])
(require '[clojure.core.specs.alpha])
;; instrumenting a function
(s/fdef foobar :args (s/cat :x int?))
@bhb
bhb / ex.clj
Last active Nov 25, 2018
Replacing default spec printer for experimentation
View ex.clj
;; launch with
;; clj -Srepro -Sdeps '{:deps {org.clojure/test.check {:mvn/version "0.9.0"} org.clojure/clojure {:mvn/version "1.10.0-beta8"}}}'
(require '[clojure.spec.alpha :as s])
(require '[clojure.spec.test.alpha :as st])
;; instrumenting a function
(s/fdef foobar :args (s/cat :x int?))
(defn foobar [x] x)
@bhb
bhb / init.cljs
Last active Nov 7, 2018 — forked from borkdude/init.cljs
Speculative + Expound in Klipse
View init.cljs
(require '[cljs.spec.alpha :as s])
(require '[cljs.spec.test.alpha :as stest])
(require '[expound.alpha :as expound])
(require '[speculative.core])
(set! s/*explain-out* (expound/custom-printer {:show-valid-values? true}))
(stest/instrument `map)
(try
(map 'lol 'lol)
(catch :default e
(println (.-message e))))
@bhb
bhb / ex.txt
Created Sep 29, 2018
Interpreting `:in` paths in `clojure.spec`
View ex.txt
> clj -Sdeps '{:deps {org.clojure/clojure {:mvn/version "1.10.0-alpha8"}}}'
Clojure 1.10.0-alpha8
user=> (require '[clojure.spec.alpha :as s])
nil
user=> ;; `explain-data` returns a description of the problem, including the `:in` path
user=> ;; which is the location of the invalid data within the context of the larger
user=> ;; data structure
user=> (-> (s/explain-data (s/coll-of int?) [1 "2"]) ::s/problems first)
{:path [], :pred int?, :val "2", :via [], :in [1]}
user=> (-> (s/explain-data (s/coll-of int?) [1 "2"]) ::s/problems first :in)
@bhb
bhb / repl.txt
Created Sep 25, 2018
Confusing behavior from `keys*`
View repl.txt
➜ ~ rlwrap clj -Srepro -Sdeps '{:deps {org.clojure/spec.alpha {:mvn/version "0.2.176"}}}'
Clojure 1.9.0
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (s/def ::point1 (s/cat :x int? :y int?))
user/point1
user=> (s/def ::x int?)
:user/x
user=> (s/def ::y int?)
:user/y
@bhb
bhb / simple_coerce.clj
Last active Sep 21, 2018
A naive implementation of opt-in coercion.
View simple_coerce.clj
;; Code is largely copied from
;; https://github.com/wilkerlucio/spec-coerce/blob/master/src/spec_coerce/core.cljc
;; with minor modifications!
(ns example.coercion
(:require [clojure.walk :as walk]
[clojure.spec.alpha :as s]))
(defonce ^:private registry-ref (atom {}))
;; WARNING - only works if all unqualified kw are unique!!!!
@bhb
bhb / repl.txt
Created Sep 21, 2018
Records vs spec for domain modelling - start with `clj -Srepro -Sdeps '{:deps {expound {:mvn/version "0.7.1"}}}'`
View repl.txt
Clojure 1.9.0
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (require '[expound.alpha :as expound])
nil
user=> (set! s/*explain-out* (expound/custom-printer {:theme :figwheel-theme :print-specs? false}))
#object[expound.alpha$custom_printer$fn__897 0x534243e4 "expound.alpha$custom_printer$fn__897@534243e4"]
user=> (s/check-asserts true)
true
user=> (defrecord Person [first-name last-name])
@bhb
bhb / repro.clj
Created Sep 7, 2018
Changing s/*explain-out* to control spec errors during macro-expansion
View repro.clj
(require '[clojure.spec.alpha :as s])
(set! s/*explain-out* (fn [ed] (println "failed")))
(let [x] 1)
;; with org.clojure/spec.alpha {:mvn/version "0.2.168"} :
;; CompilerException clojure.lang.ExceptionInfo: Call to clojure.core/let did not conform to spec:
;; failed
;; #:clojure.spec.alpha{:problems [{:path [:args :bindings :init-expr], :reason "Insufficient input", :pred clojure.core/any?, :val (), :via [:clojure.core.specs.alpha/bindings :clojure.core.specs.alpha/bindings], :in [0]}], :spec #object[clojure.spec.alpha$regex_spec_impl$reify__2499 0x749c877b "clojure.spec.alpha$regex_spec_impl$reify__2499@749c877b"], :value ([x] 1), :args ([x] 1)}, compiling:(NO_SOURCE_PATH:3:1)
You can’t perform that action at this time.