Combining infinite ranges
Philippe Sigaud
philippe.sigaud at gmail.com
Wed Jun 2 13:18:59 PDT 2010
On Wed, Jun 2, 2010 at 21:41, Andrei Alexandrescu <
SeeWebsiteForEmail at erdani.org> wrote:
> On 06/02/2010 02:29 PM, Philippe Sigaud wrote:
>
>>
>>
>> On Wed, Jun 2, 2010 at 19:57, bearophile <bearophileHUGS at lycos.com
>> <mailto:bearophileHUGS at lycos.com>> wrote:
>>
>> Philippe Sigaud:
>> > What, do you also need the no-arg version of iota?
>> >
>> > :-p
>>
>> I'd like a generator (range) similar to the Python itertools.count,
>> that yields numbers starting from the given number (defaulting to
>> zero) and just goes on and on. You can use it in many situations:
>> http://docs.python.org/library/itertools.html#itertools.count
>>
>>
>> Yes, it's handy. It's one of the first range I made, a year ago.
>>
>
> iota(n, n.max) is close. Well, it's not infinite, but cycle(iota(n, n.max))
> is. Probably a version using BigInt would be most sensible.
>
As it's mostly used to enumerate/index ranges or generate some simple
numbers, iota(n,n.max) is largely good enough, except, as you said it's not
infinite. I never used iota much, it regularly crashed on me in the
beginning. I can't remember why.
I don't know if your suggestion of BigInt is a joke or not, but the truth
is, I have a version using any numerical type, I called it numberz. numbers
is the standard version, numberz!T the templated. It uses T for indexing
also (it's a random-access range), which allowed me to do
auto r = numberz(BigInt("1000000000"), BigInt("10000000000"));
auto n = r[BigInt("3000000000")];
Hmm, this doesn't work:
auto i = iota(BigInt("0"),BigInt("10"), BigInt("1"));
But honestly, I just used it to learn ranges and D, and never had a real use
for it. It's just the kind of things that's useful to try, to break out of
some preconceptions (using a size_t to index..., 1u as a step).
As you said, it already takes loooots of iterations to exhaust a long.
Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100602/29d2d428/attachment-0001.html>
More information about the Digitalmars-d
mailing list