Thank you for all replies.

The reason that I was using set instead of map was to use the functions
from closure.set. I like them.

But now I agree that map is a better approach.

(select (fn [{:keys [id]}] (not= id 1)) xrel) is O(n). Right?

Can I say that if you need remove an item you should use a map?

What sets are good for?

leandro.


On Fri, Mar 23, 2012 at 10:37 AM, Jay Fields <j...@jayfields.com> wrote:

> Disclaimer, I'm only looking at how I would want to write it. You may
> need to do something else if you have specific performance
> requirements.
>
> clojure.set is probably your friend.
>
> user=> (def xrel #{{:id 1, :foo "bar"} {:id 2, :foo "car"} {:id 3, :foo
> "dog"}})
> #'user/xrel
> user=> (use 'clojure.set)
> nil
> user=> (select (fn [{:keys [id]}] (not= id 1)) xrel)
> #{{:id 2, :foo "car"} {:id 3, :foo "dog"}}
>
> or, if this is something you do often enough
>
> user=> (def xrel #{{:id 1, :foo "bar"} {:id 2, :foo "car"} {:id 3, :foo
> "dog"}})
> #'user/xrel
>
> user=> (use 'clojure.set)
> nil
>
> user=> (require 'clojure.data)
> nil
>
> user=> (defn submap [sub super]
>  (->
>    (clojure.data/diff sub super)
>    last
>    (= sub)))
> #'user/submap
>
> user=> (select (partial (complement submap) {:id 1}) xrel)
> #{{:foo "car", :id 2} {:foo "dog", :id 3}}
>
>
> On Fri, Mar 23, 2012 at 2:50 AM, Philip Potter
> <philip.g.pot...@gmail.com> wrote:
> > Might it be possible to use a map instead? Maps are designed to look
> values
> > up by a key which may differ from the value, which seems to be your use
> case
> > here.
> >
> > If you have
> >
> > {1 {:id 1, :foo "bar"},
> >
> >
> > 2 {:id 2, :foo "car"}}
> >
> > You can just do
> >
> > (disj my-map 2)
> >
> > To convert the original set into a map, you could do something like:
> >
> > (into {} (map (juxt :id identity) my-set))
> >
> > Phil
> >
> > On Mar 23, 2012 2:59 AM, "Leandro Oliveira" <lolive...@gmail.com> wrote:
> >>
> >> Hi all,
> >>
> >> I have a set of hash like this:
> >>
> >> #{{:id 1, :foo "bar"} {:id 2, :foo "car"}}
> >>
> >> and I'd like to remove an item based on its id value.
> >>
> >> Unfortunately, disj requires that I pass the whole map as key to remove
> >> it.
> >>
> >> Can I remove the map from the set only using the id?
> >>
> >> My intention is to avoid a lookup on set to get the whole map back.
> >>
> >> Any suggestions?
> >>
> >>
> >> Thanks in advance.
> >> leandro.
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> >> Groups "Clojure" group.
> >> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> >> For more options, visit this group at
> >> http://groups.google.com/group/clojure?hl=en
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> > For more options, visit this group at
> > http://groups.google.com/group/clojure?hl=en
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to