On Mon, Feb 7, 2011 at 9:16 PM, Andreas Kostler <andreas.koestler.le...@gmail.com> wrote: > Thank's Ken, that's doing what I want. However, being the noob I am, I don't > quite understand what's going on. > Do you mind elaborating a little bit on the functions? Your help is greatly > appreciated :)
You're welcome. Take-until is pretty straightforward. (partition l 1 s) means that given "foobarbazquux" as input sequence and "baz" as subseq, you get ((\f \o \o) (\o \o \b) (\o \b \a) ...) i.e., every set of three consecutive characters. (map vector s ss) just pairs these up with the input sequence: ([\f (\f \o \o)] [\o (\o \o \b)] [\o (\o \b \a)] ...) The take-while predicate destructures these vectors and ignores the first argument, looking for the second (the three consecutive characters) to not equal the subsequence. So it takes until [\b (\b \a \z)] where "baz" matches and stops there, omitting that vector and all the subsequent ones. The outer (map first ...) just ditches the subsequences and leaves the letters from the input sequence, stopping where the take-while stopped. That's take-until-subseq in a nutshell. It's lazy; the subseq must not be infinite but the main sequence may be without it wedging. The read-until prompt quite straightforwardly uses a lazy seq on an input stream in take-until-subseq to find the prompt and stop there; (apply str ...) turns the output seq back into a string. That leaves lazy-input. This uses the lazy-seq macro to directly implement a lazy sequence. It returns a lazy seq that will generate its first cons cell with (step), which when called produces a first of (char (.read input-stream)) and a rest of (lazy-seq (step)), i.e. the same lazy seq on the stream again (but with a character now consumed from the stream). It checks for the read method's sentinel value -1, though, returning nil in that case (by falling through when-not) which ends the lazy sequence on EOF. -- 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