--
R. Mark Volkmann
Object Computing, Inc.
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.
I know I can do this: (some #{4} [2 4 7])
Having to create a set seems overkill.
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
That doesn't do what you think it does. For example,
(contains? [2 4 7] 4) -> false
The doc string includes this: "Note that for numerically indexed
collections like vectors and Java arrays, this tests if the numeric
key is within the range of indexes." So it doesn't compare the
values.
I'd like to use it with strings like this to determine if a letter is a vowel:
(contains? "aeiou" letter)
but that doesn't work either.
Thanks! It's too bad something this basic isn't in the core.
>>> 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
Well ... I agree that it's beautiful and succinct IF you want to test
multiple values. I just think there should be a simpler way to test
for one value that is in the core.
>> 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.
Yeah, I just figured that out while you were composing your reply.
Thanks!
user=> (some (set "aeiou") "dn'tndthsstinkngvwls")
\i
Or, if you must,
user=> (clojure.contrib.seq-utils/includes? "aeiou" \o)
true
--Chouser
Why does this work
(some (set "aeiou") "e")
but this doesn't
(some #{"aeiou"} "e")
I thought (set ...) was equivalent to #{...}.
> Or, if you must,
>
> user=> (clojure.contrib.seq-utils/includes? "aeiou" \o)
> true
--
(hash-set ...) is equivalent to #{...}
'set' takes a single collection as an argument, which it will treat as
a seq and pour into a hash-set, so above the string behaves as a seq
of chars, and each char becomes an item in the set:
user=> (set "aeiou")
#{\a \e \i \o \u}
On the other hand, hash-set and #{} expect several args, each of which
becomes an item in the set:
user=> (hash-set "aeiou")
#{"aeiou"}
--Chouser
(defn pig-latin [word]
(let [first-letter (first word)]
(if (some (set "aeiou") (str first-letter))
(str word "ay")
(str (subs word 1) first-letter "ay"))))
(println (pig-latin "red")) -> "edray"
(println (pig-latin "orange")) -> "orangeay"
I'm not quite sure what your criteria is, but here are a couple other options:
; String.contains and an extra 'subs'
(defn pig-latin [word]
(let [first-letter (subs word 0 1)]
(if (.contains "aeiou" first-letter)
(str word "ay")
(str (subs word 1) first-letter "ay"))))
; String.contains with destructuring
(defn pig-latin [[first-letter :as word]]
(if (.contains "aeiou" (str first-letter))
(str word "ay")
(str (subs word 1) first-letter "ay")))
; Strings collapse quivering in fear when destructuring and regex join forces
(defn pig-latin [word]
(if-let [[_ a b] (re-find #"^([^aeiou])(.*)" word)]
(str b a "ay")
(str word "ay")))
--Chouser
Thanks! I like the second option the best.
I hope option three was written to be funny. I both laugh and cry when
looking at that. ;-)