Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the functions seq?, sequential? and coll?

Tags:

What is the difference between the functions seq? sequential? and coll?

I found some information scattered throughout the internet, but I think it would be better to centralize that information here.

like image 641
Carlos Nunes Avatar asked Mar 16 '14 15:03

Carlos Nunes


People also ask

What is the difference between a sequence and a collection?

"Collection" and "Sequence" are abstractions, not a property that can be determined from a given value. Collections are bags of values. Sequence is a data structure (subset of collection) that is expected to be accessed in a sequential (linear) manner.

What is a 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.


1 Answers

seq? is a predicate that returns true if it's argument implements ISeq interface, which is to say it provides the methods first,rest,cons. See http://clojure.org/sequences.

(seq? [1 2]) false (seq? (seq [1 2])) true 

sequential? is a predicate that returns true if it's argument implements Sequential interface. Sequential is a marker interface (no methods) and is a promise that the collection can be iterated over in a defined order (e.g. a list, but not a map).

(sequential? []) true (sequential? {}) false 

coll? is a predicate that returns true if its argument implments IPersistentCollection. So for example the clojure data structures would return true, whereas native java data structures would not:

(coll? {:a 1}) true (coll? (java.util.HashMap.)) false 
like image 122
Kevin Avatar answered Sep 22 '22 11:09

Kevin