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 and end,
  • pattern is obligatory,
  • pattern is function, that takes two params index and previousElement, both optional,
  • pattern returns each sequence element,
  • end can be a number, in that case sequence will be lazy but fixed length,
  • end can be a function with two params index, 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

Stats:

CreatedFeb 15, 2016
PublishedFeb 16, 2016
Warriors Trained76
Total Skips0
Total Code Submissions187
Total Times Completed21
JavaScript Completions21
Total Stars3
% of votes with a positive feedback rating94% of 9
Total "Very Satisfied" Votes8
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments9
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • czterystaczwarty Avatar
Ad