I know this has been discussed on the list before to some extent, but
does clojure by default have any operations which actually do what
"contains?" sounds like it would do, for all collections? I know that
you can easily write something similar using "some", but often times
you just want to find if something is in a list of items.
I've seen Rich say that contains? is for associative things, but I
think it is an unfortunate name - if the idea is to express that the
collection has a value for that key, I think the name would ideally
express that, like:
contains-key?
has-key?
maps?
I think "in?" would behave like "contains?" when given a map:
(in? {:a 1 :b 2 :c 3} :a) => true
(in? {:a 1 :b 2 :c 3} :d) => false
I thought we had beaten this one entirely to death:
http://groups.google.com/group/clojure/browse_thread/thread/ff224d2b88b671e7/575cefc2c03ce154
And yet it lives!
What is the drawback of the (some #{:y} [:x :y :z]) idiom? Is it too
verbose? Too slow? Too flexible? Too good a re-use of existing
functionality? Too helpful in opening ones eyes to the possibilities
of sets and higher order functions?
And if you really don't want to use it (why again?) there is
clojure.contrib.seq-utils/includes?, so why not use that?
--Chouser
I vote for too verbose. ;-)
> And if you really don't want to use it (why again?) there is
> clojure.contrib.seq-utils/includes?, so why not use that?
I'd like for that to be moved to core so I don't have to load it ...
which is also verbose for something that is commonly needed.
--
R. Mark Volkmann
Object Computing, Inc.
(some #{:y] [:x :y :z]) is fewer characters than
(includes? :y [:x :y :z]) so your vote of "too verbose" is hard to justify.
Good point. I was thinking more in terms of number of "noise"
characters instead of number of total characters. Looking at it that
way it's
(${}[])
versus
([])
The current form requires you to think a little more. You're thinking
"I want to see if this thing is one of several possible values.
So I want to compare one thing to a set of things.
How do I do that?
Oh, I have to put the one thing in a set and the set of things in a vector."
That's easy to remember if you code in Clojure daily, but not so easy
if you only code in Clojure once every two weeks and code in Java or
something else the rest of the time.
I'd like for that to be moved to core so I don't have to load it ...
which is also verbose for something that is commonly needed.
Embarassingly, I hadn't even considered that option. I'm happy with
the following.
user=> (def s #{"a" "c" "d" "m"})
#'user/s
user=> (contains? s "d")
true
user=> (contains? s "e")
false
You're right. I was using a vector where I should have used a set.