Created
April 14, 2011 16:37
-
-
Save liquidz/919875 to your computer and use it in GitHub Desktop.
clojure reader macro to support "partial"
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
; cf. | |
; http://briancarper.net/blog/449/clojure-reader-macros | |
; http://d.hatena.ne.jp/nokturnalmortum/20100527/1274961805 | |
(ns partial-reader | |
(import [clojure.lang LispReader LispReader$ListReader Cons Symbol])) | |
(defn dispatch-reader-macro [ch fun] | |
(let [dm (.get (doto (.getDeclaredField LispReader "dispatchMacros") | |
(.setAccessible true)) | |
nil)] | |
(aset dm (int ch) fun))) | |
(defn cons-symbol-object [symbol-str reader letter] | |
(let [c (.read reader)] | |
(if (= c (int \()) | |
(Cons. (Symbol/create symbol-str) (.invoke (LispReader$ListReader.) reader c)) | |
(throw (Exception. (str "Reader barfed on " (char c))))))) | |
(def partial-reader (partial cons-symbol-object "partial")) | |
(dispatch-reader-macro \# partial-reader) | |
; example | |
(def plus10 ##(+ 10)) | |
(println (plus10 1)) | |
(println (map ##(* 2) '(1 2 3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment