Re: Repeatedly applying functions until a condition is met
Brilliant! Thanks Denis Those are 2 great tips. I had looked through the documentation until my eyes went square but I'm not very well practiced with all the functions so I decided to ask the community for help. take-while was exactly what I needed. Many Thanks Adrian On Thursday, 26 July 2012 08:58:13 UTC+1, Adrian Mowat wrote: 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 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
Repeatedly applying functions until a condition is met
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 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: Repeatedly applying functions until a condition is met
On Thu, Jul 26, 2012 at 9:58 AM, Adrian Mowat adrian.mo...@gmail.comwrote: 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 []] Hi, Reversing a list is common in Common Lisp to return a result in its proper form. In Clojure we usually use a vector data structure and the function `conj` to do that. It adds an item to a collection in the beginning or in the end depending of the seq implementation (and in the case of a vector to the end, which is what we what). (if (empty? text) result (let [[row remainder] (read-row chunkers text)] (recur remainder (cons row result))) Always read seven times the Clojure core functions (http://clojuredocs.org, and/or http://www.clojureatlas.com/) before using loop / recur: There's probably a function in the core lib that do what you want. In your particular case I am thinking of `take-while`. (take-while even? [2 4 6 7 9]) (2 4 6) My 2 cents. Denis 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 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