On Tue, Mar 24, 2009 at 11:05 PM, Korny Sietsma <ko...@sietsma.com> wrote:
> It'd be nice to have a macro that worked more like the first example -
> "spit" is great for one-liners, but the fact that it opens and closes the
> file each time you call it seems a bit painful for anything more complex.
> Something that ends up working like:
>
> (with-out-as "test.txt"
>    (println "hello")
>    (println "world"))

WARNING: spoilers ahead:

(use 'clojure.contrib.duck-streams)

(defmacro with-out-as
  [f & body]
  `(with-open [w# (writer ~f)]
     (binding [*out* w#]
       ~...@body)))

(with-out-as "/tmp/test.txt"
  (print "hola,")
  (println " crayola...."))


Cheers,

Victor Rodriguez.

> Hmm - I've never written a macro, maybe I should give this a try...
>
> - Korny
>
> On Wed, Mar 25, 2009 at 5:10 AM, Stuart Sierra <the.stuart.sie...@gmail.com>
> wrote:
>>
>> On Mar 24, 12:42 pm, Parth Malwankar <parth.malwan...@gmail.com>
>> wrote:
>> > user=> (with-open [f (writer (file "test.txt"))]
>> >          (binding [*out* f]
>> >            (println "hello world !!!")))
>>
>> Or even more simply:
>>
>> (use 'clojure.contrib.duck-streams)
>> (spit  "test.txt"  "Hello, world!\n")
>>
>> -Stuart Sierra
>>
>
>
>
> --
> Kornelis Sietsma  korny at my surname dot com
> "Every jumbled pile of person has a thinking part
> that wonders what the part that isn't thinking
> isn't thinking of"
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to