between and among: worth Phobosization? (reprise)

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sat Mar 14 18:48:54 PDT 2015


On 12/16/13 12:38 PM, Andrei Alexandrescu wrote:
> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
> {
>      return v >= lo && v <= hi;
> }
>
> uint among(T, Us...)(T v, Us vals)
> {
>      foreach (i, U; Us)
>      {
>          if (v == vals[i]) return i + 1;
>      }
>      return 0;
> }
>
> Add?

Looks like among() has proven its worth since we introduced it. Now I 
somehow forgot between() didn't make it, and reviewed some code at work 
assuming it exists! Here's the original and proposed in a couple of 
snippets:

return (path.asPath.logicalLength() <= asPathLengths_[1] &&
             path.asPath.logicalLength() >= asPathLengths_[0]);

=>

return path.asPath.logicalLength.between(asPathLengths_[0], 
asPathLengths_[1]);

====

if (prefix.prefixLen > prefixLenRange_[1] ||
         prefix.prefixLen < prefixLenRange_[0]) {

=>

if (!prefix.prefixLen.between(prefixLenRange_[0], prefixLenRange_[1])) {

====

Well?


Andrei



More information about the Digitalmars-d mailing list