Starters
Starter is used to create an iterable. A query must start with a starter operator. This page introduces the builtin functions that create starters.
NOTE: You can also define custom starters see Extensibility for more details.
Empty
Creates a Starter
that returns an empty Iterable
. Can be useful if you need an empty Iterable
with zero memory allocation.
function empty<T>(): Starter<T>
function empty<T>(): Starter<T>
Example
const result = query(empty<number>())
for (const item of result) {
console.log(item)
}
const result = query(empty<number>())
for (const item of result) {
console.log(item)
}
Output
From
Creates a Starter
that returns the Iterable
it receives as an argument. This is the most common query starter, and the main way of transforming the builtin collections into query sources.
function from<T>(source: Iterable<T>): Starter<T>
function from<T>(source: Iterable<T>): Starter<T>
Arguments
source
:Iterable<T>
- Contains the items to return
Example
const result = query(from([1, 2, 3]))
for (const item of result) {
console.log(item)
}
const result = query(from([1, 2, 3]))
for (const item of result) {
console.log(item)
}
Output
1
2
3
1
2
3
Range
Creates a Starter
that generates an Iterable
that contains a range of sequential numbers. It uses a generator to achieve this, so the actual numbers are not materialized in a collection.
NOTE: This operator uses deferred execution. The actual operation will be evaluated each time when the query result is iterated over.
function range(start: number, count: number): Starter<number>
function range(start: number, count: number): Starter<number>
Arguments
start
:number
- The starting value of the generatedIterable
count
:number
- The length of the generatedIterable
Example
const result = query(range(10, 3))
for (const item of result) {
console.log(item)
}
const result = query(range(10, 3))
for (const item of result) {
console.log(item)
}
Output
10
11
12
10
11
12
Repeat
Creates a Starter
that generates an Iterable
that contains a provided item multiple times. It uses a generator to achieve this, so the actual items are not materialized in a collection.
NOTE: This operator uses deferred execution. The actual operation will be evaluated each time when the query result is iterated over.
function repeat<T>(item: T, times: number): Starter<T>
function repeat<T>(item: T, times: number): Starter<T>
Arguments
item
:T
- The item to repeattimes
:number
- The amount of times to repeat the item
Example
const result = query(repeat('hello', 3))
for (const item of result) {
console.log(item)
}
const result = query(repeat('hello', 3))
for (const item of result) {
console.log(item)
}
Output
hello
hello
hello
hello
hello
hello