between and among: worth Phobosization?

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Dec 17 10:35:18 PST 2013


On Tue, Dec 17, 2013 at 10:13:43AM -0800, Andrei Alexandrescu wrote:
> On 12/17/13 8:26 AM, H. S. Teoh wrote:
> >Maybe this should be a community effort. Let each of us come up with
> >a new iota, and we can compare and incorporate each other's ideas to
> >produce the best implementation. How's that?
> 
> I think the deal with "a in iota(b, c)" gets odd quite fast if e.g.
> those are floating-point numbers and the questions shows up whether a
> must be exactly a value obtained by iterating from b to c.
[...]

Sorry, I was talking about improving iota support for non-builtin types
that support ++ or +/*.

I don't agree that "a in iota(b,c)" is a good idea; it looks cute but
hides a potential performance hit if not properly implemented. Iota is
for iteration, not for general representation of numerical ranges. And
yes, when floating-point values are involved, it quickly becomes quite
nasty.  So I vote against it in favor of between(). Specifically, this
variant among those proposed:

	bool between(string bounds="[]", T, U, V)(T val, U lower, V upper);

	unittest
	{
		assert(1.between(0u, 2.0f));	// should work with mixed types
		assert(3.14.between(3u, BigInt(4)); // and library types
	}


T

-- 
Stop staring at me like that! It's offens... no, you'll hurt your eyes!


More information about the Digitalmars-d mailing list