digitalmars.D.learn - Why does filter return const?
- Edwin van Leeuwen (6/6) Apr 10 2014 I'd like to filter a range and then change the filtered results,
- bearophile (7/12) Apr 10 2014 Mixing functional-style programming with mutation is a source of
- monarch_dodra (16/22) Apr 10 2014 It doesn't. It returns front, and by Ref if at all possible too.
I'd like to filter a range and then change the filtered results, but FilterResult returns a const. Is there any (straightforward) way of doing what I want? I guess I could create duplicates, but I would like my changes also to be reflected in the original range. Cheers, Edwin
Apr 10 2014
Edwin van Leeuwen:I'd like to filter a range and then change the filtered results, but FilterResult returns a const. Is there any (straightforward) way of doing what I want? I guess I could create duplicates, but I would like my changes also to be reflected in the original range.Mixing functional-style programming with mutation is a source of troubles. If you want to do that it's safer and more clear to use normal imperative code. (There is also the copy() function, but I think it doesn't help you in this case). Bye, bearophile
Apr 10 2014
On Thursday, 10 April 2014 at 10:30:19 UTC, Edwin van Leeuwen wrote:I'd like to filter a range and then change the filtered results, but FilterResult returns a const. Is there any (straightforward) way of doing what I want? I guess I could create duplicates, but I would like my changes also to be reflected in the original range. Cheers, EdwinIt doesn't. It returns front, and by Ref if at all possible too. //---- void main() { auto arr = iota(0, 10).array(); foreach (ref e; arr.filter!"a%2"()) --e; writeln(arr); //duplicate evens } //---- [0, 0, 2, 2, 4, 4, 6, 6, 8, 8] //---- There must be something else that is wrong. What are you trying to do?
Apr 10 2014