Thread overview | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 07, 2004 [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
#for(x=0..10){ # printf("%d\n",x); #] It would make coding a bit faster and easier (makes D a good language to start to learn programming). Besides I don't see any dowsides. What you think? |
September 07, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to USER | In article <chl0t5$2qk9$1@digitaldaemon.com>, USER says... > >#for(x=0..10){ ># printf("%d\n",x); >#] > > >It would make coding a bit faster and easier (makes D a good language to start >to learn programming). >Besides I don't see any dowsides. >What you think? > I think it's horrible, it doesn't semantically make a lot of sense (also seeing how it is used for array splicing). Neither would you use that kind of thing that often. Also imagine something else used besides integers. It's a big no. You could make you own 'range' functions and then use foreach(int i; range(0, 10, 2)) for example. Or use some delegate and make an elegant function, repeat(&function, 10) or whatever. |
September 08, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joey Peters | >>it doesn't semantically make a lot of sense #for(x=0;x<10;x+++) versus #for(x=0..10) Pascal has something like that: for x:=0 to 10 do ... And 0..10 means 0 to 10 It is just immitating pascal a bit. I don't want to say, we need to get rid of the old for statement, I want to add a simplier way to do this. > Neither would you use that kind of thing that often. Most of my for cycles are somethibng like #for(int x=0;x<l;x++) so why not for(x=0..l) ?? 0..l means 0 to l in array slicing, so why not everywher?? >>Also imagine something else used besides integers In array slicing you can use only integers. There is no problem. It might seem strange to people who haven't used Pascal-like languages, but I think we can learn something from them too. They say, that Pascal is a very good language to start learning programming, because it is simple. Why not to make D a good languege to start learning too. In article <chl960$2vnd$1@digitaldaemon.com>, Joey Peters says... > >In article <chl0t5$2qk9$1@digitaldaemon.com>, USER says... >> >>#for(x=0..10){ >># printf("%d\n",x); >>#] >> >> >>It would make coding a bit faster and easier (makes D a good language to start >>to learn programming). >>Besides I don't see any dowsides. >>What you think? >> > >I think it's horrible, it doesn't semantically make a lot of sense (also seeing how it is used for array splicing). Neither would you use that kind of thing that often. Also imagine something else used besides integers. It's a big no. You could make you own 'range' functions and then use foreach(int i; range(0, 10, 2)) for example. Or use some delegate and make an elegant function, repeat(&function, 10) or whatever. > > |
September 08, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to USER | In article <chmcpd$g6d$1@digitaldaemon.com>, USER says... > >>>it doesn't semantically make a lot of sense >#for(x=0;x<10;x+++) >versus >#for(x=0..10) > >Pascal has something like that: for x:=0 to 10 do ... >And 0..10 means 0 to 10 >It is just immitating pascal a bit. >I don't want to say, we need to get rid of the old for statement, I want to add >a simplier way to do this. I think that at this point, it would be hard to implement this thing. Also, you/they could make some useful functions to get similar functionality. int[] range(int start, int end, int step = 1) { int[] ret; for(;start < end;start+=step) { ret.length = ret.length + 1; ret[ret.length - 1] = start; } } foreach(int x; range(0, 10)) { .. } >> Neither would you use that kind of thing that often. >Most of my for cycles are somethibng like >#for(int x=0;x<l;x++) >so why not for(x=0..l) ?? > >0..l means 0 to l in array slicing, so why not everywher?? > It would be grandiose confusing for the compiler. How would you do steps with that? x = 0..10..2? Also, I'm a big fan of using foreach over for loops, or when using things like x = 0 to l, you could aswell: while(x++!=l) { // problem solved } Seriously, in most languages, the times you use a for loop you're using it on an array in a for(x; x < array.length; x++) way, just an iteration, and now you can do that with foreach! The times you'd actually 'really' need it wouldn't be that a relevant reason to embed it, it's just typing a few more characters versus implementing a (relatively) hard to implement feature. > >>>Also imagine something else used besides integers >In array slicing you can use only integers. There is no problem. I'd figure that when you use x = 0..10 that '..' is part of some form of an expression. The key to making useful operators is making them work in all expressions. I think they made an exception on array splicing because it's such a useful feature indeed, though they're not operators, and you're using them as operators. I figure they implemented it something like this: arrayidentifier '[' expression optsplic ']'; optsplic: | '..' expression ; >It might seem strange to people who haven't used Pascal-like languages, but I think we can learn something from them too. > >They say, that Pascal is a very good language to start learning programming, >because it is simple. >Why not to make D a good languege to start learning too. > Just because it doesn't make you type out for loops easily doesn't mean it's not a good language to start with ;) > > >In article <chl960$2vnd$1@digitaldaemon.com>, Joey Peters says... >> >>In article <chl0t5$2qk9$1@digitaldaemon.com>, USER says... >>> >>>#for(x=0..10){ >>># printf("%d\n",x); >>>#] >>> >>> >>>It would make coding a bit faster and easier (makes D a good language to start >>>to learn programming). >>>Besides I don't see any dowsides. >>>What you think? >>> >> >>I think it's horrible, it doesn't semantically make a lot of sense (also seeing how it is used for array splicing). Neither would you use that kind of thing that often. Also imagine something else used besides integers. It's a big no. You could make you own 'range' functions and then use foreach(int i; range(0, 10, 2)) for example. Or use some delegate and make an elegant function, repeat(&function, 10) or whatever. >> >> > > |
September 08, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to USER | >>>it doesn't semantically make a lot of sense >#for(x=0;x<10;x+++) >versus >#for(x=0..10) > >Pascal has something like that: for x:=0 to 10 do ... >And 0..10 means 0 to 10 >It is just immitating pascal a bit. >I don't want to say, we need to get rid of the old for statement, I want to add >a simplier way to do this. > > >> Neither would you use that kind of thing that often. >Most of my for cycles are somethibng like >#for(int x=0;x<l;x++) >so why not for(x=0..l) ?? > >0..l means 0 to l in array slicing, so why not everywher?? Could you give my an example from your code that has this kind of loop, that can't be done with foreach in an as easy way, please? >>>Also imagine something else used besides integers >In array slicing you can use only integers. There is no problem. > >It might seem strange to people who haven't used Pascal-like languages, but I think we can learn something from them too. Basic-like languages have a similar but more powerfull version of what you are suggeting: FOR foo = begin TO end STEP whatever .. NEXT Step is optinal. So you don't need to and upto as in Pascal and can have other steps than 1. Anyway I don't see any use for that in D. >They say, that Pascal is a very good language to start learning programming, >because it is simple. >Why not to make D a good languege to start learning too. Hmm, thay say the same about Python. We could learn a lot from Python, but it just won't fit into D. |
September 08, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to USER | In article <chmcpd$g6d$1@digitaldaemon.com>, USER says... >0..l means 0 to l in array slicing, so why not everywher?? Actually, it doesn't. In array slicing, 0..1 means 0 to 0, not 0 to 1. Similarly, 0..10 in array slicing means elements 0 to 9. Jill |
September 08, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to USER | "USER" <USER_member@pathlink.com> wrote in message news:chmcpd$g6d$1@digitaldaemon.com... > >>it doesn't semantically make a lot of sense > #for(x=0;x<10;x+++) > versus > #for(x=0..10) > > Pascal has something like that: for x:=0 to 10 do ... > And 0..10 means 0 to 10 > It is just immitating pascal a bit. > I don't want to say, we need to get rid of the old for statement, I want to add > a simplier way to do this. > > > > Neither would you use that kind of thing that often. > Most of my for cycles are somethibng like > #for(int x=0;x<l;x++) > so why not for(x=0..l) ?? > > 0..l means 0 to l in array slicing, so why not everywher?? > > > >>Also imagine something else used besides integers > In array slicing you can use only integers. There is no problem. Not true, you can write your own opSlice for your class and it can have any arguments you like! > It might seem strange to people who haven't used Pascal-like languages, but I > think we can learn something from them too. > > They say, that Pascal is a very good language to start learning programming, > because it is simple. > Why not to make D a good languege to start learning too. > > > > > > In article <chl960$2vnd$1@digitaldaemon.com>, Joey Peters says... > > > >In article <chl0t5$2qk9$1@digitaldaemon.com>, USER says... > >> > >>#for(x=0..10){ > >># printf("%d\n",x); > >>#] > >> > >> > >>It would make coding a bit faster and easier (makes D a good language to start > >>to learn programming). > >>Besides I don't see any dowsides. > >>What you think? > >> > > > >I think it's horrible, it doesn't semantically make a lot of sense (also seeing > >how it is used for array splicing). Neither would you use that kind of thing > >that often. Also imagine something else used besides integers. It's a big no. > >You could make you own 'range' functions and then use foreach(int i; range(0, > >10, 2)) for example. Or use some delegate and make an elegant function, > >repeat(&function, 10) or whatever. > > > > > > |
September 09, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to USER | DTL provides the IntegralRange template that does exactly what you need. foreach(int i; new IntegralRange(int)(-10, 10, 2) { printf("%d ", i); } This prints "-10 -8 -6 -4 -2 0 2 4 6 8" The slight uglification is the need to "new", but that's out of my hands. ;) Check the D.dtl NG for details of where to get the latest distro. "USER" <USER_member@pathlink.com> wrote in message news:chl0t5$2qk9$1@digitaldaemon.com... > #for(x=0..10){ > # printf("%d\n",x); > #] > > > It would make coding a bit faster and easier (makes D a good language to start > to learn programming). > Besides I don't see any dowsides. > What you think? > > |
September 09, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote: > DTL provides the IntegralRange template that does exactly what you need. Need? > foreach(int i; new IntegralRange(int)(-10, 10, 2) <snip> for (int i = -10; i < 10; i += 2) is not only shorter to type, it also has much less overhead. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
September 09, 2004 Re: [suggestion] operator .. in for | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | In article <chppt1$29fr$1@digitaldaemon.com>, Stewart Gordon says... > >Matthew wrote: > >> DTL provides the IntegralRange template that does exactly what you need. > >Need? > >> foreach(int i; new IntegralRange(int)(-10, 10, 2) ><snip> > > for (int i = -10; i < 10; i += 2) > >is not only shorter to type, it also has much less overhead. that's the first impression. but there must be a usefulness the IntegralRange. What can it be? anyone care to enlighten us? Ant |
Copyright © 1999-2021 by the D Language Foundation