0
# Sequence Operations
1
2
Comprehensive collection processing functions for transforming, filtering, and manipulating sequences with lazy evaluation. Sequences are the fundamental abstraction in Clojure for working with collections.
3
4
## Capabilities
5
6
### Core Transformation Functions
7
8
Functions that transform sequences by applying operations to each element.
9
10
```clojure { .api }
11
(map f coll)
12
(map f c1 c2 & colls)
13
;; Applies function f to each element of coll(s)
14
;; With multiple collections, applies f to first of each, then second of each, etc.
15
;; Returns lazy sequence
16
;; Returns: clojure.lang.LazySeq
17
18
(mapv f coll)
19
(mapv f c1 c2 & colls)
20
;; Like map but returns a vector instead of lazy seq
21
;; Returns: clojure.lang.IPersistentVector
22
23
(map-indexed f coll)
24
;; Like map but f takes (index, item) as arguments
25
;; Returns lazy sequence
26
;; Returns: clojure.lang.LazySeq
27
28
(mapcat f & colls)
29
;; Equivalent to (apply concat (map f colls))
30
;; Returns lazy sequence
31
;; Returns: clojure.lang.LazySeq
32
33
(for [bindings*] expr)
34
;; List comprehension with optional :let, :when, :while clauses
35
;; bindings: [name coll :let [name expr] :when test :while test]
36
;; Returns lazy sequence
37
;; Returns: clojure.lang.LazySeq
38
```
39
40
### Filtering Functions
41
42
Functions that select subsets of sequences based on predicates.
43
44
```clojure { .api }
45
(filter pred coll)
46
;; Returns lazy sequence of items for which pred returns true
47
;; Returns: clojure.lang.LazySeq
48
49
(filterv pred coll)
50
;; Like filter but returns vector
51
;; Returns: clojure.lang.IPersistentVector
52
53
(remove pred coll)
54
;; Returns lazy sequence of items for which pred returns false/nil
55
;; Returns: clojure.lang.LazySeq
56
57
(keep f coll)
58
;; Returns lazy sequence of non-nil results of (f item)
59
;; Returns: clojure.lang.LazySeq
60
61
(keep-indexed f coll)
62
;; Like keep but f takes (index, item) as arguments
63
;; Returns: clojure.lang.LazySeq
64
65
(distinct coll)
66
;; Returns lazy sequence of distinct elements
67
;; Returns: clojure.lang.LazySeq
68
69
(dedupe coll)
70
;; Returns lazy sequence with consecutive duplicates removed
71
;; Returns: clojure.lang.LazySeq
72
```
73
74
### Reduction Functions
75
76
Functions that combine sequence elements into single values.
77
78
```clojure { .api }
79
(reduce f coll)
80
(reduce f init coll)
81
;; Applies f to pairs: (f (f init first) second) ...
82
;; If no init provided, uses first element
83
;; Returns: Object
84
85
(reductions f coll)
86
(reductions f init coll)
87
;; Returns lazy seq of intermediate reduce values
88
;; Returns: clojure.lang.LazySeq
89
90
(apply f args)
91
(apply f x args)
92
(apply f x y args)
93
(apply f x y z args)
94
(apply f a b c d & args)
95
;; Applies function f to argument list
96
;; Last argument must be a sequence
97
;; Returns: Object
98
```
99
100
### Sequence Manipulation
101
102
Functions for taking, dropping, and rearranging sequence elements.
103
104
```clojure { .api }
105
(take n coll)
106
;; Returns lazy sequence of first n items
107
;; Returns: clojure.lang.LazySeq
108
109
(take-while pred coll)
110
;; Returns lazy sequence of successive items while pred is true
111
;; Returns: clojure.lang.LazySeq
112
113
(take-last n coll)
114
;; Returns seq of last n items
115
;; Returns: clojure.lang.ISeq
116
117
(drop n coll)
118
;; Returns lazy sequence without first n items
119
;; Returns: clojure.lang.LazySeq
120
121
(drop-while pred coll)
122
;; Returns lazy sequence without successive items while pred is true
123
;; Returns: clojure.lang.LazySeq
124
125
(drop-last n? coll)
126
;; Returns lazy sequence without last n items (default n=1)
127
;; Returns: clojure.lang.LazySeq
128
129
(reverse coll)
130
;; Returns seq with elements in reverse order
131
;; Returns: clojure.lang.ISeq
132
133
(sort coll)
134
(sort comp coll)
135
;; Returns sorted sequence, optionally with comparator
136
;; Returns: clojure.lang.ISeq
137
138
(sort-by keyfn coll)
139
(sort-by keyfn comp coll)
140
;; Returns sequence sorted by keyfn results
141
;; Returns: clojure.lang.ISeq
142
143
(shuffle coll)
144
;; Returns vector with items in random order
145
;; Returns: clojure.lang.IPersistentVector
146
```
147
148
### Sequence Combination
149
150
Functions for combining multiple sequences.
151
152
```clojure { .api }
153
(concat & xs)
154
;; Returns lazy concatenation of sequences
155
;; Returns: clojure.lang.LazySeq
156
157
(interleave c1 c2 & colls)
158
;; Returns lazy seq of first items, then second items, etc.
159
;; Returns: clojure.lang.LazySeq
160
161
(interpose sep coll)
162
;; Returns lazy seq with sep between each item
163
;; Returns: clojure.lang.LazySeq
164
165
(flatten x)
166
;; Takes nested structure and returns flattened seq
167
;; Returns: clojure.lang.ISeq
168
```
169
170
### Partitioning Functions
171
172
Functions for splitting sequences into subsequences.
173
174
```clojure { .api }
175
(partition n coll)
176
(partition n step coll)
177
(partition n step pad coll)
178
;; Returns lazy sequence of lists of n items each
179
;; step defaults to n, pad provides items for incomplete partition
180
;; Returns: clojure.lang.LazySeq
181
182
(partition-all n coll)
183
(partition-all n step coll)
184
;; Like partition but includes incomplete final partition
185
;; Returns: clojure.lang.LazySeq
186
187
(partition-by f coll)
188
;; Returns lazy sequence of lists split when (f item) changes
189
;; Returns: clojure.lang.LazySeq
190
191
(split-at n coll)
192
;; Returns vector of [(take n coll) (drop n coll)]
193
;; Returns: clojure.lang.IPersistentVector
194
195
(split-with pred coll)
196
;; Returns vector of [(take-while pred coll) (drop-while pred coll)]
197
;; Returns: clojure.lang.IPersistentVector
198
```
199
200
### Grouping and Indexing
201
202
Functions for organizing sequences by keys or properties.
203
204
```clojure { .api }
205
(group-by f coll)
206
;; Returns map of (f item) keys to vectors of items with that key
207
;; Returns: clojure.lang.IPersistentMap
208
209
(frequencies coll)
210
;; Returns map of items to their occurrence counts
211
;; Returns: clojure.lang.IPersistentMap
212
213
(zipmap keys vals)
214
;; Returns map with keys mapped to corresponding vals
215
;; Returns: clojure.lang.IPersistentMap
216
```
217
218
### Search and Predicates
219
220
Functions for finding elements and testing sequence properties.
221
222
```clojure { .api }
223
(some pred coll)
224
;; Returns first truthy result of (pred item), nil if none
225
;; Returns: Object or nil
226
227
(every? pred coll)
228
;; Returns true if pred is true for every item
229
;; Returns: boolean
230
231
(not-every? pred coll)
232
;; Returns false if pred is true for every item
233
;; Returns: boolean
234
235
(not-any? pred coll)
236
;; Returns true if pred is false for every item
237
;; Returns: boolean
238
239
(nth coll index)
240
(nth coll index not-found)
241
;; Returns item at index, or not-found if out of bounds
242
;; Returns: Object
243
```
244
245
**Usage Examples:**
246
247
```clojure
248
;; Transformation
249
(map inc [1 2 3 4]) ; => (2 3 4 5)
250
(map + [1 2 3] [4 5 6]) ; => (5 7 9)
251
(map-indexed vector [a b c]) ; => ([0 a] [1 b] [2 c])
252
253
;; Filtering
254
(filter even? [1 2 3 4 5 6]) ; => (2 4 6)
255
(remove even? [1 2 3 4 5 6]) ; => (1 3 5)
256
(distinct [1 2 2 3 3 3]) ; => (1 2 3)
257
258
;; Reduction
259
(reduce + [1 2 3 4]) ; => 10
260
(reduce conj [] [1 2 3]) ; => [1 2 3]
261
262
;; Taking and dropping
263
(take 3 [1 2 3 4 5]) ; => (1 2 3)
264
(drop 2 [1 2 3 4 5]) ; => (3 4 5)
265
(take-while pos? [1 2 -1 3]) ; => (1 2)
266
267
;; Partitioning
268
(partition 2 [1 2 3 4 5 6]) ; => ((1 2) (3 4) (5 6))
269
(partition-by even? [1 3 2 4 5 7]) ; => ((1 3) (2 4) (5 7))
270
271
;; Grouping
272
(group-by even? [1 2 3 4 5 6]) ; => {false [1 3 5], true [2 4 6]}
273
(frequencies [:a :b :a :c :b :a]) ; => {:a 3, :b 2, :c 1}
274
275
;; List comprehension
276
(for [x (range 5)
277
y (range 5)
278
:when (< x y)]
279
[x y])
280
; => ([0 1] [0 2] [0 3] [0 4] [1 2] [1 3] [1 4] [2 3] [2 4] [3 4])
281
```