Iter.fromArray
func Iter.fromArray[T](xs: Array[T]): Seq[T]Iter.fromArray returns a sequence of the elements of xs, in order.
Details
- Building the sequence runs nothing. The
Iterfunctions you chain on it, such asIter.map, run only whenIter.toArrayorforruns the result, and only for as many elements as are taken. - The sequence reads
xseach time it runs, not when you build it. It sees elements set or pushed before the run, and elements pushed during the run.
Basic examples
Turn an array into a sequence and back:
Iter.fromArray([1, 2, 3]).toArray() // [1, 2, 3]Chain lazy steps on it:
Iter.fromArray(["ant", "bee", "cat"]).map({ x.toUpper() }).toArray() // ["ANT", "BEE", "CAT"]Take the first elements of an array as a sequence:
Iter.fromArray([5, 6, 7, 8]).take(2).toArray() // [5, 6]Scope
An empty array gives an empty sequence:
let empty: Array[string] = []
Iter.fromArray(empty).toArray() // []The elements may be records:
type Point = { x: int, y: int }
let points = [Point{ x: 1, y: 2 }, Point{ x: 3, y: 4 }]
Iter.fromArray(points).map({ x.x + x.y }).toArray() // [3, 7]null elements pass through like any other:
let marks: Array[int | null] = [90, null, 75]
Iter.fromArray(marks).filter({ x != null }).toArray() // [90, 75]Nested arrays stay arrays; each element of the sequence is one inner array:
Iter.fromArray([[1, 2], [], [3]]).map({ x.length() }).toArray() // [2, 0, 1]Start a pipe with it:
[4, 5, 6] | Iter.fromArray() | Iter.filter({ x % 2 == 0 }) | Iter.toArray() // [4, 6]Applications
Write a function once over Seq[int] and feed it an array or a range:
func total(s: Seq[int]): int {
var sum = 0
for n in s {
sum += n
}
sum
}
println(total(Iter.fromArray([10, 20, 30])))
println(total(Iter.range(1, 5)))60
10Find the first few results of an expensive step without running it on every element. The counter shows map ran on two elements, not five:
var calls = 0
let big = Iter.fromArray([3, 8, 1, 20, 7])
.map({
calls += 1
x * 10
})
.filter({ x > 50 })
.take(1)
.toArray()
println(big)
println(calls)[80]
2Stop a loop over the sequence early with break:
let lines = ["# title", "text", "", "more"]
for line in Iter.fromArray(lines) {
if line == "" {
break
}
println(line)
}# title
textProperties and relations
Iter.toArray undoes it, giving an array equal to xs:
let xs = [3, 1, 2]
assert(Iter.fromArray(xs).toArray() == xs)Running the same sequence twice gives the same elements:
let s = Iter.fromArray(["a", "b"])
assert(s.toArray() == s.toArray())Mapping lazily and collecting gives what Array.map gives eagerly:
let xs = [1, 2, 3]
assert(Iter.fromArray(xs).map({ x * x }).toArray() == xs.map({ x * x }))Possible issues
An array is not a Seq. Passing one where a sequence is expected is a compile error; wrap it with Iter.fromArray:
let xs = [1, 2, 3]
println(Iter.take(xs, 2)) // error: cannot use Array[int] where Seq[T] is expectedAn array's . reaches Array functions, not Iter ones, so xs.fromArray() is a compile error. Call Iter.fromArray(xs) or pipe into it:
let xs = [4, 5, 6]
println(xs.fromArray().toArray()) // error: Array[int] has no field or function fromArrayThe sequence does not copy xs. Changes to the array before the run show up in it:
let xs = [1, 2, 3]
let s = Iter.fromArray(xs)
xs.push(4)
s.toArray() // [1, 2, 3, 4]Elements pushed during the run are visited too, unlike Array.map, which visits only the elements present when it was called:
let ys = [1, 2]
for n in Iter.fromArray(ys) {
if n < 3 {
ys.push(n + 2)
}
println(n)
}1
2
3
4for iterates over an array directly, so a loop needs no Iter.fromArray:
for n in [1, 2] {
println(n)
}1
2.filter on an array is Array.filter, which runs at once and returns an array; on the sequence it is Iter.filter, which runs nothing until the sequence is run:
[1, 2, 3, 4].filter({ x > 2 }) // [3, 4]
Iter.fromArray([1, 2, 3, 4]).filter({ x > 2 }).toArray() // [3, 4]See also
Iter.toArray · Iter.range · Iter.map · Iter.filter · Iter.take · Iter · for
