|
Boost : |
Subject: Re: [boost] Lazy list
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2009-01-19 16:02:10
Zachary Turner wrote:
> I can imagine the code for this might be messy. Imagine if, using
> boost labmda expressions, I could write:
>
> lazylist<int> fib;
> fib.push_back(1);
> fib.push_back(1);
> fib.push_back(_1->prev + _1->prev->prev);
>
> foreach(fib.begin(), fib.iter(100), cout << _1); //print the first 100
> fibonacci numbers
This cannot be very efficient, nor very safe.
As I said earlier,
foreach(int i, make_range(1, 1) | append_rec(_1 + _2) | limit(100))
std::cout << i << std::endl;
seems like a much better design to me. Actually, I've done that for some
time already.
This is obviously a Boost.RangeEx extension.
append_rec() takes an N-ary function object and returns an adaptor that
takes a range of at least N elements and returns an infinite range that
applies the function object recursively.
This can check at compile-time that the range has enough elements, for
example. Also, it can reuse any other existing range, including one
defined like this.
No need for caching: the adaptor can memorize the previous N elements of
a given position.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk