Range golf challenge: apply predicate to a subrange, returning the original range modified.

H. S. Teoh hsteoh at qfbox.info
Sat Oct 8 21:10:28 UTC 2022


On Sat, Oct 08, 2022 at 12:45:29PM -0700, H. S. Teoh via Digitalmars-d wrote:
[...]
> Here's a sketch of my idea:
> - Pair the range with an index using .indexed, pass that through .select
>   to get the filtered range.
>    - Use .map to map it to the modified elements.
>    - Furthermore, tag each element with a secondary index of 0
>      (explained below).
> - Pair the original range with .indexed, plus a secondary index of 1.
> - Merge the two ranges together so that the result is lexicographically
>   sorted according the indexing order (i.e., filtered elements will
>   appear in the right positions in the original range) and the secondary
>   index (i.e., replaced elements appear first in the resulting range).

Merge, i.e., with std.algorithm.multiwayMerge.


> - Pass the merged range to .uniq to replace the filtered values with
>   their modified counterparts, with uniqueness predicate defined by the
>   (first) index. This effectively drops elements whose secondary index
>   is 1 if they have a replacement, otherwise they are left untouched.
> - Unwrap the elements to drop the indices to obtain the final result.
[...]


T

-- 
Do not reason with the unreasonable; you lose by definition.


More information about the Digitalmars-d mailing list