digitalmars.D - make (a < b < c) illegal?
- Walter Bright (14/14) Feb 07 2007 Right now, in D (as well as C and C++), when you see the expression:
- Andrei Alexandrescu (See Website For Email) (6/26) Feb 07 2007 For the record, (clears throat) let me add that I initially suggested to...
- Steve Horne (8/15) Feb 07 2007
- Frits van Bommel (2/18) Feb 08 2007 My thoughts exactly.
- Andrei Alexandrescu (See Website For Email) (4/24) Feb 08 2007 Never thought of that. Very interesting. The good cop/bad cop routine
- Derek Parnell (14/34) Feb 07 2007 First thought: Yes, your proposed change makes sense.
- Andrei Alexandrescu (See Website For Email) (5/39) Feb 07 2007 What's the intended meaning of:
- Kirk McDonald (8/57) Feb 07 2007 It's worth pointing out that Python handles these operations just like
- Andrei Alexandrescu (See Website For Email) (5/60) Feb 07 2007 Obligatory comment: it would return true also if it were interpreted as
- Kirk McDonald (10/17) Feb 07 2007 For ultimate clarity, I refer you to the Python language reference on
- Andrei Alexandrescu (See Website For Email) (7/24) Feb 07 2007 Oh, I did run the interpreter (gotta love Linux - all the cool stuff is
- Derek Parnell (17/56) Feb 07 2007 Well that *obvious*!
- Andrei Alexandrescu (See Website For Email) (8/61) Feb 07 2007 It's very easy to implement as a straight template function:
- Derek Parnell (21/86) Feb 07 2007 Right ...
- Andrei Alexandrescu (See Website For Email) (9/25) Feb 07 2007 It's really simple:
- Derek Parnell (17/25) Feb 07 2007 Thanks. It wasn't clear to me in the docs how this would be achieved. I'...
- Andrei Alexandrescu (See Website For Email) (5/18) Feb 07 2007 A tutorial for template code would indeed be great. A lil bird told me
- Walter Bright (3/6) Feb 07 2007 It's already up there!
- Derek Parnell (13/20) Feb 07 2007 And I assume that this fact is documented somewhere obvious. I'm not in ...
- Walter Bright (2/5) Feb 07 2007 It was in the wrong directory... try again.
- Derek Parnell (18/32) Feb 08 2007 I give up. I can't work out how to create this functionality for an
- Michiel (5/7) Feb 08 2007 You need to use recursion, not loops. I'm not sure how the syntax works ...
- Tom S (22/31) Feb 08 2007 How about:
- Derek Parnell (10/43) Feb 08 2007 Yes this works, but I don't know why. Is the 'foreach' executed at compi...
- Tom S (3/42) Feb 08 2007 Here's some stuff: http://digitalmars.com/d/tuple.html
- Derek Parnell (8/17) Feb 08 2007 Maybe ... I tried many combinations based on that. Still no luck.
- Daniel Giddings (21/39) Feb 08 2007 To get recursion going you need add a 1 element case and a static if to
- Michiel (1/3) Feb 08 2007 Of course! Stupid, stupid.
- Frits van Bommel (12/26) Feb 08 2007 Besides the foreach approaches already posted, this works as well:
- Reiner Pope (11/20) Feb 08 2007 You can use a foreach-over-tuple instead of recursion:
- Russell Lewis (14/31) Feb 13 2007 The chain that I'm concerned about is this:
- Russell Lewis (5/39) Feb 13 2007 Addendum: I would be ok with making the less than/greater than operators...
- Joel C. Salomon (7/26) Feb 14 2007 Why this prejudice? With the chaining as people have discussed it,
- Bill Baxter (3/30) Feb 14 2007 I would assume the reason is because it's not used in math.
- Joel Salomon (4/11) Feb 14 2007 I’ve used it — and hairier stuff besides.
- Jarrett Billingsley (6/7) Feb 07 2007 I'd really like to be able to say
- Lionello Lunesu (8/15) Feb 07 2007 So your first thought, the newbie's first thought and my first thought
- Andrei Alexandrescu (See Website For Email) (4/23) Feb 07 2007 I personally would like that. It is a good example of a dwim feature
- Henning Hasemann (14/14) Feb 08 2007 Another way to solve this problem might be the following:
- Henning Hasemann (17/33) Feb 15 2007 If I remember correctly this is also what eiffel does btw,
- Peter Modzelewski (16/36) Feb 08 2007 It's cool when language makes it easy not to make mistakes (maybe not as...
- Lionello Lunesu (5/6) Feb 08 2007 Find me one person :)
- Miles (16/31) Feb 08 2007 In C, C++ and D, the way they are now, my first thought is the
- Nicolai Waniek (17/37) Feb 14 2007 I'd like to have
Right now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.
Feb 07 2007
Walter Bright wrote:Right now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.For the record, (clears throat) let me add that I initially suggested to make them associative with the right meaning, but was shot down. Making the comparison operators (and that includes != and ==) nonassociative is the next best thing to do. Andrei
Feb 07 2007
On Wed, 07 Feb 2007 17:06:07 -0800, "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote:Walter Bright wrote:Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative.For the record, (clears throat) let me add that I initially suggested to make them associative with the right meaning, but was shot down. Making the comparison operators (and that includes != and ==) nonassociative is the next best thing to do.Agreed, but with an extra note - once the operators have been nonassociative for a while, maybe making them associative again (but with the right meanings) might be more palatable. -- Remove 'wants' and 'nospam' from e-mail.
Feb 07 2007
Steve Horne wrote:On Wed, 07 Feb 2007 17:06:07 -0800, "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote:My thoughts exactly.Walter Bright wrote:Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative.For the record, (clears throat) let me add that I initially suggested to make them associative with the right meaning, but was shot down. Making the comparison operators (and that includes != and ==) nonassociative is the next best thing to do.Agreed, but with an extra note - once the operators have been nonassociative for a while, maybe making them associative again (but with the right meanings) might be more palatable.
Feb 08 2007
Frits van Bommel wrote:Steve Horne wrote:Never thought of that. Very interesting. The good cop/bad cop routine all over again :o). AndreiOn Wed, 07 Feb 2007 17:06:07 -0800, "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> wrote:My thoughts exactly.Walter Bright wrote:Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative.For the record, (clears throat) let me add that I initially suggested to make them associative with the right meaning, but was shot down. Making the comparison operators (and that includes != and ==) nonassociative is the next best thing to do.Agreed, but with an extra note - once the operators have been nonassociative for a while, maybe making them associative again (but with the right meanings) might be more palatable.
Feb 08 2007
On Wed, 07 Feb 2007 16:55:15 -0800, Walter Bright wrote:Right now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.First thought: Yes, your proposed change makes sense. Second thought: Why not make it do what the coder is wanting it to do? Namely, make the idiom: expression1 relopA expression2 relopB expression3 translate as ( auto temp = expression2, (expression1 relopA temp) && (temp relopB expression3) ) -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 8/02/2007 12:01:13 PM
Feb 07 2007
Derek Parnell wrote:On Wed, 07 Feb 2007 16:55:15 -0800, Walter Bright wrote:What's the intended meaning of: a < b == c < d ? AndreiRight now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.First thought: Yes, your proposed change makes sense. Second thought: Why not make it do what the coder is wanting it to do? Namely, make the idiom: expression1 relopA expression2 relopB expression3 translate as ( auto temp = expression2, (expression1 relopA temp) && (temp relopB expression3) )
Feb 07 2007
Andrei Alexandrescu (See Website For Email) wrote:Derek Parnell wrote:It's worth pointing out that Python handles these operations just like Derek suggests. Take the following from Python's interactive mode:On Wed, 07 Feb 2007 16:55:15 -0800, Walter Bright wrote:What's the intended meaning of: a < b == c < d ? AndreiRight now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.First thought: Yes, your proposed change makes sense. Second thought: Why not make it do what the coder is wanting it to do? Namely, make the idiom: expression1 relopA expression2 relopB expression3 translate as ( auto temp = expression2, (expression1 relopA temp) && (temp relopB expression3) )True -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.orga=1 b=2 c=2 d=3 a<b==c<d
Feb 07 2007
Kirk McDonald wrote:Andrei Alexandrescu (See Website For Email) wrote:Obligatory comment: it would return true also if it were interpreted as (a < b) == (c < d) or as ((a < b) == c) < d. So the example is unclear. :o) AndreiDerek Parnell wrote:It's worth pointing out that Python handles these operations just like Derek suggests. Take the following from Python's interactive mode: >>> a=1 >>> b=2 >>> c=2 >>> d=3 >>> a<b==c<d TrueOn Wed, 07 Feb 2007 16:55:15 -0800, Walter Bright wrote:What's the intended meaning of: a < b == c < d ? AndreiRight now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.First thought: Yes, your proposed change makes sense. Second thought: Why not make it do what the coder is wanting it to do? Namely, make the idiom: expression1 relopA expression2 relopB expression3 translate as ( auto temp = expression2, (expression1 relopA temp) && (temp relopB expression3) )
Feb 07 2007
Andrei Alexandrescu (See Website For Email) wrote:Obligatory comment: it would return true also if it were interpreted as (a < b) == (c < d) or as ((a < b) == c) < d. So the example is unclear. :o) AndreiFor ultimate clarity, I refer you to the Python language reference on the topic: http://docs.python.org/ref/comparisons.html It's probably better to point you there than for me to attempt to explain it poorly. :-) -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Feb 07 2007
Kirk McDonald wrote:Andrei Alexandrescu (See Website For Email) wrote:Oh, I did run the interpreter (gotta love Linux - all the cool stuff is one command away) and figured how chained comparisons work. Thanks a lot for the reference. BTW, Perl 6 is slated to implement chain comparisons as well: http://en.wikipedia.org/wiki/Perl_6#Chained_comparisons AndreiObligatory comment: it would return true also if it were interpreted as (a < b) == (c < d) or as ((a < b) == c) < d. So the example is unclear. :o) AndreiFor ultimate clarity, I refer you to the Python language reference on the topic: http://docs.python.org/ref/comparisons.html It's probably better to point you there than for me to attempt to explain it poorly. :-)
Feb 07 2007
On Wed, 07 Feb 2007 17:09:35 -0800, Andrei Alexandrescu (See Website For Email) wrote:Derek Parnell wrote:Well that *obvious*! (((a < b) && (b == c)) < d) <G> Okay, okay, I see your point. But it would be useful (one day) to easily code the idiom (a op b) && (b op c), no? How about someone knocking up a mixin template for expressions of this format? I haven't got a clue how it could be done as the mixin/template/meta-programming syntax and semantics of D is still so obtuse and confusing to me that I can only do the very simplest things and then only after many false starts. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 8/02/2007 1:28:00 PMOn Wed, 07 Feb 2007 16:55:15 -0800, Walter Bright wrote:What's the intended meaning of: a < b == c < dRight now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.First thought: Yes, your proposed change makes sense. Second thought: Why not make it do what the coder is wanting it to do? Namely, make the idiom: expression1 relopA expression2 relopB expression3 translate as ( auto temp = expression2, (expression1 relopA temp) && (temp relopB expression3) )
Feb 07 2007
Derek Parnell wrote:On Wed, 07 Feb 2007 17:09:35 -0800, Andrei Alexandrescu (See Website For Email) wrote:It's very easy to implement as a straight template function: ordered(a, b, c) returns true iff a <= b <= c, and strictly_ordered(a, b, c) returns true iff a < b < c. The functions can be nicely accommodate multiple arguments. Other functions can define STL-style in-range. AndreiDerek Parnell wrote:Well that *obvious*! (((a < b) && (b == c)) < d) <G> Okay, okay, I see your point. But it would be useful (one day) to easily code the idiom (a op b) && (b op c), no? How about someone knocking up a mixin template for expressions of this format? I haven't got a clue how it could be done as the mixin/template/meta-programming syntax and semantics of D is still so obtuse and confusing to me that I can only do the very simplest things and then only after many false starts.On Wed, 07 Feb 2007 16:55:15 -0800, Walter Bright wrote:What's the intended meaning of: a < b == c < dRight now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.First thought: Yes, your proposed change makes sense. Second thought: Why not make it do what the coder is wanting it to do? Namely, make the idiom: expression1 relopA expression2 relopB expression3 translate as ( auto temp = expression2, (expression1 relopA temp) && (temp relopB expression3) )
Feb 07 2007
On Wed, 07 Feb 2007 19:02:06 -0800, Andrei Alexandrescu (See Website For Email) wrote:Derek Parnell wrote:Right ... class Foo { . . . } class Bar { . . . } Foo f = new Foo; Foo g = new Foo; Bar b = new Bar; if (ordered( f,b,g ) ) ... BANG! The template doesn't know about Foo and Bar, so there is no match. In fact, just getting a template for built-in types requires a few thousand combinations of a,b,c signatures and aliases to make it palatable. That is why I thought mixin-expressions might be more useful in this case - working at the textual level rather than the template-matching level. But I do know how to do it. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 8/02/2007 2:39:20 PMOn Wed, 07 Feb 2007 17:09:35 -0800, Andrei Alexandrescu (See Website For Email) wrote:It's very easy to implement as a straight template function: ordered(a, b, c) returns true iff a <= b <= c, and strictly_ordered(a, b, c) returns true iff a < b < c. The functions can be nicely accommodate multiple arguments. Other functions can define STL-style in-range.Derek Parnell wrote:Well that *obvious*! (((a < b) && (b == c)) < d) <G> Okay, okay, I see your point. But it would be useful (one day) to easily code the idiom (a op b) && (b op c), no? How about someone knocking up a mixin template for expressions of this format? I haven't got a clue how it could be done as the mixin/template/meta-programming syntax and semantics of D is still so obtuse and confusing to me that I can only do the very simplest things and then only after many false starts.On Wed, 07 Feb 2007 16:55:15 -0800, Walter Bright wrote:What's the intended meaning of: a < b == c < dRight now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.First thought: Yes, your proposed change makes sense. Second thought: Why not make it do what the coder is wanting it to do? Namely, make the idiom: expression1 relopA expression2 relopB expression3 translate as ( auto temp = expression2, (expression1 relopA temp) && (temp relopB expression3) )
Feb 07 2007
Derek Parnell wrote:class Foo { . . . } class Bar { . . . } Foo f = new Foo; Foo g = new Foo; Bar b = new Bar; if (ordered( f,b,g ) ) ... BANG! The template doesn't know about Foo and Bar, so there is no match. In fact, just getting a template for built-in types requires a few thousand combinations of a,b,c signatures and aliases to make it palatable. That is why I thought mixin-expressions might be more useful in this case - working at the textual level rather than the template-matching level. But I do know how to do it.It's really simple: bool ordered(T, U, V)(T a, U b, V c) { return a < b && b < c; } Expanding this to multiple arguments is also quite simple, but left as an exercise. :o) Andrei
Feb 07 2007
On Wed, 07 Feb 2007 19:51:06 -0800, Andrei Alexandrescu (See Website For Email) wrote:It's really simple: bool ordered(T, U, V)(T a, U b, V c) { return a < b && b < c; }Thanks. It wasn't clear to me in the docs how this would be achieved. I've read the template docs many times and it still doesn't make much clear.Expanding this to multiple arguments is also quite simple, but left as an exercise. :o)Okay, I'll give it a go, but I'm not confident. I'd have a go at updating the docs but *Doc* changes are even harder to get accepted than phobos changes <G> Walter, would it be too hard to make the documentation source available too? I'm not talking about the embedded phobos docs but all the other stuff. e.g. The source that generates the "template.html". -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 8/02/2007 4:29:42 PM
Feb 07 2007
Derek Parnell wrote:On Wed, 07 Feb 2007 19:51:06 -0800, Andrei Alexandrescu (See Website For Email) wrote:A tutorial for template code would indeed be great. A lil bird told me the mythical "The D Programming Language" book will definitely include one :o). AndreiIt's really simple: bool ordered(T, U, V)(T a, U b, V c) { return a < b && b < c; }Thanks. It wasn't clear to me in the docs how this would be achieved. I've read the template docs many times and it still doesn't make much clear.
Feb 07 2007
Derek Parnell wrote:Walter, would it be too hard to make the documentation source available too? I'm not talking about the embedded phobos docs but all the other stuff. e.g. The source that generates the "template.html".It's already up there! http://ftp.digitalmars.com/doc.zip
Feb 07 2007
On Wed, 07 Feb 2007 22:12:50 -0800, Walter Bright wrote:Derek Parnell wrote:And I assume that this fact is documented somewhere obvious. I'm not in the habit of browsing FTP sites on the off chance that something useful might be there. Also, if I had have seen "doc.zip" I would have assumed it was the distributed documentation and not the documentation source. Plus "doc.zip" doesn't exactly yell out "D PROGRAMMING LANGUAGE" either.Walter, would it be too hard to make the documentation source available too? I'm not talking about the embedded phobos docs but all the other stuff. e.g. The source that generates the "template.html".It's already up there!http://ftp.digitalmars.com/doc.zipHmmm... your server doesn't think it is. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 8/02/2007 5:17:21 PM
Feb 07 2007
Derek Parnell wrote:It was in the wrong directory... try again.http://ftp.digitalmars.com/doc.zipHmmm... your server doesn't think it is.
Feb 07 2007
On Thu, 8 Feb 2007 16:36:47 +1100, Derek Parnell wrote:On Wed, 07 Feb 2007 19:51:06 -0800, Andrei Alexandrescu (See Website For Email) wrote:I give up. I can't work out how to create this functionality for an arbitary number of arguments. e.g. When I code ... ordered(a,b,c,d,e) I want generated ... (a < b) && (b < c) && (c < d) && (d < e) I can't work out how to loop through the tuple. I started with template ordered(T ...) { } And everything I tried after that just didn't work. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnellIt's really simple: bool ordered(T, U, V)(T a, U b, V c) { return a < b && b < c; } Expanding this to multiple arguments is also quite simple, but left as an exercise. :o)Okay, I'll give it a go, but I'm not confident.
Feb 08 2007
I give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }
Feb 08 2007
Michiel wrote:How about: bool ascending(T ...)(T t) { static if (t.length > 0) { foreach (i, x; t[0..$-1]) { if (x >= t[i+1]) return false; } } return true; } void main() { assert (ascending(1, 2, 3, 4)); assert (ascending(1, 2)); assert (ascending(1.2, 2.1, 3.5, 4.7, 8.6)); assert (!ascending(1.2, 1.0)); assert (!ascending(1.2, 1.5, 0.5)); assert (ascending(1)); assert (ascending()); } ? -- Tomasz StachowiakI give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }
Feb 08 2007
On Thu, 08 Feb 2007 12:27:58 +0100, Tom S wrote:Michiel wrote:Yes this works, but I don't know why. Is the 'foreach' executed at compile time or run time? If compile time, where is this documented? Its not in the template docs, nor the conditional compilation page, or on the statements page. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnellHow about: bool ascending(T ...)(T t) { static if (t.length > 0) { foreach (i, x; t[0..$-1]) { if (x >= t[i+1]) return false; } } return true; } void main() { assert (ascending(1, 2, 3, 4)); assert (ascending(1, 2)); assert (ascending(1.2, 2.1, 3.5, 4.7, 8.6)); assert (!ascending(1.2, 1.0)); assert (!ascending(1.2, 1.5, 0.5)); assert (ascending(1)); assert (ascending()); }I give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }
Feb 08 2007
Derek Parnell wrote:On Thu, 08 Feb 2007 12:27:58 +0100, Tom S wrote:Both :D It executes at runtime but is completely unrolled, afaics.Michiel wrote:Yes this works, but I don't know why. Is the 'foreach' executed at compile time or run time?How about: bool ascending(T ...)(T t) { static if (t.length > 0) { foreach (i, x; t[0..$-1]) { if (x >= t[i+1]) return false; } } return true; } void main() { assert (ascending(1, 2, 3, 4)); assert (ascending(1, 2)); assert (ascending(1.2, 2.1, 3.5, 4.7, 8.6)); assert (!ascending(1.2, 1.0)); assert (!ascending(1.2, 1.5, 0.5)); assert (ascending(1)); assert (ascending()); }I give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }If compile time, where is this documented? Its not in the template docs, nor the conditional compilation page, or on the statements page.Here's some stuff: http://digitalmars.com/d/tuple.html
Feb 08 2007
On Thu, 8 Feb 2007 10:50:56 +0000 (UTC), Michiel wrote:Maybe ... I tried many combinations based on that. Still no luck. Show me something that compiles and runs, anyone? -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnellI give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }
Feb 08 2007
To get recursion going you need add a 1 element case and a static if to handle the degeneration of the recursion to Michiel's version... import std.stdio; bool ordered( T )( T a ) { return true; } bool ordered( T1, T2, R... )( T1 a, T2 b, R remainder ) { static if( remainder.length > 0 ) return a < b && ordered( b, remainder ); else return a < b; } void main() { writefln( ordered(1,2,4) ); writefln( ordered(1,4,2) ); } "Derek Parnell" <derek psych.ward> wrote in message news:<mqx61f4cfl24.yz4pjbrcs05z.dlg 40tude.net>...On Thu, 8 Feb 2007 10:50:56 +0000 (UTC), Michiel wrote:Maybe ... I tried many combinations based on that. Still no luck. Show me something that compiles and runs, anyone? -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnellI give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }
Feb 08 2007
To get recursion going you need add a 1 element case and a static if to handle the degeneration of the recursion to Michiel's version...Of course! Stupid, stupid.
Feb 08 2007
Derek Parnell wrote:On Thu, 8 Feb 2007 10:50:56 +0000 (UTC), Michiel wrote:Besides the foreach approaches already posted, this works as well: ----- bool ordered(R...)(R args) { static if (args.length < 2) return true; else return (args[0] < args[1]) && ordered(args[1..$]); } ----- It seems DMD just doesn't like it if the first two parameters have separately templated types...Maybe ... I tried many combinations based on that. Still no luck. Show me something that compiles and runs, anyone?I give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }
Feb 08 2007
Michiel wrote:You can use a foreach-over-tuple instead of recursion: bool ordered(T...)(T data) { foreach(i, d; data) { static if (i < data.length - 1) if (! (d < data[i+1])) return false; } return true; }I give up. I can't work out how to create this functionality for an arbitary number of arguments.You need to use recursion, not loops. I'm not sure how the syntax works exactly, but it might look something like this: bool ordered(T1, T2, Trest ...)(T1 first, T2 second, Trest rest) { return (first < second) && ordered(second, rest); }
Feb 08 2007
Derek Parnell wrote:On Wed, 07 Feb 2007 17:09:35 -0800, Andrei Alexandrescu (See Website ForThe chain that I'm concerned about is this: a==b == c==d which (the spacing makes clear) is meant to be (a==b) == (c==d) but which could me misread as (a==b) && (b==c) && (c==d) and which is probably (I'm not sure) currently implemented by the compiler as: ((a==b) ==c) ==d IMHO, C should have allowed comparison chaining from the start, but since it didn't, I don't think that it would be a good idea to start allowing it. There will always be the newbies from C who will misread it. (sigh)What's the intended meaning of: a < b == c < dWell that *obvious*! (((a < b) && (b == c)) < d) <G> Okay, okay, I see your point. But it would be useful (one day) to easily code the idiom (a op b) && (b op c), no? How about someone knocking up a mixin template for expressions of this format? I haven't got a clue how it could be done as the mixin/template/meta-programming syntax and semantics of D is still so obtuse and confusing to me that I can only do the very simplest things and then only after many false starts.
Feb 13 2007
Russell Lewis wrote:Derek Parnell wrote:Addendum: I would be ok with making the less than/greater than operators be chainable (since those operators are nonsense when used with boolean values), but I would ask that no expression be able to mix less-than and greater-than. It would be ok to mix < with <=, but not < with >.On Wed, 07 Feb 2007 17:09:35 -0800, Andrei Alexandrescu (See Website ForThe chain that I'm concerned about is this: a==b == c==d which (the spacing makes clear) is meant to be (a==b) == (c==d) but which could me misread as (a==b) && (b==c) && (c==d) and which is probably (I'm not sure) currently implemented by the compiler as: ((a==b) ==c) ==d IMHO, C should have allowed comparison chaining from the start, but since it didn't, I don't think that it would be a good idea to start allowing it. There will always be the newbies from C who will misread it. (sigh)What's the intended meaning of: a < b == c < dWell that *obvious*! (((a < b) && (b == c)) < d) <G> Okay, okay, I see your point. But it would be useful (one day) to easily code the idiom (a op b) && (b op c), no? How about someone knocking up a mixin template for expressions of this format? I haven't got a clue how it could be done as the mixin/template/meta-programming syntax and semantics of D is still so obtuse and confusing to me that I can only do the very simplest things and then only after many false starts.
Feb 13 2007
Russell Lewis wrote:Why this prejudice? With the chaining as people have discussed it, a < b > c expands to (a < b) && (b > c) — why would you prohibit this? --JoelThe chain that I'm concerned about is this: a==b == c==d which (the spacing makes clear) is meant to be (a==b) == (c==d) but which could me misread as (a==b) && (b==c) && (c==d) and which is probably (I'm not sure) currently implemented by the compiler as: ((a==b) ==c) ==d IMHO, C should have allowed comparison chaining from the start, but since it didn't, I don't think that it would be a good idea to start allowing it. There will always be the newbies from C who will misread it. (sigh)Addendum: I would be ok with making the less than/greater than operators be chainable (since those operators are nonsense when used with boolean values), but I would ask that no expression be able to mix less-than and greater-than. It would be ok to mix < with <=, but not < with >.
Feb 14 2007
Joel C. Salomon wrote:Russell Lewis wrote:I would assume the reason is because it's not used in math. --bbWhy this prejudice? With the chaining as people have discussed it, a < b > c expands to (a < b) && (b > c) — why would you prohibit this?The chain that I'm concerned about is this: a==b == c==d which (the spacing makes clear) is meant to be (a==b) == (c==d) but which could me misread as (a==b) && (b==c) && (c==d) and which is probably (I'm not sure) currently implemented by the compiler as: ((a==b) ==c) ==d IMHO, C should have allowed comparison chaining from the start, but since it didn't, I don't think that it would be a good idea to start allowing it. There will always be the newbies from C who will misread it. (sigh)Addendum: I would be ok with making the less than/greater than operators be chainable (since those operators are nonsense when used with boolean values), but I would ask that no expression be able to mix less-than and greater-than. It would be ok to mix < with <=, but not < with >.
Feb 14 2007
I’ve used it — and hairier stuff besides. ∅ ≠ A ⊆ (Ω ∋ α) ⊆ℝⁿ (No, I don’t actually expect any programming language to accept this.) “I do it all because I’m evil…” — Voltaire, “When you’re evil”With the chaining as people have discussed it, a < b > c expands to (a < b) && (b > c) — why would you prohibit this?I would assume the reason is because it's not used in math.
Feb 14 2007
"Derek Parnell" <derek nomail.afraid.org> wrote in message news:ytund437jnul$.1nx7qfyf3zj8u$.dlg 40tude.net...Second thought: Why not make it do what the coder is wanting it to do?I'd really like to be able to say if(0.5 <= x <= 1.5) ... :)
Feb 07 2007
Walter Bright wrote:Right now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)).So your first thought, the newbie's first thought and my first thought all agree: it means a<b && b<c As was pointed out already, let's make it that way. Adding these chained comparisons improves readability* and adds another popular feature from scripting languages. L. * compare with: if (a < b && c > b)
Feb 07 2007
Lionello Lunesu wrote:Walter Bright wrote:I personally would like that. It is a good example of a dwim feature that doesn't add to the complexity of the language. AndreiRight now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)).So your first thought, the newbie's first thought and my first thought all agree: it means a<b && b<c As was pointed out already, let's make it that way. Adding these chained comparisons improves readability* and adds another popular feature from scripting languages. L. * compare with: if (a < b && c > b)
Feb 07 2007
Another way to solve this problem might be the following: - Say Comparsion operators are not defined for booleans - Instead define a few new operations on booleans such as Aequivalence (for example "<->" instead of "=="). Note for example that xor (is it ^^ in D too?) already has the same meaning as != for booleans. implications whould be: - new operator symbols may have to be introduced - this change might break some code, but the compiler should spot every line thats affected by the change - it should lead to a much 'cleaner' grammar, think of the reason why its "hello " ~ "world" not "hello " + "world"! Henning
Feb 08 2007
On Thu, 8 Feb 2007 09:49:56 +0100 Henning Hasemann <hhasemann web.de> wrote:Another way to solve this problem might be the following: - Say Comparsion operators are not defined for booleans - Instead define a few new operations on booleans such as Aequivalence (for example "<->" instead of "=="). Note for example that xor (is it ^^ in D too?) already has the same meaning as != for booleans. implications whould be: - new operator symbols may have to be introduced - this change might break some code, but the compiler should spot every line thats affected by the change - it should lead to a much 'cleaner' grammar, think of the reason why its "hello " ~ "world" not "hello " + "world"!If I remember correctly this is also what eiffel does btw, AND it whould be possible to realize additionally to the chaining. see: a < b < c means (a < b) && (b < c) a == b == c == d means (a == b) && (b == c) && (c == d) if you whould want to test if a == b yields the same boolean value as c == d, i.e. (a == b) == (c == d) you would write (a == b) <-> (c == d) note how clear this reads, you see at a first glance that results of boolean expressions are compared, which, as it is not used very often can not happen by accident (as up to now in a < b < c) anymore. I'm not that sure if the <-> - Symbol is a good idea though, but Im sure it whould be possible to find alternatives to it. Henning
Feb 15 2007
Walter Bright napisał(a):Right now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.It's cool when language makes it easy not to make mistakes (maybe not as java by not letting programmer to do anything serious but still handy idea) but isn't it (on small scale but always) losing compatibility with C, which could complicate making bindings etc? Libraries creators could have used them in the right meaning for some strange reason... As if making "the real meaning". "The real meaning" can be different for each person. some would like to use a<b<c in c style, some in python, some will have own ideas. and if i understand correctly, macros will do it. every man could write his own macro to change it in any way he like. So if it will not make problems with C compatibility vote ++ with no "real meanings" --------------------------- Peter Modzelewski www.team0xf.com www.keyer.team0xf.com
Feb 08 2007
Peter Modzelewski wrote:some would like to use a<b<c in c styleFind me one person :) You may use google code-search: find one example where a<b<c is not a L.
Feb 08 2007
Walter Bright wrote:Right now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought?In C, C++ and D, the way they are now, my first thought is the programmer needs to be replaced.and that it does NOT mean ((a < b) && (b < c)).In a perfect language, the language should understand exactly what the programmer meant. I would love to be able to type thinks like: if (1 < x <= 10)... It makes a lot of sense. Python, ruby and other languages take it correctly.and even if it was intentional, it raises such a red flag that it shouldn't be used anyway.Someone doing this intentionally points to a bug in the D spec.Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c)I don't think so. Considering a, b and c of scalar type, if (a < b) returns a boolean result, it should NOT be comparable with a scalar. This is why I insist again that not having a true boolean type in D is a big minus. In my opinion: (a < b < c) -> (a < b) && (b < c) (a < b == c > d) -> (a < b) && (b == c) && (c > d) ((a < b) < c) -> ERROR: comparing non-scalar and scalar types
Feb 08 2007
Walter Bright wrote:Right now, in D (as well as C and C++), when you see the expression: if (a < b < c) what is your first thought? Mine is that it was written by a newbie who didn't realize that (a < b) returns true or false, and that it does NOT mean ((a < b) && (b < c)). The odds approach certainty that this is a logic error in the code, and even if it was intentional, it raises such a red flag that it shouldn't be used anyway. Andrei has proposed (and I agreed) that this should be done away with in the language, i.e. comparison operators should no longer be associative. It's a simple change to the grammar. If one really did want to write such code, it could be done with parentheses: if ((a < b) < c) to get the original behavior. At least, that looks intentional. I don't think this will break existing code that isn't already broken.I'd like to have if (a < b < c) ... automatically expanding to if ((a < b) && (b < c)) ... and if ((a < b) < c) raising some exception. Well, i can't give you any serious reasons for why, but i'm used to a < b < c from math. I tried to find something else that would make it possible to write something like if (b in [a,c]) where [a,b] is a compact interval (that means a <= b <= c). But this would let it not be distinguished from an interval's declaration as well as you wouldn't be able to write something like: if (b in (a,c]) // a < b <= c Perhaps someone has a nice, good looking, easy readable solution.
Feb 14 2007