RFC: Lazy variadic replace() that reuses find()

Nordlöw via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 7 07:29:06 PST 2015


I'm building a reference solution to a lazy variadic 
implementation of `replace`

https://github.com/nordlow/justd/blob/master/replacing.d

which, when ready, I plan to propose to Phobos' std.algorithm.

I'm already done with the easier run-time and compile-time 
variadic overloads for the case when all the replacements are 
`ElementTypes` of the haystack Range at

https://github.com/nordlow/justd/blob/master/replacing.d#L15

and

https://github.com/nordlow/justd/blob/master/replacing.d#L74

The more generic version where the replacements are of the same 
type as `haystack` is more complicated. For it, I plan to reuse 
variadic std.algorithm.searching.find in the following three 
steps:

1. Implement a new range `findSplitter`, that uses find() to 
split up the haystack into a range of ranges RoR like follows 
where each element is the results of a  call to find:

`xx_yy_zz`.findSplitter(`11`, `22`) => [(1, `xx`), (0, `_yy_`), 
(2, `zz`)]

2. Apply the existing replace overloads on RoR, resulting in

[`11`, `_yy_`, `22`]

3. Finally, merge them with joiner()

That, together in sequence, defines the most generic version of 
`find()`.

Is this a good approach?


More information about the Digitalmars-d-learn mailing list