Skip to content

Instantly share code, notes, and snippets.

@cemerick
Last active March 9, 2017 12:20
Show Gist options
  • Save cemerick/f0febaf492270634516afcb5b31e54a5 to your computer and use it in GitHub Desktop.
Save cemerick/f0febaf492270634516afcb5b31e54a5 to your computer and use it in GitHub Desktop.
slightly saner printing for Clojure fns
(defn delegate-printing-for
"Registers the necessary methods to delegate printing of instances of [class] to [fn]."
[class fn]
#?@(:clj [(defmethod print-method class
[x writer]
(.write ^java.io.Writer writer (fn x)))
(defmethod print-dup class
[x writer]
(print-method x writer))
(#'clojure.pprint/use-method
clojure.pprint/simple-dispatch
class
#'clojure.pprint/pprint-simple-default)]))
#?(:clj (delegate-printing-for
clojure.lang.AFunction
#(str "#fn["
(-> % class .getName
; can't bring myself to impl more un-munging than this
(.replaceAll "\\$" "/")
(.replaceAll "_" "-"))
" "
(format "0x%x " (System/identityHashCode %)) "]")))
; #()
; => #object[user$eval27642$fn__27643 0x44356321 "user$eval27642$fn__27643@44356321"]
;
; vs.
;
; #()
; => #fn[user$eval27696$fn__27697 0x5e1aaa7c]
@ivanpierre
Copy link

Good entry point ;)

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