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 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 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-func [n]
  (cond
   (< n 4) (str n)
   :else "error"))

(for [n [1 2 3 4 5]
  :let [result (my-func n)]
  :while (not= result "error")]
  result)

Cheers,
James

On Sunday, November 24, 2013 5:19:49 PM UTC+1, David Simmons wrote:
>
> 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 of the 
> items. I know map isn't what I'm looking for but is there a function or 
> some idiomatic piece of clojure to achieve my aim.
>
> 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 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 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 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 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 2013 17:13, David Simmons  wrote:

> 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.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 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@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 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 isn't what I'm looking for but is there a function or some idiomatic 
> piece of clojure to achieve my aim.

If you only want to break when there's an error, you could use exceptions.

Alternatively, if my-func ordinarily doesn't return nil, you could take 
advantage of map's laziness by having my-func return nil on failure-- then you 
just stop consuming the output of map when you hit a nil value.

-- 
-- 
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 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.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 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 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 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 isn't what I'm looking for but is there a function or
>> some idiomatic piece of clojure to achieve my aim.
>>
>> 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 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.
>

-- 
-- 
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 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 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 isn't what I'm looking for but is there a function or
> some idiomatic piece of clojure to achieve my aim.
>
> 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 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 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 of the 
items. I know map isn't what I'm looking for but is there a function or 
some idiomatic piece of clojure to achieve my aim.

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 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.