Hi,

On Aug 21, 10:11 am, DemAS <andrey.demi...@gmail.com> wrote:

>    I have created the function that read a txt file.  I have a very
> little experience in clojure and I will be thankfull for any
> correction and ideas.

For the record: see (doc line-seq)

But here some suggestions, since learning clojure is your main
objective, reading a file is only the playground...

 * `def` is only used at the toplevel, to declare locals use let: (let
[f-reader (..) b-reader (...) ...] ...)
 * The `def` (and hence the `let`) in the create-* functions can be
left out: (defn create-freader [fname] (java.io.FileInputStream.
fname))
 * Note: the trailing dot above is shorthand notation for `new`:
(Foo.) <=> (new Foo)
 * in `eof`:  since `line` is either a String or nil you can test it
directly: (if line false true), this makes `eof` completely
unnecessary, just use (if next-line (recur ...) result) in `read-
lines`
 * you can use `with-open` to automatically close the file when you
are done: (with-open [f-reader (create-freader ..) b-reader (create-
breader f-reader] ...), then `close-freader` is not necessary, `with-
open` will also take care in case of an exception

More advanced stuff:

Currently you read the whole file into memory. You may want to turn
that into a lazy sequence, which just reads lines as necessary. So you
can also handle files which do not fit into memory.

        (defn read-lines
          [reader]
          (lazy-seq
            (when-let [line (.readLine reader)]
              (cons line (read-lines reader)))))

Note the `when-let`: when .readLine returns nil, the `when` returns
also nil. If .readLine returns a String, it will be available to the
code in `when-let` block as `line`. However you have to take care not
to let the sequence slip out your `with-open`. Because reading lines
happens defered, the file will already be closed outside the `with-
open` block. To prevent this wrap the call to `read-lines` into a
`doall`, this realises the seq, but then the whole file is in memory
again.

But this is really only an appetizer. Check out some tutorials from
the web before wrapping your head around lazy-seq. :)

Hope this helps.

Sincerely
Meikel

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