8 Collections

 

This chapter groups together the functions that can be used consistently across different data structures. Their consistency is expressed by one of the following properties or combination thereof:

  • Their performance is consistent across different collections with little or no appreciable difference.
  • Even if there are performance differences, their use is idiomatic across different data structures.
  • The function gets the work done so quickly that the trade-off of using it against the "wrong" data structure is acceptable.
  • The function has been designed to be polymorphic and to operate on different collections.

The chapter is further divided into additional sub-chapters. We are going to first have a look at the basics functions to create, count or otherwise access a collection in the most general way. Next are the functions designed to be polymorphic. Finally, the last sub-chapter is dedicated to general purpose functions like grouping, sorting, partitioning and others that are very common in every day use.

8.1 Basics

8.1.1 into

function since 1.0

Listing 8.1. → Collection copy, Transducers
(into
  ([])
  ([to])
  ([to from])
  ([to xform from]))

into is a frequently used function in the Clojure standard library. into conjoins all items of a source collection "from" into a destination collection "to":

(into #{} (range 10))
;; #{0 7 1 4 6 3 2 9 5 8} ; #1

(into [:g :x :d] [1 5 9])
;; [:g :x :d 1 9 5]       ; #2

8.1.2 count

8.1.3 nth

8.1.4 empty

8.1.5 every?, not-every?, some and not-any?

8.1.6 empty? and not-empty

8.2 Polymorphic

8.2.1 conj

8.2.2 get