The :while modifier (list comprehensions)

2009-08-03 Thread Jonas

I find the :while modifier non intuitive

user= (for [x (range 1 10) y (range 1 10) :while (= y 2)] [x y])
()
user= (for [x (range 1 10) y (range 1 10) :while (= x 2)] [x y])
([2 1] [2 2] [2 3] [2 4] [2 5] [2 6] [2 7] [2 8] [2 9])

My (false) intuition told me that both expressions would have been
evaluated to an empty sequence. Could someone explain the rationale
behind the :while modifier?

--~--~-~--~~~---~--~~
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: The :while modifier (list comprehensions)

2009-08-03 Thread Chouser

On Mon, Aug 3, 2009 at 2:47 AM, Jonasjonas.enl...@gmail.com wrote:

 I find the :while modifier non intuitive

 user= (for [x (range 1 10) y (range 1 10) :while (= y 2)] [x y])
 ()
 user= (for [x (range 1 10) y (range 1 10) :while (= x 2)] [x y])
 ([2 1] [2 2] [2 3] [2 4] [2 5] [2 6] [2 7] [2 8] [2 9])

 My (false) intuition told me that both expressions would have been
 evaluated to an empty sequence. Could someone explain the rationale
 behind the :while modifier?

:while only bails from the loop on which it is placed.  In
your examples that's the 'y' loop, so the 'x' loop is
unaffected by your :while and proceeds through its entire
range in both examples.  In the first example, the 'y'
loop never produces anything so the result is empty.

The :while modifier for 'doseq' behaves the same way.

This has come up before:
http://clojure-log.n01se.net/date/2009-06-12.html#15:02a-15:33

--Chouser

--~--~-~--~~~---~--~~
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: The :while modifier (list comprehensions)

2009-08-03 Thread Meikel Brandmeyer

Hi,

On Aug 3, 8:47 am, Jonas jonas.enl...@gmail.com wrote:
 I find the :while modifier non intuitive

 user= (for [x (range 1 10) y (range 1 10) :while (= y 2)] [x y])
 ()
 user= (for [x (range 1 10) y (range 1 10) :while (= x 2)] [x y])
 ([2 1] [2 2] [2 3] [2 4] [2 5] [2 6] [2 7] [2 8] [2 9])

 My (false) intuition told me that both expressions would have been
 evaluated to an empty sequence. Could someone explain the rationale
 behind the :while modifier?

The :while modifier works on the clause just before it. That means
that in your case the :while works on the `y` clause, but there `x`
is constant. So you get a result when `x` is equal to two.

When you put the :while at the `x` clause you get the expected empty
seq.

user= (for [x (range 1 10) :while (= x 2) y (range 1 10)] [x y])
()

Hope this helps.

Sincerely
Meikel

--~--~-~--~~~---~--~~
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: The :while modifier (list comprehensions)

2009-08-03 Thread Jonas Enlund

 When you put the :while at the `x` clause you get the expected empty
 seq.

 user= (for [x (range 1 10) :while (= x 2) y (range 1 10)] [x y])
 ()

Interesting, I didn't know that.

Still, the behavior of :while feels strange. I guess I'll get used to it.

In the following example :while and :when are interchangeable, which
is often the case when :while is used last in the list comprehension:

user= (for [x (range 1 10) y (range 1 10) :while ( (+ x y) 5)] [x y])
([1 1] [1 2] [1 3] [2 1] [2 2] [3 1])

user= (for [x (range 1 10) y (range 1 10) :when ( (+ x y) 5)] [x y])
([1 1] [1 2] [1 3] [2 1] [2 2] [3 1])

--~--~-~--~~~---~--~~
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: The :while modifier (list comprehensions)

2009-08-03 Thread Meikel Brandmeyer

Hi,

Am 03.08.2009 um 16:56 schrieb Jonas Enlund:


In the following example :while and :when are interchangeable, which
is often the case when :while is used last in the list comprehension:

user= (for [x (range 1 10) y (range 1 10) :while ( (+ x y) 5)] [x  
y])

([1 1] [1 2] [1 3] [2 1] [2 2] [3 1])

user= (for [x (range 1 10) y (range 1 10) :when ( (+ x y) 5)] [x y])
([1 1] [1 2] [1 3] [2 1] [2 2] [3 1])


I would suspect, that :while stops earlier than :when.
So while the result is the same, I would suspect that
the :while has a (even if tiny) performance advantage.

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature