Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In clojure, why does assoc require arguments in addition to a map, but dissoc not?

Tags:

clojure

In clojure,

(assoc {})

throws an arity exception, but

(dissoc {})

does not. Why? I would have expected either both of them to throw an exception, or both to make no changes when no keys or values are provided.

EDIT: I see a rationale for allowing these forms; it means we can apply assoc or dissoc to a possibly empty list of arguments. I just don't see why one would be allowed and the other not, and I'm curious as to whether there's a good reason for this that I'm missing.

like image 395
Rob Lachlan Avatar asked Apr 23 '13 16:04

Rob Lachlan


1 Answers

I personally think the lack of 1-arity assoc is an oversight: whenever a trailing list of parameters is expected (& stuff), the function should normally be capable of working with zero parameters in order to make it possible to apply it to an empty list.

Clojure has plenty of other functions that work correctly with zero arguments, e.g. + and merge.

On the other hand, Clojure has other functions that don't accept zero trailing parameters, e.g. conj.

So the Clojure API is a bit inconsistent in this regard.....

like image 166
mikera Avatar answered Oct 03 '22 04:10

mikera