Created
October 5, 2012 18:28
-
-
Save billrobertson42/3841542 to your computer and use it in GitHub Desktop.
Turn a StringBuilder into a Clojure Function Take 2
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
(defn builder | |
([^StringBuilder sb] | |
(fn | |
([] (.toString sb)) | |
([& args] | |
(doseq [arg args] | |
(.append sb arg))))) | |
([] (builder (StringBuilder.)))) | |
(comment | |
This version allows you to invoke the returned function with no args to call | |
toString on the StringBuilder. It also provides a no args call to builder that | |
creates a new StringBuilder for you. | |
Example... | |
user=> (def foo (builder)) | |
#'user/foo | |
user=> (apply foo (interpose " " '(hello world))) | |
nil | |
user=> (foo) | |
"hello world" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment