## digitalmars.D.learn - Get n-th

• bearophile (10/13) Feb 22 2011 Do you know a much nicer way to take just the n-th item of a lazy range?
• Jonathan M Davis (11/29) Feb 22 2011 Assuming that it's a forward range rather than an input range:
• bearophile (13/18) Feb 23 2011 This program gives:
• Jonathan M Davis (6/28) Feb 23 2011 Okay, so you need to do popFrontN(s, n - 1). I'm too used to arrays whic...
bearophile <bearophileHUGS lycos.com> writes:
```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
```
Feb 22 2011
Jonathan M Davis <jmdavisProg gmx.com> writes:
```On Tuesday 22 February 2011 18:07:46 bearophile wrote:
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

Assuming that it's a forward range rather than an input range:

auto s = range.save;
s.popFrontN(n - 1);
writeln(s.front);

The problem is that you have to process a lazy range before you can get at any
of its elements, and once you've processed an element, it's no longer in the
range. So, you pretty much have to save the range and operate on a copy of it.
At that point, you can remove the elements prior to the one you care about and
then take the one you care about from the front of the range.

- Jonathan M Davis
```
Feb 22 2011
bearophile <bearophileHUGS lycos.com> writes:
```Jonathan M Davis:

Assuming that it's a forward range rather than an input range:

auto s = range.save;
s.popFrontN(n - 1);
writeln(s.front);

This program gives:
test.d(5): Error: no property 'popFrontN' for type 'Recurrence!(fun,int,2u)'

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

Thank you,
bye,
bearophile
```
Feb 23 2011
Jonathan M Davis <jmdavisProg gmx.com> writes:
```On Wednesday 23 February 2011 04:34:28 bearophile wrote:
Jonathan M Davis:
Assuming that it's a forward range rather than an input range:

auto s = range.save;
s.popFrontN(n - 1);
writeln(s.front);

This program gives:
test.d(5): Error: no property 'popFrontN' for type
'Recurrence!(fun,int,2u)'

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

Thank you,
bye,
bearophile

Okay, so you need to do popFrontN(s, n - 1). I'm too used to arrays which allow
you to use that sort of syntax. That and not bothering with [] when slicing
seem
to be the two biggest problems that I'm seeing when dealing with ranges which
aren't arrays. I'm just too used to arrays.

- Jonathan M Davis
```
Feb 23 2011
bearophile <bearophileHUGS lycos.com> writes:
```Jonathan M Davis:

Okay, so you need to do popFrontN(s, n - 1).

Right, silly me :-) I need to read error messages.

I'm too used to arrays which allow you to use that sort of syntax.

Me too.

Thank you,
bye,
bearophile
```
Feb 23 2011