Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Clojure, how to cons or conj the elements of a collection but not the collection itself?

Tags:

People also ask

What does cons do in Clojure?

A Clojure Cons takes an arbitrary Object as head, and an ISeq as tail. Since Cons itself implements ISeq , you can build sequences out of Cons cells, but they can just as well point to vectors, or lists, etc. (Note that a "list" in Clojure is a special type ( PersistentList ), and is not built from Cons objects.)

What is sequence in Clojure?

Clojure defines many algorithms in terms of sequences (seqs). A seq is a logical list, and unlike most Lisps where the list is represented by a concrete, 2-slot structure, Clojure uses the ISeq interface to allow many data structures to provide access to their elements as sequences.


cons currently behaves like so:

(cons '(1 2) '(3))
;=> ((1 2) 3)

I would like to achieve:

(magic-cons '(1 2) '(3))
;=> (1 2 3)

I couldn't find a resource for this yet this seems so simple I feel there should be a built in function.

Or I just don't know the write words to describe this situation. Either way, please let me know. Thanks!

Edit: Please don't answer with "flatten" :P ie

(flatten (cons '(1 2) '(3)))