On Fri, Jan 23, 2009 at 3:39 PM, Justin Johnson
<[email protected]> wrote:
> On Fri, Jan 23, 2009 at 2:32 PM, Mark Volkmann <[email protected]>
> wrote:
>>
>> This must be something I learned months ago and then forgot ...
>> embarassing!
>> What's the easiest way to determine if a sequence contains a given value?
>> I thought there would be something like this: (include? [2 4 7] 4) -> true
>> That doesn't exist.
Sure it does:
user=> (require 'clojure.contrib.seq-utils)
nil
user=> (clojure.contrib.seq-utils/includes? [2 4 7] 4)
true
:-)
>> I know I can do this: (some #{4} [2 4 7])
>> Having to create a set seems overkill.
It's not! It's beautiful and succinct. And it let's you test for any
of several values, and then tells you which it found:
user=> (some #{3 4 5} [2 4 7])
4
> user=> (contains? [1 2 3] 1)
> true
This is doing something different:
user=> (contains? [2 4 7] 7)
false
That's telling you that the vector has no value at index 7. The docs
try to explain this:
user=> (doc contains?)
-------------------------
clojure.core/contains?
([coll key])
Returns true if key is present in the given collection, otherwise
returns false. Note that for numerically indexed collections like
vectors and Java arrays, this tests if the numeric key is within the
range of indexes. 'contains?' operates constant or logarithmic time;
it will not perform a linear search for a value. See also 'some'.
--Chouser
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---