Beta
Lazy sequence
Description:
Implement lazySeq
function that generates lazy sequence.
Otherwise to Static sequences every element should be evaluted only when needed.
Spec:
- function takes two parameters:
pattern
andend
, pattern
is obligatory,pattern
is function, that takes two paramsindex
andpreviousElement
, both optional,pattern
returns each sequence element,end
can be anumber
, in that case sequence will be lazy but fixed length,end
can be afunction
with two paramsindex
,element
(both optional),end
function is predicate (returns boolean),- in that case sequence will finish on first element, for which returns
true
(itself NOT included, only describes the end),
- if
end
is not specified, sequence should be infinite, - sequence sould be iterable by
for...of
loop,
Examples:
let seq1 = lazySeq((index, prev) => index, 10);
for (let i of seq1) {
console.log(i); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
// every given element is index increased by 1,
// elements will return until `element` is not more than 5,
let seq2 = lazySeq((index, prev) => index+1, (index, element) => element > 5);
for (let i of seq2) {
console.log(i); // 1, 2, 3, 4, 5
}
// elements starts from 100 and each next is 0.5 less
let seq3 = lazySeq((index, prev) => (prev ? prev : 100) - 0.5 );
let iterator = seq3[Symbol.iterator]();
for (let i=0; i<9; i++) {
iterator.next();
}
condole.log(iterator.next()); // {value: 95, done: false}
Reading:
Fundamentals
Functional Programming
Lists
Similar Kata:
Stats:
Created | Feb 15, 2016 |
Published | Feb 16, 2016 |
Warriors Trained | 76 |
Total Skips | 0 |
Total Code Submissions | 187 |
Total Times Completed | 21 |
JavaScript Completions | 21 |
Total Stars | 3 |
% of votes with a positive feedback rating | 94% of 9 |
Total "Very Satisfied" Votes | 8 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 9 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |