Re: Breaking out of a map type function

2013-11-24 Thread David Simmons
> > Jernau - that looks perfect. I'll give it a go. >> > cheers Dave -- -- 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 patien

Re: Breaking out of a map type function

2013-11-24 Thread Jernau
Hi Dave, Another option is to use the formacro's while clause to stop processing as soon as you hit an error. Here's a basic example with a simple my-func that returns a string-based error to give you an idea of how it could look: (defn my

Re: Breaking out of a map type function

2013-11-24 Thread David Simmons
> > Many thanks James > -- -- 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 th

Re: Breaking out of a map type function

2013-11-24 Thread James Reeves
Something like: (defn safe-sum [coll] (reduce (fn [s x] (if x (+ x s) (reduced s))) coll)) This will compute the sum until it hits a "falsey" (i.e. nil or false) value. Alternatively, you could write it: (defn safe-sum [coll] (apply + (take-while identity coll))) - James On 24 November 20

Re: Breaking out of a map type function

2013-11-24 Thread David Simmons
> > @James - I'll take a look at take-while @Michael - I thought using exceptions to break out of a stuff was considered bad practice? cheers Davew -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure

Re: Breaking out of a map type function

2013-11-24 Thread Michael Gardner
On Nov 24, 2013, at 10:19 , David Simmons wrote: > I wish to process each item in a vector. I know I can use map to do this e.g. > (map my-func my-vector). My problem is that I need to be able to break out of > the map if my-func returns an error when processing any of the items. I know > map

Re: Breaking out of a map type function

2013-11-24 Thread David Simmons
Hi Stu I understand Reduce but can't quite see how this would work. Don't suppose you'd have a simple example would you? Many thanks Dave -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups

Re: Breaking out of a map type function

2013-11-24 Thread James Reeves
Another option is to take-while the values in the sequence are valid, and then map over the ones that are. - James On 24 November 2013 16:50, Stuart Halloway wrote: > Hi Dave, > > You can use reduce for this job, and have the reducing function return a > (reduced retval) when you want to break

Re: Breaking out of a map type function

2013-11-24 Thread Stuart Halloway
Hi Dave, You can use reduce for this job, and have the reducing function return a (reduced retval) when you want to break out. Cheers, Stu On Sun, Nov 24, 2013 at 11:19 AM, David Simmons wrote: > Hi All. > > Still struggling to get my head around Clojure - this is attempt number 4. > > I wish

Breaking out of a map type function

2013-11-24 Thread David Simmons
Hi All. Still struggling to get my head around Clojure - this is attempt number 4. I wish to process each item in a vector. I know I can use map to do this e.g. (map my-func my-vector). My problem is that I need to be able to break out of the map if my-func returns an error when processing any