(I'm more interested in it's use as a predicate rather than the fact
that it returns a sublist when true.)
"find" and "contains?" are listed under the Maps section of the data
structures page (http://clojure.org/data_structures#toc17) so no good
with lists. I can't think of any other synonyms.
The documentation for "some" suggests this:
(some (fn [elt] (= elt 'a)) '(a b c)) => true
That's pretty verbose, and even the shortcut is kinda long:
(some #(= % 'a) '(a b c)) => true
Although this alternative is alright:
(some #{'a} '(a b c)) => a
Is that the Clojure version of MEMBER?
Aloha,
David Sletten
Am 02.03.2009 um 13:54 schrieb David Sletten:
> Does Clojure have an analog of Lisp's MEMBER function?
> (member 'a '(c a f e b a b e)) => (A F E B A B E)
I don't know the member function of CL, but I interpret
your example, that it cuts away the head of the list until
the first occurence of the given thing.
You can do that in Clojure with drop-while:
(drop-while #(not= % :a) (list :c :a :f :e :b :a :b :e))
=> (:a :f :e :b :a :b :e)
Hope this helps.
Sincerely
Meikel
MEMBER is a fancy predicate that tests whether or not an item is an
element of a given list. I say fancy because rather than simply
returning true when the item is present it returns the tail of the
list starting with the item. It returns nil otherwise. I don't care
about this fancy value. I just want to know whether or not the item
is in the list without too much verbosity.
I think (some #{<ITEM>} <LIST>) is probably the way to do it.
> You can do that in Clojure with drop-while:
>
> (drop-while #(not= % :a) (list :c :a :f :e :b :a :b :e))
> => (:a :f :e :b :a :b :e)
>
Thanks for your example anyway.
Mahalo nui loa (vielen dank),
David Sletten
It's verbose in order to discourage its use since its a linear search.
See the discussion about the contains? function at
http://www.ociweb.com/mark/clojure/article.html#Lists
and http://www.ociweb.com/mark/clojure/article.html#Sets.
--
R. Mark Volkmann
Object Computing, Inc.
>
> It's verbose in order to discourage its use since its a linear search.
> See the discussion about the contains? function at
> http://www.ociweb.com/mark/clojure/article.html#Lists
> and http://www.ociweb.com/mark/clojure/article.html#Sets.
Ahh. Nice explanation. Thanks again Mark for the article.
<sheepish>
As you can see I haven't finished reading the whole thing yet.
</sheepish>
Aloha,
David Sletten