Hacker News new | ask | show | jobs
by Amnon 5393 days ago
The advantage of "streams" is that it can be used to elegantly define infinite sequences recursively.

For example, you can define powers of two as:

   var powersOf2 = new Stream(1, function() {
     return powersOf2.scale(2);
   });
(untested) which means that the sequence of powers of 2 begins with 1 follows with the the same sequence scaled by 2!

The implementation in the linked article however seems to be flawed, since it doesn't store the node's tail after computing it. This means that to get each element of the sequence you have to compute all the elements before it (even though you already did in order to get to that element).

This works not only for numbers but for other infinite sequences such as expressions in a language. I recommend the book Higher Order Perl if you're interested in this.

1 comments

MJD's Stream module from HOP can be found here: https://metacpan.org/module/HOP::Stream