arguments to dissoc and select-keys

69 views
Skip to first unread message

Mark Volkmann

unread,
Feb 21, 2009, 3:28:51 PM2/21/09
to clo...@googlegroups.com
(def popsicle-map {:red :cherry, :green :apple, :purple :grape})

; Note how the keys passed to dissoc are individual arguments.
(dissoc popsicle-map :green :blue) ; -> {:red :cherry, :purple :grape}

; Note how the keys passed to select-keys are in a vector, not
individual arguments.
(select-keys popsicle-map [:red :green :blue]) ; -> {:green :apple,
:red :cherry}

I wonder why these were implemented differently. Maybe both dissoc and
select-keys should accept both individual key arguments and a sequence
of keys.

--
R. Mark Volkmann
Object Computing, Inc.

samppi

unread,
Feb 21, 2009, 5:58:55 PM2/21/09
to Clojure
Allowing dissoc and select-keys to accept both keys as arguments and
as a collection would be nice, and backwards compatible. In any case,
ostensibly it should be consistent; otherwise, it's just an
idiosyncrasy in the language that people will have to deal with. I
wonder what the reasoning is behind it, or if it was completely
arbitrary.

David Nolen

unread,
Feb 21, 2009, 6:07:39 PM2/21/09
to clo...@googlegroups.com
dissoc is like assoc.

If you want to use collections with dissoc you can always use apply. It's not too much to type:

(def my-map {:first "Bob", :middle "Joe", :last "Smith"})
(apply dissoc my-map [:first :middle])

Mark Volkmann

unread,
Feb 21, 2009, 6:24:23 PM2/21/09
to clo...@googlegroups.com
On Sat, Feb 21, 2009 at 5:07 PM, David Nolen <dnolen...@gmail.com> wrote:
> dissoc is like assoc.
> If you want to use collections with dissoc you can always use apply. It's
> not too much to type:
> (def my-map {:first "Bob", :middle "Joe", :last "Smith"})
> (apply dissoc my-map [:first :middle])

I think you misunderstood what I want. I want to be able to pass
individual keys to select-keys. I'm not looking for a way to pass
collections of keys to dissoc and assoc.

My guess is that this inconsistency was purely accidental and could be
fixed so select-keys also accepts individual key arguments.

David Nolen

unread,
Feb 21, 2009, 6:31:44 PM2/21/09
to clo...@googlegroups.com
Oops, sorry. It seems weird to me that a function called select-keys would take a single parameter and not a vector. Also, as you said, this does seem like it would require some kind of check which would probably not worth the tradeoff. Why not write a function called select-key that does what you want?

Mark Volkmann

unread,
Feb 21, 2009, 6:47:35 PM2/21/09
to clo...@googlegroups.com
On Sat, Feb 21, 2009 at 5:31 PM, David Nolen <dnolen...@gmail.com> wrote:
> Oops, sorry. It seems weird to me that a function called select-keys would
> take a single parameter and not a vector. Also, as you said, this does seem
> like it would require some kind of check which would probably not worth the
> tradeoff. Why not write a function called select-key that does what you
> want?

I must not be writing very clearly today. This is what I want to be able to do.

(def popsicle-map {:red :cherry, :green :apple, :purple :grape})

(select-keys popsicle-map :red :green :blue)

That doesn't work though. It throws
java.lang.IllegalArgumentException: Wrong number of args passed to:
core$select-keys.

I have to wrap the keys in a collection like this instead.

(select-keys popsicle-map [:red :green :blue])

However, I can pass any number of individual keys to assoc and dissoc.
That's the inconsistency I'm trying to point out.

Rich Hickey

unread,
Feb 21, 2009, 7:01:52 PM2/21/09
to Clojure


On Feb 21, 5:58 pm, samppi <rbysam...@gmail.com> wrote:
> Allowing dissoc and select-keys to accept both keys as arguments and
> as a collection would be nice, and backwards compatible.

Nope - Collections can be keys.

> In any case,
> ostensibly it should be consistent; otherwise, it's just an
> idiosyncrasy in the language that people will have to deal with. I
> wonder what the reasoning is behind it, or if it was completely
> arbitrary.
>

In an application that uses select-keys heavily, it will be common to
have keysets as their own entities.

That's not the case for dissoc.

Rich

David Nolen

unread,
Feb 21, 2009, 8:49:59 PM2/21/09
to clo...@googlegroups.com
On Feb 21, 5:58 pm, samppi <rbysam...@gmail.com> wrote:
> Allowing dissoc and select-keys to accept both keys as arguments and
> as a collection would be nice, and backwards compatible.

Nope - Collections can be keys.

Ah the power of Clojure ;) The fact that (almost?) anything can be a key is a pretty liberating thing.

David 

Jeffrey Straszheim

unread,
Feb 21, 2009, 9:26:45 PM2/21/09
to clo...@googlegroups.com


On Sat, Feb 21, 2009 at 8:49 PM, David Nolen <dnolen...@gmail.com> wrote:

Ah the power of Clojure ;) The fact that (almost?) anything can be a key is a pretty liberating thing.

It is truly an amazing ability.

Raffael Cavallaro

unread,
Feb 21, 2009, 9:43:42 PM2/21/09
to Clojure


On Feb 21, 6:47 pm, Mark Volkmann <r.mark.volkm...@gmail.com> wrote:

> I have to wrap the keys in a collection like this instead.
>
> (select-keys popsicle-map [:red :green :blue])

of course if you're doing this much:

user=> (defmacro take-keys [some-map & keys+]
`(select-keys ~some-map ~(vec keys+)))
#'user/take-keys
user=> (take-keys popsicle-map :blue :green)
{:green :apple, :blue :blueberry}

samppi

unread,
Feb 21, 2009, 11:20:27 PM2/21/09
to Clojure
I see—that explains a lot.
Reply all
Reply to author
Forward
0 new messages