digitalmars.D.learn - Dual conditions in D and Python
- Dennis Ritchie (16/16) May 21 2015 Hi,
- Alex Parrill (2/18) May 21 2015 http://wiki.dlang.org/Language_Designs_Explained#Why_does_D_not_support_...
- Dennis Ritchie (7/8) May 21 2015 Backward compatibility with C is nice but on the other hand it is
- Jonathan M Davis via Digitalmars-d-learn (16/32) May 21 2015 No C-based language allows what python does, and based on operators work...
- Dennis Ritchie (7/35) May 21 2015 Yes, of course, some of Python's design for C ++ - programmers
- Gary Willoughby (2/6) May 21 2015 wow!
- John Colvin (10/47) May 21 2015 Something I sometimes do for strictly personal projects:
- Manfred Nowak (4/7) May 21 2015 I would rather write:
- Steven Schveighoffer (17/33) May 21 2015 There is this possibility:
- Dennis Ritchie (19/68) May 21 2015 All this, of course, looks good, but what about the principle of
- Meta (8/48) May 21 2015 All we need is user-defined opIs and then we're really cooking
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (13/18) May 21 2015 We're almost there. :)
- Dennis Ritchie (20/32) May 21 2015 A condition is that if, for example, that? :)
- weaselcat (3/22) May 21 2015 this looks like gibberish upon first sight and is not something
- Andrei Alexandrescu (6/25) May 23 2015 In fact we'll be there with 2.068:
- Russel Winder via Digitalmars-d-learn (9/13) May 23 2015 So the latter means the integers have to lashed as well as ordered? ;
- weaselcat (4/34) May 23 2015 I didn't realize this got pulled, I remember it being discussed a
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
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
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.3FBackward 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
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. 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
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. 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 DavisYes, 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
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/TPUD51604wow!
May 21 2015
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: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); }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. 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 DavisYes, 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
Dennis Ritchie wrote:if (4 <= 5 <= 6): print ("OK") -----I would rather write: if( isSorted![4,5,6]) -manfred
May 21 2015
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
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: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/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
On Thursday, 21 May 2015 at 19:05:16 UTC, Steven Schveighoffer wrote:On 5/21/15 12:57 PM, Dennis Ritchie wrote:All we need is user-defined opIs and then we're really cooking with gas. if (5 is between(4, 6)) { //... }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
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
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)) { // ... } } AliA 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
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:this looks like gibberish upon first sight and is not something I'd want to see in code I inherit.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)) { // ... } } AliA condition is that if, for example, that? :) if (5 > 2 > -9 > -13 < 10 == 10 < 21 != 45): print("OK")
May 21 2015
On 5/21/15 2:35 PM, Ali Çehreli wrote:On 05/21/2015 12:44 PM, Meta wrote:In fact we'll be there with 2.068: if (ordered(4, 5, 6)) { ... } if (strictlyOrdered(4, 5, 6)) { ... } AndreiAll 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)) { // ... } }
May 23 2015
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
On Saturday, 23 May 2015 at 17:17:17 UTC, Andrei Alexandrescu wrote:On 5/21/15 2:35 PM, Ali Çehreli wrote:I didn't realize this got pulled, I remember it being discussed a while back on the general NG. Good addition.On 05/21/2015 12:44 PM, Meta wrote:In fact we'll be there with 2.068: if (ordered(4, 5, 6)) { ... } if (strictlyOrdered(4, 5, 6)) { ... } AndreiAll 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)) { // ... } }
May 23 2015