Fibonacci with ranges
Ali Çehreli
acehreli at yahoo.com
Sat Mar 12 02:15:51 PST 2011
On 03/12/2011 01:33 AM, Russel Winder wrote:
> On Fri, 2011-03-11 at 18:46 -0500, Jesse Phillips wrote:
>> Without testing: foreach (f; take(recurrence!("a[n-1] +
a[n-2]")(0UL, 1UL), 50))
>>
>> teo Wrote:
>>
>>> Just curious: How can I get ulong here?
>>>
>>> foreach (f; take(recurrence!("a[n-1] + a[n-2]")(0, 1), 50))
>>> {
>>> writeln(f);
>>> }
>>
>
> Interestingly, or not, the code:
>
> long declarative ( immutable long n ) {
> return take ( recurrence ! ( "a[n-1] + a[n-2]" ) ( 0L , 1L ) , n ) ;
> }
take returns a lazy range which can't be returned as a single long.
Reading your other post, I think this may be what you wanted to see:
import std.range;
import std.algorithm;
auto declarative(immutable long n)
{
return take(recurrence!("a[n-1] + a[n-2]")(0L, 1L), n);
}
void main()
{
long[] data = [ 0, 1, 1, 2, 3, 5, 8 ];
foreach (n; 0 .. data.length) {
assert(equal(declarative(n), data[0..n]));
}
}
Ali
More information about the Digitalmars-d-learn
mailing list