Created
May 7, 2010 04:11
-
-
Save technomancy/393045 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (ns string-fn.core | |
| "Serializable functions! Check it out." | |
| (:refer-clojure :exclude [fn])) | |
| (defmacro ^{:doc (str (:doc (meta #'clojure.core/fn)) | |
| "\n\n Oh, but it also allows serialization!!!111eleven")} | |
| fn [& sigs] | |
| `(with-meta (clojure.core/fn ~@sigs) | |
| {:type ::serializable-fn | |
| ::source (quote ~&form)})) | |
| (defmethod print-method ::serializable-fn [o ^Writer w] | |
| (print-method (::source (meta o)) w)) | |
| ^L | |
| (use 'clojure.test) | |
| (def dinc-list '(fn [x] (inc (inc x)))) | |
| (def dinc (eval dinc-list)) | |
| (deftest fns-created-with-metadata | |
| (is (ifn? dinc)) | |
| (is (map? (meta dinc)))) | |
| (deftest metadata-fns-work-as-original | |
| (is (= 2 (dinc 0)))) | |
| (deftest metadata-fns-return-source | |
| (is (= dinc-list (:string-fn.core/source (meta dinc))))) | |
| (deftest printing-fns-show-source | |
| (is (= (pr-str dinc-list) | |
| (pr-str dinc)))) | |
| (deftest preserve-reader-metadata | |
| (is (number? (:line (meta (:string-fn.core/source | |
| (meta dinc))))))) | |
| (deftest serializable-fn-roundtrip!!!111eleven | |
| (is (= 2 ((eval (read-string (pr-str dinc))) 0)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment