Hi Folks
I have a program that parses a string into rows and fields by repeatedly
applying a sequence of functions repeatedly until the end of the string is
reached. Each function (or chunker, as I have called them) knows how to
find the next field in the stream and returns the field and the remainder
of the input text.
I've come up with a recursive implementation as shown below but I am
wondering if this is idomatic or if there is a better way using while, for
or something like that?
(defn read-row [chunkers text]
"Applies a list of functions to a string. Returns
a vector of fields found and any remaining text.
(read-row comma-chunkers \"foo,bar,bop,baz,\") => [[\"foo\" \"bar\"]
\"bop,baz,\"]"
(reduce #(read-chunk %2 %1) (cons text chunkers)))
(defn read-all-rows [chunkers starting-text]
"Repeatedly applies chunkers to text until the end of the
text is reached."
(reverse (loop [text starting-text result []]
(if (empty? text)
result
(let [[row remainder] (read-row chunkers text)]
(recur remainder (cons row result)))))))
The full source is at https://github.com/mowat27/clam
Hopefully that makes sense but please let me know if you have any queries
Many Thanks
Adrian
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en