You'll want to take a look at the docs for c.c.string[1], so have that
open in another tab.  Anyway, let's assume you have the data in a file
mytext.txt

First, load the raw data with the slurp fn

user=>(def raw-string (slurp "mytext.txt"))

Next, you'll want to use the split-lines fn to create a sequence out
of the raw-string

user=>(def raw-lines (split-lines raw-string))

Now, let's consider an individual line.  We'll need to do 3 things
1. Split on whitespace - for this we'll call split.  Here's the anon.
fn:

(fn [line] (split #"\s+" line))

2. Parse each number - for this we need Java interop.

(fn [line] (map #(Integer/parseInt %) (split #"\s+" line)))

3. Wrap the result in a vector -simply call vec

This looks like the following:

(fn [line] (vec (map #(Integer/parseInt %) (split #"\s+" line))))

Now, we take this operation and map it over every line

(map (fn [line] (vec (map #(Integer/parseInt %) (split #"\s+" line))))
raw-lines)

Almost there.  We need to wrap the final call with a vec as well [3].

(def parsed-vec (vec (map (fn [line] (vec (map #(Integer/parseInt %)
(split #"\s+" line)))) raw-lines)))

That's how I would do this forward.  To do the reverse is much
simpler.  We'll need to use c.c.io [2] (formerly duck-streams).

First, we turn the vec back into a string.  This can be done easily
enough with c.c.string/join

(join "\n" (map #(join " " %) raw-vec))

And then we just spit this into a file:

(spit "output.txt" (join "\n" (map #(join " " %) raw-vec)))

And that should do it :)

HTH,
Sean

1. http://richhickey.github.com/clojure-contrib/string-api.html
2. http://richhickey.github.com/clojure-contrib/io-api.html
3. For those that have been here a while, my requisite point free
version...
(def parsed-vec (vec (map (& vec (p map #(Integer/parseInt %)) (p
split #"\s+")) raw-lines)))

On Apr 21, 2:40 pm, "I.K." <ignacykrasicki...@gmail.com> 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 clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group 
> athttp://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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
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