Re: problem with take-while

2011-07-27 Thread Michael Wood
On 27 July 2011 00:03, axyzxp axy...@gmail.com wrote:
 Hi,
 experimenting with clojure API i had this:

 user= (take-while #(= (mod 20 %) 0) (apply (fn [x y] (rest (range
 (max x y [10 20]))
 (1 2)

 but i expect to have (1 2 5 10) because of (apply (fn [x y] (rest
 (range (max x y [10 20]) returns (1 2 3 4 5 6 7 8 9 10 11 12 13 14
 15 16 17 18 19) and (mod 20 5) and (mod 20 10) should give me true and
 not should be dropped...

 I'm sure i'm doing some big nasty errors but don't know where...

In addition to the other answers, you can use:

(range 1 20)

instead of:

(rest (range 20))

and zero? instead of (= x 0).

e.g.:

(filter #(zero? (mod 20 %))
  (range 1 (apply max [10 20])))

-- 
Michael Wood esiot...@gmail.com

-- 
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: problem with take-while

2011-07-27 Thread axyzxp
thanks guys, now everything is clearer to me.
I really apreciate your help

-- 
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: problem with take-while

2011-07-27 Thread Ken Wesson
On Wed, Jul 27, 2011 at 1:51 PM, axyzxp axy...@gmail.com wrote:
 thanks guys, now everything is clearer to me.
 I really apreciate your help

You're welcome.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

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


problem with take-while

2011-07-26 Thread axyzxp
Hi,
experimenting with clojure API i had this:

user= (take-while #(= (mod 20 %) 0) (apply (fn [x y] (rest (range
(max x y [10 20]))
(1 2)

but i expect to have (1 2 5 10) because of (apply (fn [x y] (rest
(range (max x y [10 20]) returns (1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19) and (mod 20 5) and (mod 20 10) should give me true and
not should be dropped...

I'm sure i'm doing some big nasty errors but don't know where...

-- 
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: problem with take-while

2011-07-26 Thread Stephen C. Gilardi

 user= (take-while #(= (mod 20 %) 0) (apply (fn [x y] (rest (range
 (max x y [10 20]))
 (1 2)
 
 but i expect to have (1 2 5 10) because of (apply (fn [x y] (rest
 (range (max x y [10 20]) returns (1 2 3 4 5 6 7 8 9 10 11 12 13 14
 15 16 17 18 19) and (mod 20 5) and (mod 20 10) should give me true and
 not should be dropped...

take-while stops returning more elements as soon as the condition is false.

--Steve

-- 
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: problem with take-while

2011-07-26 Thread Ken Wesson
On Tue, Jul 26, 2011 at 8:27 PM, Stephen C. Gilardi squee...@mac.com wrote:

 user= (take-while #(= (mod 20 %) 0) (apply (fn [x y] (rest (range
 (max x y [10 20]))
 (1 2)

 but i expect to have (1 2 5 10) because of (apply (fn [x y] (rest
 (range (max x y [10 20]) returns (1 2 3 4 5 6 7 8 9 10 11 12 13 14
 15 16 17 18 19) and (mod 20 5) and (mod 20 10) should give me true and
 not should be dropped...

 take-while stops returning more elements as soon as the condition is false.
 --Steve

The function that does what the OP seems to want is filter.

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
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: problem with take-while

2011-07-26 Thread OGINO Masanori
Because take-while takes while (= (mod 20 %) 0) and (= (mod 20 3) 0)
returns false.
Use filter, but be careful filter will hung up with infinite sequences.

-- 
Name:  OGINO Masanori (荻野 雅紀)
E-mail: masanori.og...@gmail.com

-- 
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: problem with take-while

2011-07-26 Thread OGINO Masanori
filter will hung up with infinite sequences. is incorrect. Sorry.
filter may hung if you request something impossible.

For (silly) example, (second (filter even? (range))) returns 2.
However (second (filter zero? (range))) goes in search of the second
zero in natural numbers...

-- 
Name:  OGINO Masanori (荻野 雅紀)
E-mail: masanori.og...@gmail.com

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