-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.ts
34 lines (32 loc) · 943 Bytes
/
generators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Generates a sequence of numbers.
* @param from Starting from
* @param to Ends with (excluding this value)
* @param step Step to increment or decrement. 1 by default.
*/
export function* range(from: number, to: number, step: number = 1) {
for (let i = from; i < to; i += step) {
yield i;
}
}
/**
* Generates a sequence of numbers.
* @param from Starting from
* @param to Ends with (including this value)
* @param step Step to increment or decrement. 1 by default.
*/
export function* rangeInclusive(from: number, to: number, step: number = 1) {
for (let i = from; i <= to; i += step) {
yield i;
}
}
/**
* Generates a sequence that yields the same item n-times.
* @param item Any item or value
* @param times Number of times to repeat
*/
export function* repeat<T>(item: T, times: number) {
for (let i = 0; i < times; i++) {
yield item;
}
}