Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 21, 2015 Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex Parrill | 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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | Dennis Ritchie wrote:
> if (4 <= 5 <= 6):
> print ("OK")
> -----
I would rather write:
if( isSorted![4,5,6])
-manfred
|
May 21, 2015 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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 Re: Dual conditions in D and Python | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | > 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/ |
Copyright © 1999-2021 by the D Language Foundation