Jump to page: 1 211  
Page
Thread overview
between and among: worth Phobosization?
Dec 16, 2013
bearophile
Dec 16, 2013
Walter Bright
Dec 16, 2013
Timon Gehr
Dec 16, 2013
Walter Bright
Dec 16, 2013
Andrej Mitrovic
Dec 17, 2013
Marco Leise
Dec 17, 2013
Simen Kjærås
Dec 16, 2013
H. S. Teoh
Dec 17, 2013
Marco Leise
Dec 17, 2013
Walter Bright
Dec 17, 2013
Marco Leise
Dec 16, 2013
Brad Anderson
Dec 16, 2013
Timon Gehr
Dec 16, 2013
Walter Bright
Dec 16, 2013
Jakob Ovrum
Dec 16, 2013
bearophile
Dec 16, 2013
H. S. Teoh
Dec 16, 2013
Walter Bright
Dec 17, 2013
Simen Kjærås
Dec 17, 2013
H. S. Teoh
Dec 17, 2013
H. S. Teoh
Dec 16, 2013
Walter Bright
Dec 17, 2013
H. S. Teoh
Dec 16, 2013
Timon Gehr
Dec 16, 2013
Max Klyga
Dec 17, 2013
Chris Cain
Dec 17, 2013
Chris Cain
Dec 17, 2013
H. S. Teoh
Dec 18, 2013
Chris Cain
Dec 18, 2013
Timon Gehr
Dec 17, 2013
H. S. Teoh
Dec 18, 2013
Chris Cain
Dec 18, 2013
Chris Cain
Dec 17, 2013
Vladimir Panteleev
Dec 17, 2013
Jerry
Dec 17, 2013
Jerry
Dec 17, 2013
Byron
Dec 17, 2013
Andrej Mitrovic
Dec 17, 2013
Byron
Dec 17, 2013
Dmitry Olshansky
Dec 17, 2013
Byron
Dec 19, 2013
Lionello Lunesu
Dec 19, 2013
Lionello Lunesu
Dec 19, 2013
Jakob Ovrum
Re: between and among: worth Phobosization? (reprise)
Mar 26, 2015
Jesse Phillips
Mar 26, 2015
Tobias Pankrath
Mar 26, 2015
Vladimir Panteleev
Mar 26, 2015
Vladimir Panteleev
Mar 26, 2015
Vladimir Panteleev
Mar 26, 2015
H. S. Teoh
Mar 26, 2015
Jakob Ovrum
Mar 26, 2015
H. S. Teoh
Mar 27, 2015
Vladimir Panteleev
Mar 27, 2015
H. S. Teoh
Mar 27, 2015
John Colvin
December 16, 2013
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?


Andrei
December 16, 2013
Andrei Alexandrescu:

> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
> {
>     return v >= lo && v <= hi;
> }

I vote a +1 for between(). One use case:

http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Stronger_Statically_Typed_Version

It also helps in the D translation of Python expressions like:

if foo() and 3 < bar(x) < 5 and ...:

That in D need to be split in more lines unless bar() is pure and light.

-----------------

Regarding Phobos, currently one of the features I miss mostly is an optional template function for the min and max functions:

https://d.puremagic.com/issues/show_bug.cgi?id=4705#c16

Bye,
bearophile
December 16, 2013
On Monday, 16 December 2013 at 21:13:58 UTC, bearophile wrote:

> It also helps in the D translation of Python expressions like:
>
> if foo() and 3 < bar(x) < 5 and ...:

Not really, it uses "<=" which illustrates why it is better to use lambdas where a named function hides what goes on and there are several interpretations.

Besides, in many algos you want "low <= x < hi"  or the opposite to avoid double-hits on edge-cases.
December 16, 2013
On 12/16/2013 12:38 PM, Andrei Alexandrescu wrote:
> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
> {
>      return v >= lo && v <= hi;
> }

You'd need 4 such functions, < <, < <=, <= <, <= <=, and it just seems like trivia.

>
> uint among(T, Us...)(T v, Us vals)
> {
>      foreach (i, U; Us)
>      {
>          if (v == vals[i]) return i + 1;
>      }
>      return 0;
> }

This has O(n) behavior, which might be unexpected for the user.

December 16, 2013
On Monday, 16 December 2013 at 20:38:52 UTC, Andrei Alexandrescu wrote:
> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
> {
>     return v >= lo && v <= hi;
> }
>

I must say that:

    if (val.between(3, 10))

sure is a lot easier to understand at a glance than:

    if (val >= 3 && val <= 10)

Although there is a problem with the word "between" not being clear about whether it is inclusive or not.

I do kind of enjoy the Ruby-style DSL-lite they use to make code more readable so I'm for it.
December 16, 2013
On 12/16/2013 09:38 PM, Andrei Alexandrescu wrote:
> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
> {
>      return v >= lo && v <= hi;
> }
> ...

There's the issue of different possibilities for inclusive/exclusive end points. Maybe we want to add a template parameter taking values from ["[)","()","(]","[]"]. Even then it is not too clear what the default should be. (Probably "[)".)

> uint among(T, Us...)(T v, Us vals)
> {
>      foreach (i, U; Us)
>      {
>          if (v == vals[i]) return i + 1;
>      }
>      return 0;
> }
> ...

I have my own version of this. I vote add.

December 16, 2013
On 12/16/2013 10:45 PM, Walter Bright wrote:
> On 12/16/2013 12:38 PM, Andrei Alexandrescu wrote:
>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>> {
>>      return v >= lo && v <= hi;
>> }
>
> You'd need 4 such functions, < <, < <=, <= <, <= <=, and it just seems
> like trivia.
>
>>
>> uint among(T, Us...)(T v, Us vals)
>> {
>>      foreach (i, U; Us)
>>      {
>>          if (v == vals[i]) return i + 1;
>>      }
>>      return 0;
>> }
>
> This has O(n) behavior, which might be unexpected for the user.
>

Do you mean O(vals.length)? O(vals.length)=O(1).
December 16, 2013
On Monday, 16 December 2013 at 21:47:35 UTC, Brad Anderson wrote:
>     if (val >= 3 && val <= 10)

if (3 <= val && val <= 10)

Pretty clear.
December 16, 2013
On 12/16/2013 10:47 PM, Brad Anderson wrote:
> On Monday, 16 December 2013 at 20:38:52 UTC, Andrei Alexandrescu wrote:
>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>> {
>>     return v >= lo && v <= hi;
>> }
>>
>
> I must say that:
>
>      if (val.between(3, 10))
>
> sure is a lot easier to understand at a glance than:
>
>      if (val >= 3 && val <= 10)
> ...

if (3 <= val && val <= 10)

is similarly easy to understand at a glance as the former. (Its drawback compared to that is that often a temporary variable will be required to be introduced in order to avoid recomputation.)
December 16, 2013
On 12/16/2013 1:47 PM, Brad Anderson wrote:
> On Monday, 16 December 2013 at 20:38:52 UTC, Andrei Alexandrescu wrote:
>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>> {
>>     return v >= lo && v <= hi;
>> }
>>
>
> I must say that:
>
>      if (val.between(3, 10))
>
> sure is a lot easier to understand at a glance than:
>
>      if (val >= 3 && val <= 10)
>
> Although there is a problem with the word "between" not being clear about
> whether it is inclusive or not.

Exactly, meaning I'd have to go look at the source code for it, whereas with the latter I can see right away what it is. The function is a net programmer time loss.

« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11