Get n-th

bearophile bearophileHUGS at lycos.com
Tue Feb 22 18:07:46 PST 2011


Do you know a much nicer way to take just the n-th item of a lazy range?


import std.stdio, std.array, std.range;
void main() {
    auto fib = recurrence!("a[n-1] + a[n-2]")(1, 1);
    writeln(array(take(fib, 10)).back);
}


In Python I use next(isslice(x, n, n+1)):

>>> from itertools import islice
>>> r = (x*x for x in xrange(10)) # lazy
>>> next(islice(r, 5, 6))
25

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list