idiomatic filter-not or inverting predicate

624 views
Skip to first unread message

Andy C

unread,
Aug 21, 2014, 4:01:21 PM8/21/14
to clo...@googlegroups.com
Hi,


I was wondering what is the nicest way to do filter-not in Clojure. Here are 3 expressions:

user=> (filter #(apply = %) '([1 2] [1 1]))
([1 1])
user=> (filter #(apply not= %) '([1 2] [1 1]))
([1 2])
user=> (filter #(not (apply = %)) '([1 2] [1 1]))
([1 2])


First one is just a base filtering. Second is taking advantage of having inverted predicate. Now let's assume that we have only a "positive" predicate, hence we would have to invert it by hand. That leads me to 3rd expression above.

I was wondering though is it is possible to somehow get "not" closer to "=", so the fact that we invert it is more obvious ...

Is it possible to do that? Or perhaps there is an easier way to code it up all together ?

Thx,
Andy

Sam Ritchie

unread,
Aug 21, 2014, 4:04:26 PM8/21/14
to clo...@googlegroups.com
I think you want complement, like (filter (complement odd?) ...)

August 21, 2014 at 2:01 PM
--
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.

--
Sam Ritchie (@sritchie)

Ambrose Bonnaire-Sergeant

unread,
Aug 21, 2014, 4:07:13 PM8/21/14
to clojure
Also see clojure.core/remove.

Daniel Solano Gómez

unread,
Aug 21, 2014, 4:07:48 PM8/21/14
to clo...@googlegroups.com
What about 'remove' instead of 'filter'?

Sincerely,

Daniel

Andy C

unread,
Aug 28, 2014, 11:30:20 PM8/28/14
to clo...@googlegroups.com
Hi,

Thanks a lot for hints. "remove" was what I needed.

This
    (filter (complement #(apply = %)) '([1 2] [1 1]))

also looks very clean now.

Regards,
Andy

Reply all
Reply to author
Forward
0 new messages