substitute

Substitute in parallel all elements in r which equal (according to pred) ss[2*n] with ss[2*n + 1] for n = 0, 1, 2, ....

  1. template substitute(substs...)
  2. auto substitute(R r, Ss ss)
    substitute
    (
    alias pred =
    (
    a
    ,
    b
    )
    => a == b
    R
    Ss...
    )
    (
    R r
    ,
    Ss ss
    )
    if (
    isInputRange!(Unqual!R) &&
    Ss.length >= 2
    &&
    &&
    haveCommonType!(ElementType!R, Ss)
    )
  3. template substitute(ss...)
  4. auto substitute(R r, Ss ss)

Examples

import std.conv : to;
import std.algorithm : map;
auto x = `42`.substitute('2', '3')
             .substitute('3', '1');
static assert(is(ElementType!(typeof(x)) == dchar));
assert(equal(x, `41`));
assert(`do_it`.substitute('_', ' ')
              .equal(`do it`));
int[3] x = [1, 2, 3];
auto y = x[].substitute(1, 0.1)
            .substitute(0.1, 0.2);
static assert(is(typeof(y.front) == double));
assert(y.equal([0.2, 2, 3]));
assert(`do_it`.substitute('_', ' ',
                          'd', 'g',
                          'i', 't',
                          't', 'o')
              .equal(`go to`));
import std.range : retro;
assert(equal([1, 2, 3].substitute(1, 0.1)
                      .retro,
             [3, 2, 0.1]));

Meta