Common Generator functions
Description:
One very useful feature of JavaScript (and thus derived languages like TypeScript) are generator functions. They allow you to easily create an iterable object for arbitrary purposes.
Unlike arrays these generators don't have to contain their entire contents, which makes them suitable for handling infinite sequences, or allows saving on memory if your particular usecase does not have to care about the entirety of some sequence of things.
In this Kata we will implement a few generic generator functions for a couple of tasks that crop up when dealing with lists and streams of data.
1: range(start, stop, step)
start
, stop
and step
are numbers.
Count upwards from start
until stop
, with the given step
.
step
can be assumed to always be positive.step
is an optional argument; If it is not specified, behave as if it is 1.stop
is an optional argument; If it is not specified, count upwards forever.start
is an optional argument; If it is not specified, count starting from 0.
2: generate(start, iterate, continues)
start
can be anything.iterate
is a function, that returns the next element of the sequence if given the current element of the sequence.continues
is a function, that returns false if the given returned value ofiterate
is past the end of the sequence.
Generate a sequence of arbitrary values using the provided starting point and iteration function.
E.g. generate(1, i => i*2, i => i<10)
results in the sequence [1, 2, 4, 8]
continues
is an optional argument; If it is not specified, the sequence is endless.- if
continues(start)
is false, the generated sequence is empty. - be prepared to handle
null
orundefined
amongst the generated values
3: delta(sequence, deltaFunction, start)
sequence
is an iterable of arbitrary values.delta
is a function accepting a pair of values fromsequence
.start
is a value to be passed todelta
as the element before the first element ofsequence
.
Generate a sequence of the return values of delta
when called for every element of sequence
and their precursor element.
E.g. delta([1, 3, 2], (a, b) => b-a, 0)
results in the sequence [1, 2, -1]
sequence
will never containundefined
(but may containnull
)start
is an optional argument; If it is not specified, the first element of the resulting sequence will bedelta(sequence[0], sequence[1])
. If start is specified its value will never beundefined
.
4: zip(sequence1, sequence2)
sequence1
and sequence2
are iterables of arbitrary values.
Generate a sequence of arrays containing the elements of the given sequences.
Specifically zip(sequence1, sequence2)[x] = [sequence1[x], sequence2[x]]
.
If any of the given sequences ends, the sequence you are generating ends as well.
In particular this means that if any input sequence has no elements, the resulting sequence has no elements either.
Similar Kata:
Stats:
Created | Sep 6, 2023 |
Published | Sep 7, 2023 |
Warriors Trained | 177 |
Total Skips | 16 |
Total Code Submissions | 263 |
Total Times Completed | 47 |
TypeScript Completions | 22 |
JavaScript Completions | 32 |
Total Stars | 8 |
% of votes with a positive feedback rating | 88% of 24 |
Total "Very Satisfied" Votes | 18 |
Total "Somewhat Satisfied" Votes | 6 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 10 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |