December 25, 2013
On Wednesday, 25 December 2013 at 10:58:53 UTC, bearophile wrote:
> Probably directly addressed by issue 10762:
> [snip]
> But if you rewrite iota() also take in account the following:
> [snip again]
> An important enhancement:
>
> Optional "[]" syntax for std.range.iota too
Wow! That's a lot of stuff needing work. Now I understand why some people were waiting for a better iota :D
Everything seems reasonable, and the "[]" syntax could be a nice addition, too.
Shouldn't be that much difficult.

> (A less important enhancement is issue 11252).
On the other hand, I have honestly no idea whatsoever about how to implement this
>
> Bye,
> bearophile
I will probably start working a bit tomorrow, and I guess I will leave the current float specialization untouched, at least at the beginning. In the mean time, merry Christmas to everyone!
December 27, 2013
On Wed, Dec 25, 2013 at 02:30:45PM +0000, Francesco Cattoglio wrote:
> On Wednesday, 25 December 2013 at 10:58:53 UTC, bearophile wrote:
[...]
> >(A less important enhancement is issue 11252).
> On the other hand, I have honestly no idea whatsoever about how to implement this
[...]

It should only be supported for numeric types (i.e., that support +, *, /). Here's a crude attempt at it:

	struct Iota(S,T,U) {
		S start;
		T end;
		U step;

		... // other stuff here

		bool opBinaryRight(string op)(V val)
			if (op=="in" &&
				is(V.init < S.init) &&
				is((V.init - S.init) % U.init) &&
				is(T.init >= V.init))
		{
			if (val < start || val >= end)
				return false;

			// Surprisingly, x%y actually appears to work
			// like fmod(x,y) in D for floating point types!
			// Not sure how to represent 0 in a generic way
			// though.
			return ((val - start) % step == 0);
		}
	}


T

-- 
What is Matter, what is Mind? Never Mind, it doesn't Matter.
December 28, 2013
On Tuesday, 24 December 2013 at 12:02:54 UTC, Francesco Cattoglio wrote:
> iota(DateTime(2012, 1, 1), DateTime(2013, 1, 1), dur!"days"(5));
> can you easily tell what is the "back" element?

If it can't be defined reasonably for a custom step[1], then simply don't support it when a custom step is provided. There's absolutely no reason for this to compromise the functionality of the common case.

[1] Which I'm not convinced of; e.g. `back` == `DateTime(2013, 1, 1) - dur!"days"(5)`.
December 28, 2013
On 28/12/13 09:06, Jakob Ovrum wrote:
> [1] Which I'm not convinced of; e.g. `back` == `DateTime(2013, 1, 1) -
> dur!"days"(5)`.

Are you sure that's going to work if the iota covers a leap year? :-)

December 28, 2013
On Saturday, 28 December 2013 at 12:16:32 UTC, Joseph Rushton Wakeling wrote:
> On 28/12/13 09:06, Jakob Ovrum wrote:
>> [1] Which I'm not convinced of; e.g. `back` == `DateTime(2013, 1, 1) -
>> dur!"days"(5)`.
>
> Are you sure that's going to work if the iota covers a leap year? :-)

And that's exactly the reason I choose 2012 as an example :D
December 28, 2013
On Tuesday, 24 December 2013 at 15:41:06 UTC, H. S. Teoh wrote:
> This code is wrong for iota(1.0, 9.5), because .back must be of the form
> start + n*step for some integer n, but in this case end is not an
> integral multiple of step away from start. (It's not only wrong for
> .back, it also won't terminate because start==end will never hold.)

It is a simplified example. It has no pretence of handling floating point.
December 28, 2013
On Saturday, 28 December 2013 at 12:16:32 UTC, Joseph Rushton Wakeling wrote:
> Are you sure that's going to work if the iota covers a leap year? :-)

How would it fail? std.datetime does a good job with leap years AFAIK.
December 28, 2013
On 28/12/13 14:58, Jakob Ovrum wrote:
> How would it fail? std.datetime does a good job with leap years AFAIK.

Try the attached code.  The largest value of DateTime(2012, 1, 1) + dur!"days"(5 * n) that is less than DateTime(2013, 1, 1) is _not_ DateTime(2013, 1, 1) - dur!"days"(5). :-)


December 28, 2013
On 28/12/13 14:16, Francesco Cattoglio wrote:
> And that's exactly the reason I choose 2012 as an example :D

It's going to get fun if we have to start taking into account leap seconds too ;-)

On a serious note -- I wouldn't worry about what kind of range type you generate with iota.  It'll be fine to implement iota for a new type starting with an input (or more likely, forward) range, and add random-access functionality later as and where it's possible.
December 28, 2013
On Saturday, 28 December 2013 at 13:58:40 UTC, Jakob Ovrum wrote:
> On Saturday, 28 December 2013 at 12:16:32 UTC, Joseph Rushton Wakeling wrote:
>> Are you sure that's going to work if the iota covers a leap year? :-)
>
> How would it fail? std.datetime does a good job with leap years AFAIK.

Yes it does, the catch is:
2012, Jan, 1 + "365 days") = 2012, Dec, 31
because 2012 is a leap year. If you compute "back = end - duration", you are asserting the user will always enter correct bounds. This is fine for trivial intervals, but iota won't be usable anywhere else.
It's always the same "issue": you have to compute the last element inside iota, you should never rely on the user giving you ideal inputs.