Iter.take
func Iter.take[T](s: Seq[T], n: int): Seq[T]Iter.take returns a sequence of at most the first n elements of s. It panics if n < 0.
Details
Iter.takeis lazy: it returns a new sequence without runnings. The source runs when aforloop or Iter.toArray runs the result.- The result stops
sas soon as it has yieldednelements, so an Iter.map before it calls its function onlyntimes. Withnof0it does not runsat all. - When
shas fewer thannelements, the result has all of them. - A negative
npanics whenIter.takeis called, before the result runs. - The result is finite once
shas yieldednelements, even whensnever ends, soIter.takebounds an endless sequence.
Basic examples
Keep the first three numbers of a range:
Iter.range(0, 10) | Iter.take(3) | Iter.toArray() // [0, 1, 2]Loop over the first two elements of an array:
for name in Iter.take(Iter.fromArray(["Ann", "Bob", "Cy"]), 2) {
println(name)
}Ann
BobScope
A count past the end gives every element; a count of 0 gives none:
Iter.range(0, 2) | Iter.take(5) | Iter.toArray() // [0, 1]
Iter.range(0, 2) | Iter.take(0) | Iter.toArray() // []
Iter.range(5, 5) | Iter.take(3) | Iter.toArray() // []Records and null elements pass through unchanged:
type Task = { title: string, done: bool }
let tasks = [Task{ title: "write", done: true }, Task{ title: "test", done: false }]
Iter.fromArray(tasks) | Iter.take(1) | Iter.toArray() // [Task{ title: "write", done: true }]
Iter.fromArray([null, 2, null]) | Iter.take(2) | Iter.toArray() // [null, 2]Call it through .:
Iter.fromArray(["a", "b", "c"]).take(2).toArray() // ["a", "b"]Applications
Find the first five multiples of 7 without deciding how far to search. Iter.take stops the endless source once it has five:
func from(start: int): Seq[int] {
func(yield: func(int): bool) {
var i = start
while true {
if !yield(i) { return }
i += 1
}
}
}
println(from(1) | Iter.filter({ x % 7 == 0 }) | Iter.take(5) | Iter.toArray())[7, 14, 21, 28, 35]Print the first six Fibonacci numbers, one per line:
func fibonacci(): Seq[int] {
func(yield: func(int): bool) {
var a = 0
var b = 1
while true {
if !yield(a) { return }
let next = a + b
a = b
b = next
}
}
}
for n in fibonacci() | Iter.take(6) {
println(n)
}0
1
1
2
3
5Read only the first values sent on a channel with Channel.values:
let c: Channel[string] = Channel.new(capacity: 4)
for word in ["one", "two", "three", "four"] {
c.send(word)
}
c.close()
for word in Channel.values(c) | Iter.take(2) {
println(word)
}one
twoProperties and relations
Taking n elements gives the same array as slicing the first n, when there are at least n:
let xs = [4, 8, 15, 16, 23, 42]
assert((Iter.fromArray(xs) | Iter.take(3) | Iter.toArray()) == Array.slice(xs, 0, 3))The result never has more than n elements, nor more than s has:
for n in Iter.range(0, 6) {
let got = Iter.range(0, 3) | Iter.take(n) | Iter.toArray()
assert(len(got) == Math.min(n, 3))
}The result stops the source after n elements, so a mapping function before it runs n times, and not at all for a count of 0:
var calls = 0
let squares = Iter.range(0, 1000000) | Iter.map({ calls += 1; x * x })
squares | Iter.take(3) | Iter.toArray() // [0, 1, 4]
calls // 3
squares | Iter.take(0) | Iter.toArray() // []
calls // 3Taking twice keeps the smaller count:
let s = Iter.range(0, 10)
assert((s | Iter.take(5) | Iter.take(2) | Iter.toArray()) == (s | Iter.take(2) | Iter.toArray()))Possible issues
A negative count panics at the call, even when the result never runs:
let none = Iter.take(Iter.range(0, 5), -1) // panic: Iter.take needs a count of at least 0, got -1Iter.take returns a sequence, not an array. Run it with Iter.toArray to get one:
let first: Array[int] = Iter.range(0, 5) | Iter.take(2)
// error: cannot use Seq[int] where Array[int] is expectedIter.take ends an endless source only once n elements reach it. A filter before it that matches fewer than n elements never ends:
func from(start: int): Seq[int] {
func(yield: func(int): bool) {
var i = start
while true {
if !yield(i) { return }
i += 1
}
}
}
println(from(1) | Iter.filter({ x < 0 }) | Iter.take(1) | Iter.toArray())See also
Iter.range, Iter.fromArray, Iter.filter, Iter.map, Iter.toArray, Array.slice, Channel.values
