Lazy-seq of a binary file

2011-12-11 Thread Simone Mosciatti
Hi Guys, I'm pretty new of clojure so sorry for the maybe stupid question... Anyway, i'm looking for read a file byte by byte, perfect would be get a lazy-seq of every byte in the file, it's looks, for me, very weird that there isn't a built-in or some easy way to do that, but I haven't find noth

Re: Lazy-seq of a binary file

2011-12-11 Thread Stephen Compall
On Sat, 2011-12-10 at 23:13 -0800, Simone Mosciatti wrote: > Anyway, i'm looking for read a file byte by byte, perfect would be get > a lazy-seq of every byte in the file, it's looks, for me, very weird > that there isn't a built-in or some easy way to do that The tradeoffs aren't universal enough

Re: Lazy-seq of a binary file

2011-12-12 Thread Stuart Sierra
It's hard to do this efficiently, unfortunately. See my notes & comments on http://dev.clojure.org/display/design/Byte+Sequences I did experiment with creating "chunked" binary sequences once, but never had time to finish it. -S -- You received this message because you are subscribed to the G

Re: Lazy-seq of a binary file

2011-12-12 Thread Simone Mosciatti
If I do just something like that: (def fl (clojure.java.io/reader "/path/to/file")) (defn lazy-reader [fl] (lazy-seq (cons (.read fl) (lazy-reader fl Can work ? (0.03696 ms for 500 char) Possible problem ? On Dec 11, 9:49 pm, Stephen Compall wrote: > On Sat, 2011-12-10 at 23:13

Re: Lazy-seq of a binary file

2011-12-12 Thread Stephen Compall
On Mon, 2011-12-12 at 10:21 -0800, Simone Mosciatti wrote: > (defn lazy-reader [fl] > (lazy-seq > (cons (.read fl) (lazy-reader fl > > Can work ? (0.03696 ms for 500 char) > Possible problem ? You need a termination case; your lazy-reader currently always yields an infinite sequen

Re: Lazy-seq of a binary file

2011-12-12 Thread Simone Mosciatti
I thought to just put it into a take... (take number-of-byte-necessary (lazy-reader (clojure.java.io/reader "path/to/file"))) On Dec 12, 12:34 pm, Stephen Compall wrote: > On Mon, 2011-12-12 at 10:21 -0800, Simone Mosciatti wrote: > > (defn lazy-reader [fl] > >     (lazy-seq > >         (cons (.

Re: Lazy-seq of a binary file

2011-12-12 Thread Simone Mosciatti
Ok, I found a possible problem, if i try to put all together, so write something like this: (defn lazy-reader [filename] (with-open [fl (clojure.java.io/reader filename)] (loop [bite (.read fl)] (lazy-seq (cons (bite) (recur (.read fl))) Obviously doesn't work... Any su

Re: Lazy-seq of a binary file

2011-12-12 Thread Stephen Compall
On Mon, 2011-12-12 at 20:03 -0800, Simone Mosciatti wrote: > Any suggest of how fix that ? In general, avoid loop. Specifically, try using letfn or (fn SOME-NAME-HERE [args...] ...) as your recursion target. -- Stephen Compall ^aCollection allSatisfy: [:each|aCondition]: less is better -- You

Re: Lazy-seq of a binary file

2011-12-12 Thread Cedric Greevey
You also probably want more efficiency. Try something closer to: (defn lazy-reader [filename] (let [rd (fn [rdr] (let [buf (char-array 4096) n (.read rdr buf 0 4096)] (condp == n -1 (.close rdr) 0 (recur rdr)

Re: Lazy-seq of a binary file

2011-12-13 Thread Stuart Sierra
Here's a version I hacked up a while ago: https://gist.github.com/1472163 -S -- 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 pat

Re: Lazy-seq of a binary file

2011-12-13 Thread Simone Mosciatti
No, I'm sure to not use all the sequence, so I will follow your second advice, but... Cause of my non-perfect english I've not really understand the last part. Who is the caller ? You suggest something like this: (let [fl (clojure.java.io/reader "path/filename") rd (lazy-reader fl)] (do-m

Re: Lazy-seq of a binary file

2011-12-13 Thread Cedric Greevey
On Tue, Dec 13, 2011 at 10:14 PM, Simone Mosciatti wrote: > No, I'm sure to not use all the sequence, so I will follow your second > advice, but... > > Cause of my non-perfect english I've not really understand the last > part. >  Who is the caller ? >  You suggest something like this: > > (let [f

Re: Lazy-seq of a binary file

2011-12-13 Thread Simone Mosciatti
Ok, now by now i think to have understand... To do right, I should build a macro similar to let where I pass the filename and after execute the body close the stream, right ? On Dec 13, 9:42 pm, Cedric Greevey wrote: > On Tue, Dec 13, 2011 at 10:14 PM, Simone Mosciatti wrote: > > No, I'm sure t

Re: Lazy-seq of a binary file

2011-12-13 Thread Cedric Greevey
On Tue, Dec 13, 2011 at 11:12 PM, Simone Mosciatti wrote: > Ok, now by now i think to have understand... > > To do right, I should build a macro similar to let where I pass the > filename and after execute the body close the stream, right ? Easier to just use the pre-existing one: with-open. Som

Re: Lazy-seq of a binary file

2011-12-13 Thread Simone Mosciatti
Where by now: (defn lazy-reader [fl] (assert ...) (lazy-seq (cons (.read fl) (lazy-reader fl The first one ? This means that I haven't understand nothing, right? (I'm so sorry for this stupid question... :embarassed: ) On Dec 13, 10:23 pm, Cedric Greevey wrote: > On Tue,

Re: Lazy-seq of a binary file

2011-12-13 Thread Cedric Greevey
On Tue, Dec 13, 2011 at 11:33 PM, Simone Mosciatti wrote: > Where by now: > (defn lazy-reader [fl] >     (assert ...) >     (lazy-seq >         (cons (.read fl) (lazy-reader fl > > The first one ? Er, buffering of the I/O is probably preferable, but that would probably work OK in many cases.

Re: Lazy-seq of a binary file

2011-12-13 Thread Simone Mosciatti
Thank you so much, just one last thing, why you use a char-array ? If I want use a byte-array, and no map all the whole sequence ? On Dec 13, 10:39 pm, Cedric Greevey wrote: > On Tue, Dec 13, 2011 at 11:33 PM, Simone Mosciatti wrote: > > Where by now: > > (defn lazy-reader [fl] > >     (assert .

Re: Lazy-seq of a binary file

2011-12-14 Thread Cedric Greevey
On Wed, Dec 14, 2011 at 12:04 AM, Simone Mosciatti wrote: > Thank you so much, just one last thing, why you use a char-array ? Reader returns chars. > If I want use a byte-array, and no map all the whole sequence ? Use an InputStream rather than a reader if you're reading binary files (or text

Re: Lazy-seq of a binary file

2011-12-14 Thread Simone Mosciatti
Ok thank you so much, i got it. Thanks again ;-) Simone On Dec 14, 3:22 am, Cedric Greevey wrote: > On Wed, Dec 14, 2011 at 12:04 AM, Simone Mosciatti wrote: > > Thank you so much, just one last thing, why you use a char-array ? > > Reader returns chars. > > > If I want use a byte-array, and n

Re: Lazy-seq of a binary file

2011-12-15 Thread Cedric Greevey
On Wed, Dec 14, 2011 at 11:47 PM, Simone Mosciatti wrote: > Ok thank you so much, i got it. > > Thanks again ;-) You're welcome. -- 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