std.algorithm's remove

Brad Anderson eco at gnuk.net
Sat Aug 24 19:47:01 PDT 2013


On Sunday, 25 August 2013 at 02:24:30 UTC, maarten van damme 
wrote:
> hello,
>
> I'm a hobyist-programmer and around where I live there's a 
> group of haskell
> fanatics. They posted solutions to a recent programming 
> challenge which I
> find to be a bit ugly. For fun I wanted to implement it in d 
> and a rough
> version (not correct yet, this was written/hacked in 5 minutes 
> after
> reading the exercise)
>
> My rough version is posted here : http://dpaste.dzfl.pl/4b5a6578
>
> if you look at the output, you'll see this particular line :
> "omkom -> komkom because of : kom momkom momkom -> momkomm"
>
> This is because of what remove from std.algorithm does. It not 
> only returns
> a range with that element removed (as the name implies), it 
> also modifies
> the original range.
> I assume this decision was made for efficiency purposes but 
> that is one of
> the most ugliest things I have ever come across. At least c# 
> forces the
> 'ref' in it's parameters so you know something's up. Is there 
> any way I
> could've known this? (apart from reading the documentation on 
> every single
> trivial function in the std library?)

It was done that way intentionally because the purpose of remove 
is to remove from the source range.  If you don't want to affect 
the source range use filter.

I suspect you could trace remove's lineage back to C++ STL's 
remove which works similarly (but is significantly clunkier and 
harder to use).


More information about the Digitalmars-d-learn mailing list