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`));
1 assert(`do_it`.substitute('_', ' ')
2               .equal(`do it`));
3 int[3] x = [1, 2, 3];
4 auto y = x[].substitute(1, 0.1)
5             .substitute(0.1, 0.2);
6 static assert(is(typeof(y.front) == double));
7 assert(y.equal([0.2, 2, 3]));
8 assert(`do_it`.substitute('_', ' ',
9                           'd', 'g',
10                           'i', 't',
11                           't', 'o')
12               .equal(`go to`));
13 import std.range : retro;
14 assert(equal([1, 2, 3].substitute(1, 0.1)
15                       .retro,
16              [3, 2, 0.1]));

Meta