How about this?

(use 'clojure.contrib.str-utils 'clojure.contrib.duck-streams)

(defn parse [file]
  (let [r (reader file)]
    (map (fn [line] (map #(Integer/parseInt %) (.split line " ")))
            (take (Integer/parseInt (.readLine r)) (repeatedly
#(.readLine r))))))

(defn unparse [xss file]
  (let [w (writer file)]
    (.writeLine w (str (count xss))
    (doseq [xs xss]
      (.writeLine w (str-join " " (map str xs)))))))

-Per

On Thu, Apr 22, 2010 at 1:40 AM, I.K. <[email protected]> wrote:
> Hi!
> I'm learning Clojure and trying some Google Code Jam exercises.
> I am more or less satisfied with the style of algorithms I write, but
> I would like to know how to do input/output. I want it to be Clojure
> style (terse/functional/efficient) not just rewriting the usual
> loops...
> Take a look at simplest example from io standpoint:
> http://code.google.com/codejam/contest/dashboard?c=188266#.
> I want the file:
>
> 3
> 2 3
> 2 3 7
> 9 10
>
> turn into vector: [ [2 3] [2 3 7] [9 10] ],
>
> and in reverse direction (produce file given above from this vector).
>
> Show your style.
>
> Thanks,
> I.K.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to