Re: Breaking out of doseq
What about (first (filter identity (map (fn [[rx f]] (if (matches? rx input) (f input))) regexes))), where regexes is something like [[regex1 f1] [regex2 f2] ...] and input is some string. As long as none of the fs returns nil, this should work, using lazy sequence behavior to stop at the first matching regex. (You may want to do something to de-chunk the regexes vector, if you use a vector, if the fs have side effects or any of the regexes or fs are expensive, and use the de-chunked seq as the argument to map.) The whole shebang should return nil if none of the regexes match (via calling "first" on an empty sequence). On Wed, Nov 27, 2013 at 10:21 AM, Phillip Lord wrote: > > I think you want to use "some" instead -- you need a list of regexps > anyway given that, as you say, the order is significant. > > Some can return the first match that hits. doseq is not good for this > anyway, since doseq doesn't return anything. > > Phil > > Jonathon McKitrick writes: > > > To clarify what I'm trying to do, I have a map of regexes, and after > > iterating them, when one matches (the order of the regexes is > significant) > > I want exactly one result returned by applying the looked up function to > > the string. After that regex matches, no more matches should be > attempted. > > > > What is the 'reduced' function? > > > > On Wednesday, November 27, 2013 9:26:21 AM UTC-5, Jonathon McKitrick > wrote: > >> > >> I'm iterating a map (regex -> function) and I'd like to call FUNCTION > with > >> the result of re-groups after a match for REGEX is found. I also want > to > >> exit the sequence, returning the results of FUNCTION. In common lisp, I > >> would use return-from, but how would this be done in clojure? > >> > >> > > > > -- > -- > Phillip Lord, Phone: +44 (0) 191 222 7827 > Lecturer in Bioinformatics, Email: > phillip.l...@newcastle.ac.uk > School of Computing Science, > http://homepages.cs.ncl.ac.uk/phillip.lord > Room 914 Claremont Tower, skype: russet_apples > Newcastle University, twitter: phillord > NE1 7RU > > -- > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Breaking out of doseq
I think you want to use "some" instead -- you need a list of regexps anyway given that, as you say, the order is significant. Some can return the first match that hits. doseq is not good for this anyway, since doseq doesn't return anything. Phil Jonathon McKitrick writes: > To clarify what I'm trying to do, I have a map of regexes, and after > iterating them, when one matches (the order of the regexes is significant) > I want exactly one result returned by applying the looked up function to > the string. After that regex matches, no more matches should be attempted. > > What is the 'reduced' function? > > On Wednesday, November 27, 2013 9:26:21 AM UTC-5, Jonathon McKitrick wrote: >> >> I'm iterating a map (regex -> function) and I'd like to call FUNCTION with >> the result of re-groups after a match for REGEX is found. I also want to >> exit the sequence, returning the results of FUNCTION. In common lisp, I >> would use return-from, but how would this be done in clojure? >> >> > > -- -- Phillip Lord, Phone: +44 (0) 191 222 7827 Lecturer in Bioinformatics, Email: phillip.l...@newcastle.ac.uk School of Computing Science, http://homepages.cs.ncl.ac.uk/phillip.lord Room 914 Claremont Tower, skype: russet_apples Newcastle University, twitter: phillord NE1 7RU -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Breaking out of doseq
2013/11/27 Jonathon McKitrick > To clarify what I'm trying to do, I have a map of regexes, and after > iterating them, when one matches (the order of the regexes is significant) > I want exactly one result returned by applying the looked up function to > the string. After that regex matches, no more matches should be attempted. Sounds like you want clojure.core/filter. -- MK http://github.com/michaelklishin http://twitter.com/michaelklishin -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Breaking out of doseq
reduced wraps a value in such a way that it stops the current reduce and returns a value. You might want `some` in this case. (let [matched (some matches? regexes)] ...) http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/some Also filter + first has a similar effect. Thanks, Ambrose On Wed, Nov 27, 2013 at 10:43 PM, Jonathon McKitrick wrote: > To clarify what I'm trying to do, I have a map of regexes, and after > iterating them, when one matches (the order of the regexes is significant) > I want exactly one result returned by applying the looked up function to > the string. After that regex matches, no more matches should be attempted. > > What is the 'reduced' function? > > > On Wednesday, November 27, 2013 9:26:21 AM UTC-5, Jonathon McKitrick wrote: >> >> I'm iterating a map (regex -> function) and I'd like to call FUNCTION >> with the result of re-groups after a match for REGEX is found. I also want >> to exit the sequence, returning the results of FUNCTION. In common lisp, I >> would use return-from, but how would this be done in clojure? >> >> -- > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Breaking out of doseq
To clarify what I'm trying to do, I have a map of regexes, and after iterating them, when one matches (the order of the regexes is significant) I want exactly one result returned by applying the looked up function to the string. After that regex matches, no more matches should be attempted. What is the 'reduced' function? On Wednesday, November 27, 2013 9:26:21 AM UTC-5, Jonathon McKitrick wrote: > > I'm iterating a map (regex -> function) and I'd like to call FUNCTION with > the result of re-groups after a match for REGEX is found. I also want to > exit the sequence, returning the results of FUNCTION. In common lisp, I > would use return-from, but how would this be done in clojure? > > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Breaking out of doseq
Ah, maybe recursion is the answer! On Wednesday, November 27, 2013 9:26:21 AM UTC-5, Jonathon McKitrick wrote: > > I'm iterating a map (regex -> function) and I'd like to call FUNCTION with > the result of re-groups after a match for REGEX is found. I also want to > exit the sequence, returning the results of FUNCTION. In common lisp, I > would use return-from, but how would this be done in clojure? > > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Breaking out of doseq
I'm not sure doseq is what you want.. I'd probably just use loop recur. On Wed, Nov 27, 2013 at 9:43 AM, Jonathon McKitrick wrote: > To clarify what I'm trying to do, I have a map of regexes, and after > iterating them, when one matches (the order of the regexes is significant) I > want exactly one result returned by applying the looked up function to the > string. After that regex matches, no more matches should be attempted. > > What is the 'reduced' function? > > > On Wednesday, November 27, 2013 9:26:21 AM UTC-5, Jonathon McKitrick wrote: >> >> I'm iterating a map (regex -> function) and I'd like to call FUNCTION with >> the result of re-groups after a match for REGEX is found. I also want to >> exit the sequence, returning the results of FUNCTION. In common lisp, I >> would use return-from, but how would this be done in clojure? >> > -- > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Breaking out of doseq
Hi Jonathon, I'm not sure I fully understand what you're after, but I suspect reduce + reduced would be helpful. (reduce (fn [a c] (reduced 'foo)) [] [1 2 3]) ;=> 'foo As far as I'm aware you can't exit a doseq early. (Actually I guess you could use a mutable reference as the argument to :while, but reduced is nicer). http://clojuredocs.org/clojure_core/clojure.core/for#example_913 Thanks, Ambrose On Wed, Nov 27, 2013 at 10:26 PM, Jonathon McKitrick wrote: > I'm iterating a map (regex -> function) and I'd like to call FUNCTION with > the result of re-groups after a match for REGEX is found. I also want to > exit the sequence, returning the results of FUNCTION. In common lisp, I > would use return-from, but how would this be done in clojure? > > -- > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Breaking out of doseq
I'm iterating a map (regex -> function) and I'd like to call FUNCTION with the result of re-groups after a match for REGEX is found. I also want to exit the sequence, returning the results of FUNCTION. In common lisp, I would use return-from, but how would this be done in clojure? -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.