www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - RFC: Lazy variadic replace() that reuses find()

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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?
Nov 07 2015
parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Saturday, 7 November 2015 at 15:29:08 UTC, Nordlöw wrote:
 `xx_yy_zz`.findSplitter(`11`, ...
should be `xx_yy_zz`.findSplitter("xx", "zz")
Nov 07 2015