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.
(1..1_000_000_000).seq.filter { it % 7 == 0 }.take(3).array # → [7,14,21](1..1_000_000_000).map { it * 2 }collection too largeSources
| Form | Gives |
|---|---|
xs.seq / seq(xs) | from an array |
(a..b).seq | from a range; nothing is built |
seq { (i) -> … } | a generator: called with the index, nil ends it |
io.lines | input lines one by one |
[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.
| Function | What it does |
|---|---|
map / filter / reject | as for an array |
flat_map | unpacks an array, range or seq one level |
take(n) / drop(n) | the first n / everything after them |
take_while / drop_while | while the condition holds |
zip(*others) | zips, stops at the shortest |
concat(*others) | one stream after another |
uniq | without repeats |
each_slice(n) / each_cons(n) | batches / windows of n |
(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.
| Function | What it does |
|---|---|
array | collects into an array |
each { … } | iterates |
first(n) / last(n) | the first / the last |
find / any / all / none | stop at the deciding element |
len / count / has | number / count / whether present (and x in s) |
reduce / sum / min / max | folding |
min_by / max_by | by a key |
join(sep = "") | a string |
tally / group_by | a dict |
(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 # → truefor x in (1..3).seq { print(x) }
println()123Laziness you can see
The generator below counts its calls: 8 elements were computed, not a million.
pulls = 0
s = seq { (i) -> pulls = pulls + 1; i }
s.filter { it % 7 == 0 }.take(2).array # → [0,7]
pulls # → 8A seq is a recipe, not a cursor
Every terminal function runs the source again.
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.
(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[3, 1, 2].seq.sortsort expects an array, got seqLine-by-line input
io.lines gives the input lines as a seq: a file of any size is read line by line.
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:
for x in (seq { (i) -> i }) { break if x > 2; print(x) }
println()012