bug? #'separate in seq-utils

2 views
Skip to first unread message

reynard

unread,
Feb 18, 2010, 6:32:28 AM2/18/10
to Clojure

ok, the following works as expected.

user> (separate (fn [n]
(= n 2))
'(1 2 3 4 5))
[(2) (1 3 4 5)]


now, I want to randomly select a number from '(1 2 3 4 5), I change
the function to the following, and the result is NOT expected.

user> (separate (fn [n]
(= n (inc (int (rand 5)))))
'(1 2 3 4 5))
[() (2 3 4)]


Is it a bug in the separate function or do I use it incorrectly?

Stuart Halloway

unread,
Feb 18, 2010, 9:51:55 AM2/18/10
to clo...@googlegroups.com
You are using it incorrectly. Separate returns a vector of things that
match, and a vector of things that don't match. Every time you call
the function it matches against a different random number. Take a look
at rand-elt in clojure.contrib.seq-utils.

Maybe the docstring for separate should say that fn should be side-
effect-free...

Stu

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

reynard

unread,
Feb 18, 2010, 10:07:22 AM2/18/10
to Clojure

On Feb 18, 10:51 pm, Stuart Halloway <stuart.hallo...@gmail.com>
wrote:


> You are using it incorrectly. Separate returns a vector of things that  
> match, and a vector of things that don't match. Every time you call  
> the function it matches against a different random number. Take a look  
> at rand-elt in clojure.contrib.seq-utils.

Yes, the problem is solved by first binding the result of the random
number, before applying it inside #'separate, as follows, thanks

user> (let [rand-num (inc (int (rand 5)))]
(separate (fn [n]
(= n rand-num))
'(1 2 3 4 5)))
[(4) (1 2 3 5)]

> Maybe the docstring for separate should say that fn should be side-
> effect-free...

I concur.

Reply all
Reply to author
Forward
0 new messages