May 21, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 21 May 2015 at 19:05:16 UTC, Steven Schveighoffer wrote:
> On 5/21/15 12:57 PM, Dennis Ritchie wrote:
>> Hi,
>> In Python I can write this:
>>
>> if (4 <= 5 <= 6):
>> print ("OK")
>> -----
>> http://rextester.com/NNAM70713
>>
>> In D, I can only write this:
>>
>> import std.stdio;
>>
>> void main() {
>>
>> if (4 <= 5 && 5 <= 6)
>> puts("OK");
>> }
>> -----
>> http://rextester.com/FICP83173
>>
>> I wanted to ask what is the reason? Maybe the program on Python's slower
>> because of this? Or legacy C/C++ affected D?
>
>
> There is this possibility:
>
> switch(5){
> case 4: .. case 6:
> }
>
> You could also make some nifty abuse-of-syntax types:
>
> struct Between(T)
> {
> T low;
> T high;
> bool opBinaryRight!(op : "in")(T val) { return val >= low && val <= high;}
> }
>
> auto between(T)(T low, T high) { return Between!T(low, high); }
>
> if(5 in between(4, 6))
>
> :)
>
> -Steve
All we need is user-defined opIs and then we're really cooking with gas.
if (5 is between(4, 6))
{
//...
}
|
May 21, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On 05/21/2015 12:44 PM, Meta wrote:
> All we need is user-defined opIs and then we're really cooking with gas.
>
> if (5 is between(4, 6))
> {
> //...
> }
>
We're almost there. :)
bool is_between(T0, T1, T2)(T0 what, T1 min, T2 max)
{
return (what >= min) && (what <= max);
}
void main()
{
if (5.is_between(4, 6)) {
// ...
}
}
Ali
|
May 21, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Thursday, 21 May 2015 at 21:35:22 UTC, Ali Çehreli wrote: > We're almost there. :) > > bool is_between(T0, T1, T2)(T0 what, T1 min, T2 max) > { > return (what >= min) && (what <= max); > } > > void main() > { > if (5.is_between(4, 6)) { > // ... > } > } > > Ali A condition is that if, for example, that? :) if (5 > 2 > -9 > -13 < 10 == 10 < 21 != 45): print("OK") ----- http://rextester.com/JSC75231 import std.stdio; void main() { if (5 > 2 && 2 > -9 && -9 > -13 && -13 < 10 && 10 == 10 && 10 < 21 && 21 != 45) writeln("OK"); } ----- http://rextester.com/AZFL70044 |
May 21, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | On Thursday, 21 May 2015 at 23:14:47 UTC, Dennis Ritchie wrote:
> On Thursday, 21 May 2015 at 21:35:22 UTC, Ali Çehreli wrote:
>> We're almost there. :)
>>
>> bool is_between(T0, T1, T2)(T0 what, T1 min, T2 max)
>> {
>> return (what >= min) && (what <= max);
>> }
>>
>> void main()
>> {
>> if (5.is_between(4, 6)) {
>> // ...
>> }
>> }
>>
>> Ali
>
> A condition is that if, for example, that? :)
>
> if (5 > 2 > -9 > -13 < 10 == 10 < 21 != 45):
> print("OK")
this looks like gibberish upon first sight and is not something I'd want to see in code I inherit.
|
May 23, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 5/21/15 2:35 PM, Ali Çehreli wrote: > On 05/21/2015 12:44 PM, Meta wrote: > >> All we need is user-defined opIs and then we're really cooking with gas. >> >> if (5 is between(4, 6)) >> { >> //... >> } >> > > We're almost there. :) > > bool is_between(T0, T1, T2)(T0 what, T1 min, T2 max) > { > return (what >= min) && (what <= max); > } > > void main() > { > if (5.is_between(4, 6)) { > // ... > } > } In fact we'll be there with 2.068: http://dlang.org/phobos-prerelease/std_algorithm_sorting.html#.ordered if (ordered(4, 5, 6)) { ... } if (strictlyOrdered(4, 5, 6)) { ... } Andrei |
May 23, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Sat, 2015-05-23 at 10:17 -0700, Andrei Alexandrescu via Digitalmars-d-learn wrote: > […] > > if (ordered(4, 5, 6)) { ... } > if (strictlyOrdered(4, 5, 6)) { ... } So the latter means the integers have to lashed as well as ordered? ; -) -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder |
May 23, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Saturday, 23 May 2015 at 17:17:17 UTC, Andrei Alexandrescu wrote:
> On 5/21/15 2:35 PM, Ali Çehreli wrote:
>> On 05/21/2015 12:44 PM, Meta wrote:
>>
>>> All we need is user-defined opIs and then we're really cooking with gas.
>>>
>>> if (5 is between(4, 6))
>>> {
>>> //...
>>> }
>>>
>>
>> We're almost there. :)
>>
>> bool is_between(T0, T1, T2)(T0 what, T1 min, T2 max)
>> {
>> return (what >= min) && (what <= max);
>> }
>>
>> void main()
>> {
>> if (5.is_between(4, 6)) {
>> // ...
>> }
>> }
>
> In fact we'll be there with 2.068:
>
> http://dlang.org/phobos-prerelease/std_algorithm_sorting.html#.ordered
>
> if (ordered(4, 5, 6)) { ... }
> if (strictlyOrdered(4, 5, 6)) { ... }
>
>
> Andrei
I didn't realize this got pulled, I remember it being discussed a while back on the general NG. Good addition.
|
Copyright © 1999-2021 by the D Language Foundation