How about:
(defn filter-keys [coll keys] (filter #(some % keys) coll))
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/
"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
Am 13.06.2011 um 04:36 schrieb Jeff Sigmon:
> Wonderful! Pretty cool how the map itself is the predicate to the some function.
The usual disclaimer about keys with false or nil as value applies.
Sincerely
Meikel
Yes, a map is a function from its keys to its to values, just as a
keyword is also a function (from a map to a value identified by that
keyword).
And, as Meikel notes, if you have a value of false or nil in your map,
my "shortcut" won't work.
If you're on Clojure 1.3.0, a solution that addresses Meikel's concern would be:
(defn filter-keys [coll keys] (filter (apply some-fn (map #(fn [c]
(find c %)) keys)) coll))
Maybe someone can clean that up?
Am 13.06.2011 um 08:00 schrieb Sean Corfield:
> (defn filter-keys [coll keys] (filter (apply some-fn (map #(fn [c]
> (find c %)) keys)) coll))
>
> Maybe someone can clean that up?
Just use contains? in your original solution.
(defn filter-keys
[ks coll]
(filter #(some (partial contains? %) ks) coll))
Not as nice as before, but it should work.
Sincerely
Meikel
Needs to be (defn filter-keys [coll ks] ...) but, yes, that is cleaner
than mine. Nice.
So many functions...
Am 13.06.2011 um 09:19 schrieb Sean Corfield:
>> (defn filter-keys
>> [ks coll]
>> (filter #(some (partial contains? %) ks) coll))
>
> Needs to be (defn filter-keys [coll ks] ...)
I disagree. Clojure puts sequence arguments usually last. See filter, remove, take-while, ... I would exchange the arguments and follow the usual Clojure style.
> So many functions...
Hehe. :) That's why it's called “functional.” ;)
Sincerely
Meikel
The OP specifically asked for (filter-keys input [:a :b]) - I was
merely pointing out your solution had the arguments the wrong way
round _for his question_.
I do agree that the usual practice is to have the collection last and
actually kept doing that while I was trying to solve his puzzle - and
then I'd run his example and it wouldn't work, until I swapped the
arguments :)
--
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