Skip to content

Instantly share code, notes, and snippets.

Stuart Halloway stuarthalloway

Block or report user

Report or block stuarthalloway

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
View renames_spec.clj
;; see also https://twitter.com/thing_umbrella/status/1111427898487898113
(require '[clojure.spec.alpha :as s])
;; spec that ensures the keys in renames match the keys in map
(s/def ::rename-keys-args
(s/and (s/cat :map map? :renames map?)
(fn [{:keys [map renames]}]
(every? map (keys renames)))))
;; ok
@stuarthalloway
stuarthalloway / example.clj
Created Feb 8, 2019
What Clojure errors look like
View example.clj
(let [a 1 b])
Syntax error macroexpanding clojure.core/let at (REPL:5:1).
[a 1 b] - failed: even-number-of-forms? at: [:bindings] spec: :clojure.core.specs.alpha/bindings
@stuarthalloway
stuarthalloway / specs_over_tests.clj
Created Oct 13, 2018
Capture domain knowledge once in specs, instead of burying it in tests
View specs_over_tests.clj
;; compare to https://gist.github.com/lensgolda/13e549476fb65d3b8c2d71ca59b15937#file-test-clj
(require '[clojure.spec.alpha :as s])
;; general domain knowledge, not buried in a test
(s/def ::info (s/keys :req-un [::name]))
(s/def ::items (s/coll-of pos-int?))
(s/def ::active boolean?)
;; knowledge specific to this test data
View expression_at_at_time.clj
(ns foo)
(defn hello [x] (str "Hi " x))
(ns bar)
(println (foo/hello "there"))
(remove-ns 'foo)
(println (resolve 'foo/hello))
@stuarthalloway
stuarthalloway / clj_2373_examples.clj
Last active Aug 22, 2018
Intended for form-at-a-time at the REPL
View clj_2373_examples.clj
(require
'[clojure.main :as main]
'[clojure.pprint :as pp])
(def strings
"Some strings that should fail somewhere in read/compile"
[":::5"
"(let [x])"
"(cond 1)"
"defmulti 5 class)"
View fizzbuzz.clj
(->> [(cycle [:fizz :_ :_])
(cycle [:buzz :_ :_ :_ :_])]
(apply map vector)
(take 25))
View clj_2373_take_1.clj
(comment "interop runtime exception repl-caught")
(/ 1 0)
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:163)
(comment "interop runtime exception pst")
(pst)
ArithmeticException Divide by zero
clojure.lang.Numbers.divide (Numbers.java:163)
clojure.lang.Numbers.divide (Numbers.java:3833)
;; elided
View inline_testing.clj
(defn round5
"Round to the closest positive multiple of 5.
Negative numbers round to 0, which is not
considered a multiple of 5."
{:test (fn [] (let [roundsto (fn [e] #(= e (round5 %)))]
(testing "Negative numbers"
(is (every? (roundsto 0) (range -20 0))))
(testing "Positive numbers rounding to 5"
(is (every? (roundsto 5) (range 0 8))))
(testing "Positive numbers rounding not to 5"
@stuarthalloway
stuarthalloway / atomic_state_update.clj
Last active Nov 25, 2017
Update state with a pure function
View atomic_state_update.clj
;; fixed version of 'state' at http://mishadoff.com/blog/clojure-design-patterns/#episode-3-state
;; takeaway: if you call 'swap!' twice on the same atom, you are probably making a mistake
(def user {:name "Jackie Brown"
:balance 0
:subscription? false})
(def ^:const SUBSCRIPTION_COST 30)
(defn pay
You can’t perform that action at this time.