Issue 10762 - std.range.iota should support any type that has ordered comparisons, incrementing, and addition
Summary: std.range.iota should support any type that has ordered comparisons, incremen...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2013-08-05 12:01 UTC by hsteoh
Modified: 2017-08-31 11:48 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description hsteoh 2013-08-05 12:01:50 UTC
For example, iota should work with Date:

    auto r = iota(Date(2013,1,1), Date(2014,1,1), dur!"days"(1));

Currently, this is not supported.
Comment 1 hsteoh 2013-08-05 12:04:55 UTC
Basically, any type that comparable by "<", supports "++" or addition with the increment type, should work with iota.
Comment 2 monarchdodra 2013-08-05 12:20:56 UTC
Related:
[Issue 6447] New: iota(BigInt) too
http://d.puremagic.com/issues/show_bug.cgi?id=6447

AFAIK, the only difficulty with making this work is that the return type is currently "CommonType!(Beg, End, Step)".

Whereas it should be more like something along the lines of:
typeof(CommonType!(Beg, End).init += Step.init);

But nothing too difficult to integrate anyways.
Comment 3 bearophile_hugs 2013-08-05 12:34:51 UTC
Elsewhere there was a discussion for:

iota!"[]"('a', 'z')
iota!"[]"(ubyte.min, ubyte.max)


Should enums too be supported?

enum MyE { A=1, B=15, C=6, D=9, E=22, F=3 }

iota(MyE.B, MyE.E)
Comment 4 hsteoh 2013-08-05 13:41:39 UTC
There are other considerations as well.

Let's say we are calling iota(start,end,inc).
Let S=typeof(start),
    E=typeof(end),
    I=typeof(inc),
    C=CommonType!(S,E).

Then:

1) Ideally, as long as C.init<end is a valid expression, and start+inc is convertible to type C, then iota(start,end,inc) should be supported.

2) Should we optimize iota(start,end,inc) in the case that I supports integer multiplication? That is to say, if n is an integer, and n*inc (or inc*n) is a type that can be added to values of type C, then we could potentially return a random access range wherein opIndex returns start + i*inc as long as the result < end (we can throw a RangeError if result !< end). This might be a nice optimization for things like BigInt in some cases.

3) If start+inc is *not* supported, but start++ is, then should we support iota(start,end)?

4) If start+inc is *not* supported but start-- is, and end < start, should we support iota(start,end)?
Comment 5 monarchdodra 2013-08-06 03:41:22 UTC
(In reply to comment #4)
> 2) Should we optimize iota(start,end,inc) in the case that I supports integer
> multiplication? That is to say, if n is an integer, and n*inc (or inc*n) is a
> type that can be added to values of type C, then we could potentially return a
> random access range wherein opIndex returns start + i*inc as long as the result
> < end (we can throw a RangeError if result !< end). This might be a nice
> optimization for things like BigInt in some cases.

I think providing RA is possible, but at the same time, I doubt there is much usecase for it. iota's prime usecase is iteration I mean. If we can make it happen, then great I guess, but I don't really see it as essential.

> 3) If start+inc is *not* supported, but start++ is, then should we support
> iota(start,end)?
> 
> 4) If start+inc is *not* supported but start-- is, and end < start, should we
> support iota(start,end)?

I think that for "iota(end)/iota(start, end)", the constraint should be that "++"/"--" works. For "iota(start, end, step)", then the constraint should be "+=".

So I to answer your question, I think that "Yes", we should support iota(start, end) as soon as ++/-- is available.
Comment 7 hsteoh 2015-01-22 18:21:59 UTC
Related: https://issues.dlang.org/show_bug.cgi?id=6447
Comment 8 github-bugzilla 2015-01-26 09:27:06 UTC
Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/b159a5bdc980abb90833b32fa04044a002dfc794
Merge pull request #2895 from quickfur/iota_bigint

Issue 6447 & 10762: support user-defined types in iota()
Comment 9 github-bugzilla 2015-02-18 03:41:32 UTC
Commit pushed to 2.067 at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/b159a5bdc980abb90833b32fa04044a002dfc794
Merge pull request #2895 from quickfur/iota_bigint