Dual conditions in D and Python
Dennis Ritchie via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu May 21 12:43:59 PDT 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:
>> 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/
More information about the Digitalmars-d-learn
mailing list