December 16, 2013 between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | 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 Re: between and among: worth Phobosization? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | 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.
|
Copyright © 1999-2021 by the D Language Foundation