As it seems clojure.set/union can be used with maps as parameters too.
=>(clojure.set/union {:a 1 :b 2 :c 3 :d 4} {:a 1 :b 4 :c 4})
{:a 1, :c 4, :b 4, :d 4}
So it is possible to get a union of keys of a map with
=> (keys (clojure.set/union {:a 1 :b 2 :c 3 :d 4} {:a 1 :b 4 :c 4}))
(:a :c :b :d)
If I have a map with nil as key
=> (clojure.set/union {:a 1 :b 2 :c 3 nil 4} {:a 1 :b 4 :c 4 nil 7})
{nil 7, :a 1, :c 4, :b 4}
with 'false' as key
=>(clojure.set/union {:a 1 :b 2 :c 3 false 4} {:a 1 :b 4 :c 4 false 7})
{:a 1, :c 4, :b 4, false 7}
with 'true' and 'false' as keys and value
=> (clojure.set/union {:a 1 true 2 :c 3 false true} {:a 1 true 4 :c 4
false 7})
{:a 1, :c 4, true 4, false 7}
clojure.set/intersection on the otherside throws an exception
=> (clojure.set/intersection {:a 1 :b 2 :c 3 :d 4} {:a 1 :b 4 :c 4})
ClassCastException clojure.lang.PersistentArrayMap cannot be cast to
clojure.lang.IPersistentSet clojure.core/disj (core.clj:1436)
The same for clojure.set/difference
(clojure.set/difference {:a 1 :b 2 :c 3 :d 4} {:a 1 :b 4 :c 4})
ClassCastException clojure.lang.PersistentArrayMap cannot be cast to
clojure.lang.IPersistentSet clojure.core/disj (core.clj:1436)
As it seems only union accepts nearly any type of collection where
difference and intersection throws exception if the passed parameters
are not sets.
Am 01.07.2013 21:58, schrieb Mark Engelberg:
Expanding on what Jim said, you don't usually need to convert the keys
of a map into a set, because the map pretty much acts like a set of
its keys if you use contains?
For example, (contains? {:a 1, :b 2} :a) tests whether :a is among the
set of keys in the map.
I believe that clojure.set's union, intersection, and difference all
work out of the box on maps (acting as if they are sets of keys), but
you should do some testing and/or inspection of the source code to
verify this, especially on maps with nil or false values.
On Mon, Jul 1, 2013 at 12:37 PM, Jim - FooBar(); <jimpil1...@gmail.com
<mailto:jimpil1...@gmail.com>> wrote:
On 01/07/13 20:02, Pablo Nussembaum wrote:
This is pretty strange behavior to me, why is the case that keys
function don't return a set like java?
It doesn't need to be, does it?...the map itself ensures that
there are no duplicate keys anyway. I guess it's one of those
'less is more' cases...
Jim
--
--
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
<mailto: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
<mailto:clojure%2bunsubscr...@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 unsubscribe from this group and stop receiving emails from it,
send an email to clojure+unsubscr...@googlegroups.com
<mailto:clojure%2bunsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
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 unsubscribe from this group and stop receiving emails from it, send
an email to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
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 unsubscribe from this group and stop receiving emails from it, send an email
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.