between and among: worth Phobosization? (reprise)

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 26 15:23:12 PDT 2015


On 3/26/15 2:52 PM, Vladimir Panteleev wrote:
> On Thursday, 26 March 2015 at 21:51:54 UTC, Vladimir Panteleev
> wrote:
>> Oh yeah, this assumes hi <= lo. I thought this was part of the
>> function contract.
>
> I meant lo <= hi

New idea:

bool ordered(pred = "a < b")(T...)(T values)
{
     foreach (i, _; T[1 .. $])
     {
         if (binaryFun!pred(values[i], values[i - 1]) return false;
     }
     return true;
}

Instead of x.between(a, b) one would write ordered(a, x, b).

Cons: can't use the UFCS nicely. Doesn't generalize to all combinations 
of < and <=.

Pros: Consistent with isSorted so easy to grasp. Does generalize to 
testing multiple values.

Destroy!


Andrei



More information about the Digitalmars-d mailing list