Skip to content

Instantly share code, notes, and snippets.

@ryfow
Last active June 12, 2016 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryfow/69a64e966d48258dfa9dcb5aa74005eb to your computer and use it in GitHub Desktop.
Save ryfow/69a64e966d48258dfa9dcb5aa74005eb to your computer and use it in GitHub Desktop.
(require '[clojure.spec :as s])
(defmacro tpe [ & forms ]
`(try
~@forms
(catch Exception e#
(println "Form: " (pr-str '~@forms))
(println "Exception Class: " (.getClass e#))
(println "Message: " (.getMessage e#))
(println))))
(defn header [s]
(println "=========================================================")
(println s)
(println "========================================================="))
(s/fdef clojure.core/map
:args (s/cat :fn ifn?
:seq (s/* sequential?))
:return sequential?)
(header "Before instrumenting clojure.core/map")
(tpe (into [] (map nil [1 2 3] [1 2 3])))
(tpe (into [] (map 1 [1 2 3] [1 2 3])))
(tpe (into [] (map identity 4)))
(header "After instrumenting clojure.core/map")
(s/instrument #'clojure.core/map)
(tpe (into [] (map nil [1 2 3] [1 2 3])))
(tpe (into [] (map 1 [1 2 3] [1 2 3])))
(tpe (into [] (map identity 4)))
(header "Before instrumenting clojure.core/identity")
(tpe (into [] (map identity [1 2 3] [1 2 3])))
(s/fdef clojure.core/identity
:args (s/cat :one-argument (constantly true)))
(s/instrument #'clojure.core/identity)
(header "After instrumenting clojure.core/identity")
(tpe (into [] (map identity [1 2 3] [4 5 6])))
@ryfow
Copy link
Author

ryfow commented Jun 12, 2016

% java -jar ~/.m2/repository/org/clojure/clojure/1.9.0-alpha5/clojure-1.9.0-alpha5.jar core.clj
=========================================================
Before instrumenting clojure.core/map
=========================================================
Form:             (into [] (map nil [1 2 3] [1 2 3]))
Exception Class:  java.lang.NullPointerException
Message:          nil

Form:             (into [] (map 1 [1 2 3] [1 2 3]))
Exception Class:  java.lang.ClassCastException
Message:          java.lang.Long cannot be cast to clojure.lang.IFn

Form:             (into [] (map identity 4))
Exception Class:  java.lang.IllegalArgumentException
Message:          Don't know how to create ISeq from: java.lang.Long

=========================================================
After instrumenting clojure.core/map
=========================================================
Form:             (into [] (map nil [1 2 3] [1 2 3]))
Exception Class:  clojure.lang.ExceptionInfo
Message:          Call to #'clojure.core/map did not conform to spec:
In: [0] val: nil fails at: [:args :fn] predicate: ifn?
:clojure.spec/args  (nil [1 2 3] [1 2 3])


Form:             (into [] (map 1 [1 2 3] [1 2 3]))
Exception Class:  clojure.lang.ExceptionInfo
Message:          Call to #'clojure.core/map did not conform to spec:
In: [0] val: 1 fails at: [:args :fn] predicate: ifn?
:clojure.spec/args  (1 [1 2 3] [1 2 3])


Form:             (into [] (map identity 4))
Exception Class:  clojure.lang.ExceptionInfo
Message:          Call to #'clojure.core/map did not conform to spec:
In: [1] val: 4 fails at: [:args :seq] predicate: sequential?
:clojure.spec/args  (#object[clojure.core$identity 0x15a04efb "clojure.core$identity@15a04efb"] 4)


=========================================================
Before instrumenting clojure.core/identity
=========================================================
Form:             (into [] (map identity [1 2 3] [1 2 3]))
Exception Class:  clojure.lang.ArityException
Message:          Wrong number of args (2) passed to: core/identity

=========================================================
After instrumenting clojure.core/identity
=========================================================
Form:             (into [] (map identity [1 2 3] [4 5 6]))
Exception Class:  clojure.lang.ExceptionInfo
Message:          Call to #'clojure.core/identity did not conform to spec:
In: [1] val: (4) fails at: [:args] predicate: (cat :one-argument identity),  Extra input
:clojure.spec/args  (1 4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment