www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dual conditions in D and Python

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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?
May 21 2015
next sibling parent reply "Alex Parrill" <initrd.gz gmail.com> writes:
On Thursday, 21 May 2015 at 16:57:16 UTC, 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?
http://wiki.dlang.org/Language_Designs_Explained#Why_does_D_not_support_chaining_comparison_operators.3F
May 21 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 21 May 2015 at 17:17:29 UTC, Alex Parrill wrote:
 http://wiki.dlang.org/Language_Designs_Explained#Why_does_D_not_support_chaining_comparison_operators.3F
Backward compatibility with C is nice but on the other hand it is a road to nowhere! Because of this compatibility, I'm compelled to write return instead of ret and else if instead of elif :) I think to create a truly correct C++, it was necessary to completely abandon the backward compatibility with C.
May 21 2015
prev sibling next sibling parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Thursday, May 21, 2015 16:57:14 Dennis Ritchie via Digitalmars-d-learn 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?
No C-based language allows what python does, and based on operators work in C-based languages, what python is doing simply doesn't fit or make sense. What happens in C/C++/D/Java/C#/etc. land is that 4 <= 5 results in a bool, at which point you'd end up with a comparison between that bool and 6, which is _not_ something that you want. But with other operators _is_ very much what you'd want. Operator chaining works in the same way across all operators in C-based languages, and trying to make 4 <= 5 <= 6 be equivalent to 4 <= 5 && 5 <= 6 would make it so that they weren't consistent. And it wouldn't make the language any more powerful, because you can quite easily just do 4 <= 5 && 5 <= 6 instead of 4 <= 5 <= 6. It only costs you a few characters and results in the language being far more consistent. I'm honestly quite surprised that python would allow such a thing, but they seem to do a lot of stuff that most programmers from C-based languages (especially C++) would think is crazy. - Jonathan M Davis
May 21 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 21 May 2015 at 17:43:25 UTC, Jonathan M Davis wrote:
 No C-based language allows what python does, and based on 
 operators work in
 C-based languages, what python is doing simply doesn't fit or 
 make sense.
 What happens in C/C++/D/Java/C#/etc. land is that 4 <= 5 
 results in a bool,
 at which point you'd end up with a comparison between that bool 
 and 6, which
 is _not_ something that you want. But with other operators _is_ 
 very much
 what you'd want. Operator chaining works in the same way across 
 all
 operators in C-based languages, and trying to make 4 <= 5 <= 6 
 be equivalent
 to 4 <= 5 && 5 <= 6 would make it so that they weren't 
 consistent. And it
 wouldn't make the language any more powerful, because you can 
 quite easily
 just do 4 <= 5 && 5 <= 6 instead of 4 <= 5 <= 6. It only costs 
 you a few
 characters and results in the language being far more 
 consistent. I'm
 honestly quite surprised that python would allow such a thing, 
 but they seem
 to do a lot of stuff that most programmers from C-based 
 languages
 (especially C++) would think is crazy.

 - Jonathan M Davis
Yes, of course, some of Python's design for C ++ - programmers will look crazy, but they are worth it :) elif instead of else if: http://rextester.com/WOSH30608 The parallel exchange values: http://rextester.com/TPUD51604
May 21 2015
next sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 21 May 2015 at 18:26:28 UTC, Dennis Ritchie wrote:
 elif instead of else if:
 http://rextester.com/WOSH30608

 The parallel exchange values:
 http://rextester.com/TPUD51604
wow!
May 21 2015
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 21 May 2015 at 18:26:28 UTC, Dennis Ritchie wrote:
 On Thursday, 21 May 2015 at 17:43:25 UTC, Jonathan M Davis 
 wrote:
 No C-based language allows what python does, and based on 
 operators work in
 C-based languages, what python is doing simply doesn't fit or 
 make sense.
 What happens in C/C++/D/Java/C#/etc. land is that 4 <= 5 
 results in a bool,
 at which point you'd end up with a comparison between that 
 bool and 6, which
 is _not_ something that you want. But with other operators 
 _is_ very much
 what you'd want. Operator chaining works in the same way 
 across all
 operators in C-based languages, and trying to make 4 <= 5 <= 6 
 be equivalent
 to 4 <= 5 && 5 <= 6 would make it so that they weren't 
 consistent. And it
 wouldn't make the language any more powerful, because you can 
 quite easily
 just do 4 <= 5 && 5 <= 6 instead of 4 <= 5 <= 6. It only costs 
 you a few
 characters and results in the language being far more 
 consistent. I'm
 honestly quite surprised that python would allow such a thing, 
 but they seem
 to do a lot of stuff that most programmers from C-based 
 languages
 (especially C++) would think is crazy.

 - Jonathan M Davis
Yes, of course, some of Python's design for C ++ - programmers will look crazy, but they are worth it :) elif instead of else if: http://rextester.com/WOSH30608 The parallel exchange values: http://rextester.com/TPUD51604
Something I sometimes do for strictly personal projects: import std.typecons : ω = tuple; import std.typetuple : Ω = TypeTuple; void main() { auto a = 1, b = 2; Ω!(a, b) = ω(b, a); assert(a==2 && b==1); }
May 21 2015
prev sibling next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Dennis Ritchie wrote:

 if (4 <= 5 <= 6):
      print ("OK")
 -----
I would rather write: if( isSorted![4,5,6]) -manfred
May 21 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
May 21 2015
next sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
 Something I sometimes do for strictly personal projects:

 import std.typecons : ω = tuple;
 import std.typetuple : Ω = TypeTuple;

 void main()
 {
     auto a = 1, b = 2;
     Ω!(a, b) = ω(b, a);
     assert(a==2 && b==1);
 }
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 this, of course, looks good, but what about the principle of the ideal programming language :) "In the end I want to focus on one philosophical principle, which lies at the basis of my ideas about the ideal programming language. Typically, during the discussion in the forums, when you start to talk in a language that is not X features Y, be sure there is someone who will say: Why, that's if you take the features A, B and C, and screw them crutches D, E, F, then we will get almost Y. Yes, it is. But I do not like this approach. One can imagine that such programmers want some complicated way through the maze. You can go through the maze, but the way the curve and non-obvious. I also want to be instead of the labyrinth has a large area, on which from any point to any other one would go in a straight line. Just a straight line." ----- The quotation is taken from the article: http://habrahabr.ru/post/257875/
May 21 2015
prev sibling parent reply "Meta" <jared771 gmail.com> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
next sibling parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
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
parent "weaselcat" <weaselcat gmail.com> writes:
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 21 2015
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
next sibling parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
prev sibling parent "weaselcat" <weaselcat gmail.com> writes:
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.
May 23 2015