Re: tomcat 6/7 stream closed error in ring json - works in lein ring server
(is this is the longest delay in replying ever? :)) You are right - I mistakenly thought that json-wrap-params only exposed the request params as oppose to exposing the JSONified body in the params; I thought json-wrap-body and json-wrap-params were complimentary. Now I have read the docs (https://github.com/ring-clojure/ring-json) it is clear that only one is required. Problem solved - chose one or the other. On Monday, 4 November 2013 02:46:55 UTC, James Reeves wrote: > > It would. I'm not sure why one fails and the other does not, unless the > request object passed through Tomcat is different somehow, or unless you > accidentally sent two different HTTP requests when testing. > > Regardless, the error message you've received is pretty clear - the JSON > stream is consumed by the first JSON middleware before the second can get > to up. > > - James > > > On 4 November 2013 01:40, Colin Yates > > wrote: > >> Hi James, >> >> Not sure why I did that double wrapping.. >> >> However, wouldn't that also fail in Jetty? >> >> >> On 4 November 2013 01:02, James Reeves > > wrote: >> >>> Hi Colin, >>> >>> One of the compromises Ring makes for efficiency is that the body of a >>> request is an InputStream, rather than a static string or byte array >>> pre-loaded into memory. Because it's a stream, it can potentially be >>> consumed by previous middleware. >>> >>> For some reason you have both wrap-json-body and wrap-json-params in >>> your stacktrace: >>> >>> ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) >>> ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) >>> >>> Both middleware read from the body InputStream, so one of them is going >>> to fail when you try and read the body twice. >>> >>> - James >>> >>> >>> On 4 November 2013 00:20, Colin Yates > >>> wrote: >>> >>>> Hi all, >>>> >>>> I have developed a ring/compojure app which receives and servers JSON. >>>> All is well in 'lein ring server' but as soon as I do 'lein ring uberwar' >>>> and deploy it to tomcat 6 or 7 it fails. To be explicit, the app deploys >>>> and I can view the static resources, but as soon as I issue a JSON POST I >>>> get the following error: >>>> >>>> [code] >>>> java.io.IOException: Stream closed >>>> org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:325) >>>> >>>> org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:193) >>>> sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) >>>> sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) >>>> sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) >>>> java.io.InputStreamReader.read(InputStreamReader.java:184) >>>> java.io.BufferedReader.fill(BufferedReader.java:154) >>>> java.io.BufferedReader.read(BufferedReader.java:175) >>>> clojure.core$slurp.doInvoke(core.clj:6279) >>>> clojure.lang.RestFn.invoke(RestFn.java:410) >>>> ring.middleware.json$read_json.doInvoke(json.clj:12) >>>> clojure.lang.RestFn.invoke(RestFn.java:423) >>>> ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) >>>> ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) >>>> compojure.core$routing$fn__362.invoke(core.clj:107) >>>> clojure.core$some.invoke(core.clj:2443) >>>> compojure.core$routing.doInvoke(core.clj:107) >>>> clojure.lang.RestFn.applyTo(RestFn.java:139) >>>> clojure.core$apply.invoke(core.clj:619) >>>> compojure.core$routes$fn__366.invoke(core.clj:112) >>>> health.servlet$_service$fn__1016.invoke(servlet.clj:1) >>>> ring.util.servlet$make_service_method$fn__50.invoke(servlet.clj:126) >>>> health.servlet$_service.invoke(servlet.clj:1) >>>> health.servlet.service(Unknown Source) >>>> [/code] >>>> >>>> Any help? >>>> >>>> -- >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To post to this group, send email to clo...@googlegroups.com >>>> >>>> Note that posts from new members are moderated - please be patient with >>>> your first post. >>>> To unsubscribe from this group, sen
Re: tomcat 6/7 stream closed error in ring json - works in lein ring server
It would. I'm not sure why one fails and the other does not, unless the request object passed through Tomcat is different somehow, or unless you accidentally sent two different HTTP requests when testing. Regardless, the error message you've received is pretty clear - the JSON stream is consumed by the first JSON middleware before the second can get to up. - James On 4 November 2013 01:40, Colin Yates wrote: > Hi James, > > Not sure why I did that double wrapping.. > > However, wouldn't that also fail in Jetty? > > > On 4 November 2013 01:02, James Reeves wrote: > >> Hi Colin, >> >> One of the compromises Ring makes for efficiency is that the body of a >> request is an InputStream, rather than a static string or byte array >> pre-loaded into memory. Because it's a stream, it can potentially be >> consumed by previous middleware. >> >> For some reason you have both wrap-json-body and wrap-json-params in your >> stacktrace: >> >> ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) >> ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) >> >> Both middleware read from the body InputStream, so one of them is going >> to fail when you try and read the body twice. >> >> - James >> >> >> On 4 November 2013 00:20, Colin Yates wrote: >> >>> Hi all, >>> >>> I have developed a ring/compojure app which receives and servers JSON. >>> All is well in 'lein ring server' but as soon as I do 'lein ring uberwar' >>> and deploy it to tomcat 6 or 7 it fails. To be explicit, the app deploys >>> and I can view the static resources, but as soon as I issue a JSON POST I >>> get the following error: >>> >>> [code] >>> java.io.IOException: Stream closed >>> org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:325) >>> >>> org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:193) >>> sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) >>> sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) >>> sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) >>> java.io.InputStreamReader.read(InputStreamReader.java:184) >>> java.io.BufferedReader.fill(BufferedReader.java:154) >>> java.io.BufferedReader.read(BufferedReader.java:175) >>> clojure.core$slurp.doInvoke(core.clj:6279) >>> clojure.lang.RestFn.invoke(RestFn.java:410) >>> ring.middleware.json$read_json.doInvoke(json.clj:12) >>> clojure.lang.RestFn.invoke(RestFn.java:423) >>> ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) >>> ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) >>> compojure.core$routing$fn__362.invoke(core.clj:107) >>> clojure.core$some.invoke(core.clj:2443) >>> compojure.core$routing.doInvoke(core.clj:107) >>> clojure.lang.RestFn.applyTo(RestFn.java:139) >>> clojure.core$apply.invoke(core.clj:619) >>> compojure.core$routes$fn__366.invoke(core.clj:112) >>> health.servlet$_service$fn__1016.invoke(servlet.clj:1) >>> ring.util.servlet$make_service_method$fn__50.invoke(servlet.clj:126) >>> health.servlet$_service.invoke(servlet.clj:1) >>> health.servlet.service(Unknown Source) >>> [/code] >>> >>> Any help? >>> >>> -- >>> -- >>> 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 unsubscribe from this group and stop receiving emails from it, send >>> an email to clojure+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- >> -- >> 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
Re: tomcat 6/7 stream closed error in ring json - works in lein ring server
Hi James, Not sure why I did that double wrapping.. However, wouldn't that also fail in Jetty? On 4 November 2013 01:02, James Reeves wrote: > Hi Colin, > > One of the compromises Ring makes for efficiency is that the body of a > request is an InputStream, rather than a static string or byte array > pre-loaded into memory. Because it's a stream, it can potentially be > consumed by previous middleware. > > For some reason you have both wrap-json-body and wrap-json-params in your > stacktrace: > > ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) > ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) > > Both middleware read from the body InputStream, so one of them is going to > fail when you try and read the body twice. > > - James > > > On 4 November 2013 00:20, Colin Yates wrote: > >> Hi all, >> >> I have developed a ring/compojure app which receives and servers JSON. >> All is well in 'lein ring server' but as soon as I do 'lein ring uberwar' >> and deploy it to tomcat 6 or 7 it fails. To be explicit, the app deploys >> and I can view the static resources, but as soon as I issue a JSON POST I >> get the following error: >> >> [code] >> java.io.IOException: Stream closed >> org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:325) >> >> org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:193) >> sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) >> sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) >> sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) >> java.io.InputStreamReader.read(InputStreamReader.java:184) >> java.io.BufferedReader.fill(BufferedReader.java:154) >> java.io.BufferedReader.read(BufferedReader.java:175) >> clojure.core$slurp.doInvoke(core.clj:6279) >> clojure.lang.RestFn.invoke(RestFn.java:410) >> ring.middleware.json$read_json.doInvoke(json.clj:12) >> clojure.lang.RestFn.invoke(RestFn.java:423) >> ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) >> ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) >> compojure.core$routing$fn__362.invoke(core.clj:107) >> clojure.core$some.invoke(core.clj:2443) >> compojure.core$routing.doInvoke(core.clj:107) >> clojure.lang.RestFn.applyTo(RestFn.java:139) >> clojure.core$apply.invoke(core.clj:619) >> compojure.core$routes$fn__366.invoke(core.clj:112) >> health.servlet$_service$fn__1016.invoke(servlet.clj:1) >> ring.util.servlet$make_service_method$fn__50.invoke(servlet.clj:126) >> health.servlet$_service.invoke(servlet.clj:1) >> health.servlet.service(Unknown Source) >> [/code] >> >> Any help? >> >> -- >> -- >> 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 unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- > -- > 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 a topic in the > Google Groups "Clojure" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/clojure/RxctbwMoOPU/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: tomcat 6/7 stream closed error in ring json - works in lein ring server
Hi Colin, One of the compromises Ring makes for efficiency is that the body of a request is an InputStream, rather than a static string or byte array pre-loaded into memory. Because it's a stream, it can potentially be consumed by previous middleware. For some reason you have both wrap-json-body and wrap-json-params in your stacktrace: ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) Both middleware read from the body InputStream, so one of them is going to fail when you try and read the body twice. - James On 4 November 2013 00:20, Colin Yates wrote: > Hi all, > > I have developed a ring/compojure app which receives and servers JSON. > All is well in 'lein ring server' but as soon as I do 'lein ring uberwar' > and deploy it to tomcat 6 or 7 it fails. To be explicit, the app deploys > and I can view the static resources, but as soon as I issue a JSON POST I > get the following error: > > [code] > java.io.IOException: Stream closed > org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:325) > > org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:193) > sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) > sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) > sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) > java.io.InputStreamReader.read(InputStreamReader.java:184) > java.io.BufferedReader.fill(BufferedReader.java:154) > java.io.BufferedReader.read(BufferedReader.java:175) > clojure.core$slurp.doInvoke(core.clj:6279) > clojure.lang.RestFn.invoke(RestFn.java:410) > ring.middleware.json$read_json.doInvoke(json.clj:12) > clojure.lang.RestFn.invoke(RestFn.java:423) > ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) > ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) > compojure.core$routing$fn__362.invoke(core.clj:107) > clojure.core$some.invoke(core.clj:2443) > compojure.core$routing.doInvoke(core.clj:107) > clojure.lang.RestFn.applyTo(RestFn.java:139) > clojure.core$apply.invoke(core.clj:619) > compojure.core$routes$fn__366.invoke(core.clj:112) > health.servlet$_service$fn__1016.invoke(servlet.clj:1) > ring.util.servlet$make_service_method$fn__50.invoke(servlet.clj:126) > health.servlet$_service.invoke(servlet.clj:1) > health.servlet.service(Unknown Source) > [/code] > > Any help? > > -- > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: tomcat 6/7 stream closed error in ring json - works in lein ring server
Apologies - I just realised a Compojure group existed. I have posted in there (https://groups.google.com/forum/#!topic/compojure/_jut36dXmCM) as well. On Monday, 4 November 2013 00:20:29 UTC, Colin Yates wrote: > > Hi all, > > I have developed a ring/compojure app which receives and servers JSON. > All is well in 'lein ring server' but as soon as I do 'lein ring uberwar' > and deploy it to tomcat 6 or 7 it fails. To be explicit, the app deploys > and I can view the static resources, but as soon as I issue a JSON POST I > get the following error: > > [code] > java.io.IOException: Stream closed > org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:325) > > org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:193) > sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) > sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) > sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) > java.io.InputStreamReader.read(InputStreamReader.java:184) > java.io.BufferedReader.fill(BufferedReader.java:154) > java.io.BufferedReader.read(BufferedReader.java:175) > clojure.core$slurp.doInvoke(core.clj:6279) > clojure.lang.RestFn.invoke(RestFn.java:410) > ring.middleware.json$read_json.doInvoke(json.clj:12) > clojure.lang.RestFn.invoke(RestFn.java:423) > ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) > ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) > compojure.core$routing$fn__362.invoke(core.clj:107) > clojure.core$some.invoke(core.clj:2443) > compojure.core$routing.doInvoke(core.clj:107) > clojure.lang.RestFn.applyTo(RestFn.java:139) > clojure.core$apply.invoke(core.clj:619) > compojure.core$routes$fn__366.invoke(core.clj:112) > health.servlet$_service$fn__1016.invoke(servlet.clj:1) > ring.util.servlet$make_service_method$fn__50.invoke(servlet.clj:126) > health.servlet$_service.invoke(servlet.clj:1) > health.servlet.service(Unknown Source) > [/code] > > Any help? > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
tomcat 6/7 stream closed error in ring json - works in lein ring server
Hi all, I have developed a ring/compojure app which receives and servers JSON. All is well in 'lein ring server' but as soon as I do 'lein ring uberwar' and deploy it to tomcat 6 or 7 it fails. To be explicit, the app deploys and I can view the static resources, but as soon as I issue a JSON POST I get the following error: [code] java.io.IOException: Stream closed org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:325) org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:193) sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) java.io.InputStreamReader.read(InputStreamReader.java:184) java.io.BufferedReader.fill(BufferedReader.java:154) java.io.BufferedReader.read(BufferedReader.java:175) clojure.core$slurp.doInvoke(core.clj:6279) clojure.lang.RestFn.invoke(RestFn.java:410) ring.middleware.json$read_json.doInvoke(json.clj:12) clojure.lang.RestFn.invoke(RestFn.java:423) ring.middleware.json$wrap_json_body$fn__1031.invoke(json.clj:19) ring.middleware.json$wrap_json_params$fn__1035.invoke(json.clj:31) compojure.core$routing$fn__362.invoke(core.clj:107) clojure.core$some.invoke(core.clj:2443) compojure.core$routing.doInvoke(core.clj:107) clojure.lang.RestFn.applyTo(RestFn.java:139) clojure.core$apply.invoke(core.clj:619) compojure.core$routes$fn__366.invoke(core.clj:112) health.servlet$_service$fn__1016.invoke(servlet.clj:1) ring.util.servlet$make_service_method$fn__50.invoke(servlet.clj:126) health.servlet$_service.invoke(servlet.clj:1) health.servlet.service(Unknown Source) [/code] Any help? -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Stream closed...
I'm really fresh to clojure way of doing things and your answers will definitely help. Thank you! Cheers, Daniel On 13 Sie, 06:46, Sean Corfield wrote: > 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 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 > > 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 > > > 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 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)) > > >>> ))) > -- 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
Re: Stream closed...
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 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 > 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 > > 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 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.googl
Re: Stream closed...
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 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 > 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 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
Re: Stream closed...
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 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 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
Re: Stream closed...
(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 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) -- 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
Stream closed...
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