Skip to content

Instantly share code, notes, and snippets.

@billrobertson42
Created October 5, 2012 18:28
Show Gist options
  • Save billrobertson42/3841542 to your computer and use it in GitHub Desktop.
Save billrobertson42/3841542 to your computer and use it in GitHub Desktop.
Turn a StringBuilder into a Clojure Function Take 2
(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