1

currently I am still doing this question :

Make a general purpose infinite sequence initialisation function that creates infinite lazy sequences. It will take as parameter a function to compute the next value from the current value. In other words, it should be a “factory” for functions like naturalNumbers. Thus, if we call our function initSequence, then initSequence(n=>n+1) will return a function equivalent to naturalNumbers.

this template is given

    interface LazySequence<T> {
    value: T;
    next(): LazySequence<T>;
}

// Implement the function:
function initSequence<T>(transform: (value: T) => T): (initialValue: T) => LazySequence<T>
{
    // Your code here 
}

So far, this is my code in TypeScript

interface LazySequence<T> {
    value: T;
    next(): LazySequence<T>;
}

// Implement the function:
function initSequence<T>(transform: (value: T) => T): (initialValue: T) => LazySequence<T>
{
    // Your code here ...
    return () => initSequence(v=>v+1) => LazySequence;
}

and it seemed like the code doesn't work accordingly. can anybody help? then can anybody make a clear explanation regarding lazy evaluation, lazy iteration and another lazy things? thanks a lot

  • 2
    Is there a reason you aren't using an existing tool for this, like generators (built into the language) or observables (via a library like Rx)? – ssube Aug 20 '17 at 15:09
  • 1
    Your code doesn't even seem to typecheck. Or do I miss something? – Bergi Aug 20 '17 at 15:14
  • 1
    It seems like a 'homework'. If that is the case, please refer https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – Sayan Pal Aug 20 '17 at 15:15

1 Answers1

2

Not sure whether you need to use the given interface or not, but it seems like you need generator function. Read about this here.

An example can be as follows:

// A generic infinite sequence generator function.
function* infiniteSequence<T>(initialValue: T, transform: (value: T) => T): IterableIterator<T> {
    while (true) {
        yield initialValue;
        initialValue = transform(initialValue);
    }
}

// create different sequences using the generic sequence generator function.

let intSequence = infiniteSequence(1, n => n + 1); // sequence of natural numbers
console.log(intSequence.next().value); // 1
console.log(intSequence.next().value); // 2
console.log(intSequence.next().value); // 3 

let oddSequence = infiniteSequence(1, n => n + 2); // sequence of odd numbers
console.log(oddSequence.next().value); // 1
console.log(oddSequence.next().value); // 3
console.log(oddSequence.next().value); // 5

let evenSequence = infiniteSequence(0, n => n + 2); // sequence of even numbers
console.log(evenSequence.next().value); // 0
console.log(evenSequence.next().value); // 2 
console.log(evenSequence.next().value); // 4

Update based on OP's comment:

As this question seems like a homework, I will just try to provide some useful hints.

  1. Think about the return type of initSequence which is (initialValue: T) => LazySequence<T>, but you are returning () => initSequence(v=>v+1) => LazySequence, which does not match the required return type. In other words you need to return a function which takes a value of type T returns a object of type LazySequence<T>. Read about arrow functions in here and here.

  2. Think about how you can construct an object of an interface. Hint: Duck Typing. Read about this here.

  3. The last bit of this problem is to construct the next method and value property. Note that next again returns a object of type LazySequence<T>, where value should be the next value in the sequence(this is the part I am assuming). Note that initSequence(...)(...) can return such an object.

Have fun :)

Hope this helps.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Sayan Pal
  • 4,768
  • 5
  • 43
  • 82