www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Lazy generic std.algorithm.replace()

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Is there a reason why std.algorithm doesn't provide a lazy range 
implementation of replace()?

I'm aware this isn't difficult to express using `map` like, for 
instance,

auto replace(S, F, T)(S, F from, T to)
{
     return s.map!(a => a == from ? to : a);
}

but I prefer syntactic sugars.

While at it we could make use of variadics to enable syntax such 
as

     assert(equal("do_it".replace('_', ' ',
                                  'd', 'g',
                                  'i', 't',
                                  't', 'o',
            "go to");

along with optional predicate (as usual).

What do you say?
Oct 23 2015
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Friday, 23 October 2015 at 11:23:55 UTC, Nordlöw wrote:
 Is there a reason why std.algorithm doesn't provide a lazy 
 range implementation of replace()?
Sample implementation at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1939 for one replacement pair.
Oct 23 2015
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Friday, 23 October 2015 at 11:53:06 UTC, Nordlöw wrote:
 https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1939

 for one replacement pair.
Made it variadic at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1950
Oct 23 2015
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Friday, 23 October 2015 at 21:44:28 UTC, Nordlöw wrote:
 On Friday, 23 October 2015 at 11:53:06 UTC, Nordlöw wrote:
 https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1939

 for one replacement pair.
Made it variadic at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1950
Added hashed based version at https://github.com/nordlow/justd/blob/master/algorithm_ex.d#L1999
Oct 23 2015