May 06, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote: > "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message > news:c7d6dp$1o8d$1@digitaldaemon.com... <snip> >>Not _the_ expression, but _an_ expression. That expression is b[i], >>where b remains the _result_ of the expression in the code. > > I'm sorry but my English is not that great and i didn't undesrtand what > areyou trying > to say. Maybe that wasn't the best way of putting it. Suppose you have a function that returns an array. Then you could do qwert[] = yuiop(); or equivalently qwert[] = yuiop()[]; The function yuiop is evaluated once. Each element of the result is then taken and put into the corresponding element of qwert. Similarly, with arithmetic c[] = a[] + b[]; the addition is done once across the array, and then the array elements are put into c. In fact, these are all equivalent: c[] = a + b; c[] = a[] + b; c[] = a + b[]; c[] = a[] + b[]; c[] = (a + b)[]; > It would be nice if there was a syntax to ensure that the expression on the > right is > evaluated every time. > > For example > int[] x = new int[30]; > int i=0; > x[!] = i++; > > This would fill x with numbers form 0 to 29 what isn't currently possible > this easy. I'm not sure about this. One might look at this and think "this isn't in a loop, so i will be incremented once". Especially before they've discovered what [!] means. Rewriting the left-hand side to change the behaviour of the right-hand side seems a little odd. Moreover, if the assignment is in the middle of an expression, or you have [!]s chained together, it could get really confusing. > I was also thinking (when we get array operations one day) that it would be > great to have access to the index used in for. > > i could then write > x[] = index(0) * 3; > filling x with 0, 3,6... Hmm ... I don't know how tricky such a feature would be. > another thing i tried to do today is: > int[][][] x; > x = new int[][][10]; > x[] = new int[][30]; //this doesn't work the expected way > // maybe x[!] = new int[][30]; > > but this isn't even possible: > > x[][] = new int[15]; // x[!][!] = new int[15]; would also be nice I don't know if something like x = new int[15][20][30]; would work. Maybe I'll try it when I get home.... <snip> > But if we could do even: > x[!][!][!] = index(0)+index(1)+index(2); > > that would be a very easy way to fill arrays with some pattern. <snip> Maybe there are some APL hackers around, who'd like to be able to just about anything with a one-liner.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
May 06, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | "Vathix" <vathixSpamFix@dprogramming.com> wrote in message news:c7dq7l$2qt9$1@digitaldaemon.com... > In article <c7dp1g$2pv9$1@digitaldaemon.com>, ivan.senji@public.srce.hr says... > > > >Here goes another strange thing. > >int[][][] x = new int[][][10]; > >x[] = new int[][20]; //but not with the expected meaning. > >x[][] = new int[][21]; //possible > >x[][][] = new int[][22]; //also possible > >x[][][][] = new int[][23]; //still possible... > > > >type of x is int[][][] > >type of x[]is int[][] > >type of x[][] is int[][] > >type of x[][][] is int[][] > > > > > > x[] is shorthand for x[0 .. x.length]. > So there's nothing wrong with x[0 .. x.length][0 .. x.length][0 .. x.length] > nor with x[][][]. It's just re-slicing the same memory. Makes sence! Thanks. But it would be nice to have moredimensional slices. > > > -- > Christopher E. Miller > |
May 06, 2004 Re: array operations + some strange things | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message news:c7e1kp$5d5$1@digitaldaemon.com... > Ivan Senji wrote: > > > "Stewart Gordon" <smjg_1998@yahoo.com> wrote in message news:c7d6dp$1o8d$1@digitaldaemon.com... > <snip> > >>Not _the_ expression, but _an_ expression. That expression is b[i], where b remains the _result_ of the expression in the code. > > > > I'm sorry but my English is not that great and i didn't undesrtand what > > areyou trying > > to say. > > Maybe that wasn't the best way of putting it. > > Suppose you have a function that returns an array. Then you could do > > qwert[] = yuiop(); > > or equivalently > > qwert[] = yuiop()[]; > > The function yuiop is evaluated once. Each element of the result is then taken and put into the corresponding element of qwert. > > Similarly, with arithmetic > > c[] = a[] + b[]; > > the addition is done once across the array, and then the array elements are put into c. > > In fact, these are all equivalent: > > c[] = a + b; > c[] = a[] + b; > c[] = a + b[]; > c[] = a[] + b[]; > c[] = (a + b)[]; > > > It would be nice if there was a syntax to ensure that the expression on the > > right is > > evaluated every time. > > > > For example > > int[] x = new int[30]; > > int i=0; > > x[!] = i++; > > > > This would fill x with numbers form 0 to 29 what isn't currently possible > > this easy. > > I'm not sure about this. One might look at this and think "this isn't in a loop, so i will be incremented once". Especially before they've discovered what [!] means. Rewriting the left-hand side to change the behaviour of the right-hand side seems a little odd. Moreover, if the assignment is in the middle of an expression, or you have [!]s chained together, it could get really confusing. You are probbably right. It is good to ask stupid question because that way you can get good answers :) > > I was also thinking (when we get array operations one day) that it would be > > great to have access to the index used in for. > > > > i could then write > > x[] = index(0) * 3; > > filling x with 0, 3,6... > > Hmm ... I don't know how tricky such a feature would be. > > > another thing i tried to do today is: > > int[][][] x; > > x = new int[][][10]; > > x[] = new int[][30]; //this doesn't work the expected way > > // maybe x[!] = new int[][30]; > > > > but this isn't even possible: > > > > x[][] = new int[15]; // x[!][!] = new int[15]; would also be nice > > I don't know if something like > > x = new int[15][20][30]; > > would work. Maybe I'll try it when I get home.... This won't work. The most you can do is x = new int[][][30]; I tried it also in c# where it is also imposible for jagged arrays but is possible for rectangular arrays. > <snip> > > But if we could do even: > > x[!][!][!] = index(0)+index(1)+index(2); > > > > that would be a very easy way to fill arrays with some pattern. > <snip> > > Maybe there are some APL hackers around, who'd like to be able to just about anything with a one-liner.... > > Stewart. > > -- > My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
Copyright © 1999-2021 by the D Language Foundation