Thread overview | |||||
---|---|---|---|---|---|
|
April 10, 2014 Why does filter return const? | ||||
---|---|---|---|---|
| ||||
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 |
April 10, 2014 Re: Why does filter return const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | 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
|
April 10, 2014 Re: Why does filter return const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | 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?
|
Copyright © 1999-2021 by the D Language Foundation