Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 02, 2013 [Issue 11415] New: Assign range to array | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=11415 Summary: Assign range to array Product: D Version: unspecified Platform: All OS/Version: All Status: NEW Severity: normal Priority: P2 Component: druntime AssignedTo: nobody@puremagic.com ReportedBy: daniel350@bigpond.com --- Comment #0 from daniel350@bigpond.com 2013-11-01 18:28:20 PDT --- I would have expected the following to work: int[] d = [1,2,3,4,5,6,7]; d[] = d.filter!(x => x > 3)[]; Where the rhs could have been assigned to the lhs. Unfortunately this gives the following: Error: cannot implicitly convert expression (f.opSlice()) of type FilterResult!(__lambda2, int[]) to int[] For now, the only idiomatic solution I could find is this roundabout way: auto t = d.filter!(x => x > 3).copy(d); d = d[0 .. $ - t.length]; -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 Shammah Chancellor <shammah.chancellor@gmail.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |shammah.chancellor@gmail.co | |m --- Comment #1 from Shammah Chancellor <shammah.chancellor@gmail.com> 2013-11-01 18:32:58 PDT --- Seconded. If an operator implements opSlice it should behave as expected for a r-value per the Array assignment documentation. This can be converted to a similar foreach() loop that arrays are. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 yazan.dabain@gmail.com changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |yazan.dabain@gmail.com --- Comment #2 from yazan.dabain@gmail.com 2013-11-02 02:09:07 PDT --- Filter produces a lazy range. In other words, it is not an int array. To store the result of filter in d[] you need to eagerly evaluate it. To do that you can use std.array.array on the result of filter. The code becomes: int[] d = [1,2,3,4,5,6,7]; d = d.filter!(x => x > 3).array(); If this answers your question, please close the bug. http://dlang.org/phobos/std_array.html#.array -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 monarchdodra@gmail.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED CC| |monarchdodra@gmail.com Resolution| |INVALID --- Comment #3 from monarchdodra@gmail.com 2013-11-02 02:44:19 PDT --- (In reply to comment #2) > Filter produces a lazy range. In other words, it is not an int array. To store > the result of filter in d[] you need to eagerly evaluate it. To do that you can > use std.array.array on the result of filter. > The code becomes: > > int[] d = [1,2,3,4,5,6,7]; > d = d.filter!(x => x > 3).array(); > > If this answers your question, please close the bug. > > http://dlang.org/phobos/std_array.html#.array Yes. Do note though that this will allocate a new array, and not *copy* filter "into" the "d" array. Not that it's wrong, I just want to highlight it, as I don't think its quite what the op wanted. If you want to assign the *contents*, then std.algorithm.copy will do what you want: http://dlang.org/phobos/std_algorithm.html#copy int[] d = [1,2,3,4,5,6,7]; d.filter!(x => x > 3)().copy(d); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 --- Comment #4 from monarchdodra@gmail.com 2013-11-02 02:46:09 PDT --- (In reply to comment #0) > I would have expected the following to work: > > int[] d = [1,2,3,4,5,6,7]; > d[] = d.filter!(x => x > 3)[]; I'm surprised filter has opSlice() at all: It's not a range primitive, and is useless. If anything, it leads to error (as you just tried to use it) > Error: cannot implicitly convert expression (f.opSlice()) of type > FilterResult!(__lambda2, int[]) to int[] opSlice should be removed from filter, or any other range. It only makes sense for containers (including static arrays), or plain dynamic arrays. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 bearophile_hugs@eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |bearophile_hugs@eml.cc --- Comment #5 from bearophile_hugs@eml.cc 2013-11-02 03:02:45 PDT --- See also Issue 10176 for an array append of a lazy range. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 --- Comment #6 from yazan.dabain@gmail.com 2013-11-02 03:18:54 PDT --- @monarchdodra, that's true, however even this usage of copy is wrong as d should change length. Printing d after the copy produces: [4, 5, 6, 7, 5, 6, 7] where it is meant to be [4, 5, 6, 7] So you'll either have to go with what daniel350 had in his first post, or with the allocating array call. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 --- Comment #7 from monarchdodra@gmail.com 2013-11-02 04:36:18 PDT --- (In reply to comment #6) > @monarchdodra, that's true, however even this usage of copy is wrong as d should change length. > > Printing d after the copy produces: [4, 5, 6, 7, 5, 6, 7] where it is meant to be [4, 5, 6, 7] > > So you'll either have to go with what daniel350 had in his first post, or with the allocating array call. Huh. I didn't even notice OP had gotten it right in his original post. Serves me for not fully reading. I guess it really boils down to if you want a new array, or if you want to overwrite existing data. For example, copy can be used to write inside a slice taken from a stack allocated static array. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 02, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 --- Comment #8 from Shammah Chancellor <shammah.chancellor@gmail.com> 2013-11-02 16:47:07 PDT --- The point is that the syntax is listed in http://dlang.org/arrays.html. This syntax should be extended to forward ranges since that is how one would *EXPECT* it to work. Either that or the array vector copy syntax should be abandoned. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
November 03, 2013 [Issue 11415] Assign range to array | ||||
---|---|---|---|---|
| ||||
Posted in reply to daniel350@bigpond.com | http://d.puremagic.com/issues/show_bug.cgi?id=11415 --- Comment #9 from monarchdodra@gmail.com 2013-11-02 17:21:22 PDT --- (In reply to comment #8) > The point is that the syntax is listed in http://dlang.org/arrays.html. This syntax should be extended to forward ranges since that is how one would *EXPECT* it to work. Either that or the array vector copy syntax should be abandoned. The array vector copy is more than just convenient syntax: It's a request for a vectorized operation, which is built into naked arrays. If this syntax did work, you'd *EXPECT* a vector operation too, but you'd be getting a plain foreach copy, which would be just as wrong, but less explicitly so. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation