between and among: worth Phobosization? (reprise)

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 26 15:28:24 PDT 2015


On Thu, Mar 26, 2015 at 03:23:12PM -0700, Andrei Alexandrescu via Digitalmars-d wrote:
> 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!
[...]

Neat idea! This would let us translate mathematical statements of the
form "1 < x < 2 < y < 3" to ordered(1, x, 2, y, 3), and various other
combinations.

Don't like the name, though. Prefer 'isOrdered', otherwise it sounds
like some kind of sorting algorithm (as in, returns an ordered sequence
of its arguments).

As for combinations of < and <=, what about taking multiple template
arguments? E.g.:

	if (isOrdered!("<", "<=")(0, x, 10)) { ... }


T

-- 
If it tastes good, it's probably bad for you.


More information about the Digitalmars-d mailing list