Any serious programming language has a variety of collections in its library. Most reduce the resulting complexity by overloading the common operations for multiple types. But few include cons
among the generic operations. This is partly because it's unnatural for many types - only the functional collections like lists and trees support it efficiently. But it may also be a problem that the operation has no obvious name.
Dylan calls it add
, which sounds like arithmetic. Clojure calls it conj
, for conjoin
, which is unfortunately confusable with the complex conjugate function. C++ has push
, but that's implicitly imperative. Other possibilities: extend
, augment
(or aug
?), with
, or maybe a more abstract symbol like &
. Does anyone have a better idea?
+
ReplyDelete+ is kind of taken for addition. It could be overloaded, but that's an even more confusing overloading than + for concatenation.
ReplyDeleteGood point! I like overloading, but I'm sure there are good general arguments against it.
ReplyDeleteStill, it feels to me like something that should be represented by a symbolic operator rather thahn a word. So I like your idea of
&
or you could go for something like
+++
Having said all that, the main reason I suggested
+
was that I couldn't resist the chance to make a one-byte comment :-)
E calls it "with", and provides it for lists, maps, and sets. E.g.:
ReplyDelete[1, 2].with(3)
--> [1, 2, 3]
[3 => 'a', 7 => 'b'].with(4, 'c')
--> [3 => 'a', 7 => 'b', 4 => 'c']