Yeah, I got the impression the OP was trying to create a new file with
double the contents of the old one - the (str file-path 2) piece - but yours
is certainly a slick way to double the original file!

On Fri, Aug 12, 2011 at 8:47 PM, Dave Ray <dave...@gmail.com> wrote:

> Even shorter:
>
> (defn duplicate-file-data [file-path] (spit file-path (slurp
> file-path) :append true))
>
> Dave
>
> On Fri, Aug 12, 2011 at 11:16 PM, Sean Corfield <seancorfi...@gmail.com>
> wrote:
> > I think you also want to reorganize the code so you get the line-seq and
> > then the line-count outside the for loop. And bear in mind that (inc
> > line-count) just returns line-count + 1 - it does not update line-count
> > which is what I'm guessing you're expecting?
> > Or you could just use slurp and spit:
> > (defn duplicate-file-data [file-path] (let [content (slurp file-path)]
> (spit
> > (str file-path 2) (str content content))))
> >
> > On Fri, Aug 12, 2011 at 8:05 PM, Sean Corfield <seancorfi...@gmail.com>
> > wrote:
> >>
> >> (for ...) generates a lazy sequence so it isn't realized until after the
> >> value is returned from the function. You need to wrap (for ...) with
> (doall
> >> ...) to realize the sequence inside (with-open ...)
> >>
> >> On Fri, Aug 12, 2011 at 4:47 PM, turcio <tur...@gmail.com> wrote:
> >>>
> >>> Hi,
> >>> I'm trying to write a function which creates file twice as big
> >>> compared to the original file by simply duplicating its content.
> >>>
> >>> It looks like in the for loop I can't even read the first line
> >>> although I'm using with-open. Can you tell me what am I doing wrong?
> >>>
> >>> (defn duplicate-file-data [file-path]
> >>>  (with-open [reader (clojure.java.io/reader file-path)
> >>>              writer (clojure.java.io/writer (str file-path 2) :append
> >>> true)]
> >>>                               (for [line (line-seq reader)
> >>>                                     :let [line-count (count(line-seq
> >>> reader))
> >>>                                           curr-line 0]
> >>>                                     :when (< curr-line line-count)]
> >>>                                 ((.write writer (str line))
> >>>                                          (.newLine writer)
> >>>                                          (inc curr-line))
> >>>                                 )))
> >>>
> >>>
> >>> --
> >>> Thanks
> >>> Daniel
> >>>
> >>> --
> >>> 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
> >>
> >>
> >> --
> >> Sean A Corfield -- (904) 302-SEAN
> >> An Architect's View -- http://corfield.org/
> >> World Singles, LLC. -- http://worldsingles.com/
> >> Railo Technologies, Inc. -- http://www.getrailo.com/
> >>
> >> "Perfection is the enemy of the good."
> >> -- Gustave Flaubert, French realist novelist (1821-1880)
> >
> >
> >
> > --
> > Sean A Corfield -- (904) 302-SEAN
> > An Architect's View -- http://corfield.org/
> > World Singles, LLC. -- http://worldsingles.com/
> > Railo Technologies, Inc. -- http://www.getrailo.com/
> >
> > "Perfection is the enemy of the good."
> > -- Gustave Flaubert, French realist novelist (1821-1880)
> >
> > --
> > 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
>
> --
> 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
>



-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

-- 
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