Skip to content

Sequences ​

seq is a lazy stream: elements are taken one at a time, and only as many as the answer needs. For big ranges, endless generators and line-by-line input. An array is capped at a million elements; a seq is not.

mzs
(1..1_000_000_000).seq.filter { it % 7 == 0 }.take(3).array  # → [7,14,21]
mzs
(1..1_000_000_000).map { it * 2 }
text
collection too large

Sources ​

FormGives
xs.seq / seq(xs)from an array
(a..b).seqfrom a range; nothing is built
seq { (i) -> … }a generator: called with the index, nil ends it
io.linesinput lines one by one
mzs
[1, 2, 3].seq.array  # → [1,2,3]
seq { (i) -> if i < 4 { i * i } }.array  # → [0,1,4,9]
seq { (i) -> i }.take(3).array  # → [0,1,2]

A generator cannot yield nil: that is the end of the stream.

Lazy functions ​

They return a new seq and compute nothing.

FunctionWhat it does
map / filter / rejectas for an array
flat_mapunpacks an array, range or seq one level
take(n) / drop(n)the first n / everything after them
take_while / drop_whilewhile the condition holds
zip(*others)zips, stops at the shortest
concat(*others)one stream after another
uniqwithout repeats
each_slice(n) / each_cons(n)batches / windows of n
mzs
(1..9).seq.map { it * 10 }.take(3).array  # → [10,20,30]
(1..9).seq.take_while { it < 4 }.array  # → [1,2,3]
(1..9).seq.drop(6).array  # → [7,8,9]
(1..3).seq.flat_map { [it, -it] }.array  # → [1,-1,2,-2,3,-3]
(1..5).seq.zip(["a", "b"]).array  # → [[1,"a"],[2,"b"]]
[1, 2].seq.concat(3..4).array  # → [1,2,3,4]
[3, 1, 3, 2, 1].seq.uniq.array  # → [3,1,2]
(1..7).seq.each_slice(3).array  # → [[1,2,3],[4,5,6],[7]]

Terminal functions ​

They pull the stream until they have the answer.

FunctionWhat it does
arraycollects into an array
each { … }iterates
first(n) / last(n)the first / the last
find / any / all / nonestop at the deciding element
len / count / hasnumber / count / whether present (and x in s)
reduce / sum / min / maxfolding
min_by / max_byby a key
join(sep = "")a string
tally / group_bya dict
mzs
(1..4).seq.sum  # → 10
(1..4).seq.reduce { (a, x) -> a * x }  # → 24
(1..9).seq.find { it > 3 }  # → 4
(1..9).seq.first(2)  # → [1,2]
(1..9).seq.last(2)  # → [8,9]
(1..9).seq.count { it.even }  # → 4
(1..3).seq.join("-")  # → "1-2-3"
["a", "b", "a"].seq.tally  # → {"a":2,"b":1}
2 in (1..3).seq  # → true
mzs
for x in (1..3).seq { print(x) }
println()
text
123

Laziness you can see ​

The generator below counts its calls: 8 elements were computed, not a million.

mzs
pulls = 0
s = seq { (i) -> pulls = pulls + 1; i }
s.filter { it % 7 == 0 }.take(2).array  # → [0,7]
pulls  # → 8

A seq is a recipe, not a cursor ​

Every terminal function runs the source again.

mzs
s = (1..3).seq.map { it * 2 }
s.array  # → [2,4,6]
s.array  # → [2,4,6]

What a seq lacks ​

sort, reverse, shuffle, sample need the whole stream: call .array first. A seq has no JSON, and == compares identity.

mzs
(5..1).seq.array.sort  # → []
[3, 1, 2].seq.array.sort  # → [1,2,3]
type((1..3).seq)  # → "seq"
(1..3).seq.is("array")  # → false
mzs
[3, 1, 2].seq.sort
text
sort expects an array, got seq

Line-by-line input ​

io.lines gives the input lines as a seq: a file of any size is read line by line.

mzs
include io
errors = io.lines.filter { it.has("ERROR") }.take(10).array
io.lines.each_slice(500) { (batch) -> println("${batch.len} lines") }

Careful: a closure in a for header ​

In a for header, braces are the loop body. Put a generator in parentheses:

mzs
for x in (seq { (i) -> i }) { break if x > 2; print(x) }
println()
text
012