Filter predicates and side-effects

99 views
Skip to first unread message

James Elliott

unread,
Sep 2, 2015, 11:48:15 AM9/2/15
to Clojure
I notice that the documentation for filter and filterv are emphatic that the predicate you are using be free of side effects. Why is that so? For example, I would like to use filterv to collect a list of lighting effects which respond false to a protocol method which asks whether they have finished. In the implementation of that method, the effects are free to update their internal state based on the current show state and time, when deciding whether or not they are finished, and so I can’t guarantee there will be no side effects while filtering the effects. Does that mean I can’t use filterv? If not, why not? Looking at the source, I see no obvious problems.

Raymond Huang

unread,
Sep 2, 2015, 12:06:01 PM9/2/15
to clo...@googlegroups.com
I believe the reason it's not recommended is because it returns a lazy chunked sequence. This means that it's not guaranteed to eagerly perform your side-effects, unless you wrap it in `doall`.

On Wed, Sep 2, 2015 at 8:48 AM, James Elliott <brun...@gmail.com> wrote:
I notice that the documentation for filter and filterv are emphatic that the predicate you are using be free of side effects. Why is that so? For example, I would like to use filterv to collect a list of lighting effects which respond false to a protocol method which asks whether they have finished. In the implementation of that method, the effects are free to update their internal state based on the current show state and time, when deciding whether or not they are finished, and so I can’t guarantee there will be no side effects while filtering the effects. Does that mean I can’t use filterv? If not, why not? Looking at the source, I see no obvious problems.

--
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
---
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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Elliott

unread,
Sep 2, 2015, 12:13:25 PM9/2/15
to Clojure
Indeed, that would make sense for filter, but since filterv immediately puts them all into a vector, I was particularly puzzled by the warning on its doc string.

In my case I am not relying on the side effects for anything, I merely cannot guarantee there are none (and even if chunking were an issue, the list is immediately fully consumed). So I tentatively think I am ok, but it is still an unsettling warning.

Michael Blume

unread,
Sep 2, 2015, 12:47:50 PM9/2/15
to Clojure
Don't call filter for side effects, don't rely on when side effects will or will not happen, but yeah, your filter predicate can have side effects.
Reply all
Reply to author
Forward
0 new messages