www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does filter return const?

reply "Edwin van Leeuwen" <edder tkwsping.nl> writes:
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
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
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, Edwin
It 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