Last active
March 9, 2017 12:20
-
-
Save cemerick/f0febaf492270634516afcb5b31e54a5 to your computer and use it in GitHub Desktop.
slightly saner printing for Clojure fns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good entry point ;)