Logo

Programming-Idioms

  • Rust
  • JS
  • Ruby

Idiom #254 Replace value in list

Replace all exact occurrences of "foo" with "bar" in the string list x

x.map!{|el| el == "foo" ? "bar" : el}
for v in &mut x {
    if *v == "foo" {
        *v = "bar";
    }
}
x = x.map(e => e === 'foo' ? 'bar' : e);
(replace {"foo" "bar"} x)

New implementation...