Greg
Greg

Reputation: 41

Replacing every occurrence of some element by some other element

What is the best Scala way to replace from some list every occurrence of element x by some other element y? This is what I am doing right now:

list map { 
  case `x` => y
  case a => a
}

Is there more concise way available? Thanks.

Upvotes: 4

Views: 3166

Answers (3)

Aaron Novstrup
Aaron Novstrup

Reputation: 21017

If you need to do this a lot, you might write a utility function:

def replace[T](x: T, y: T) = (i: T) => if (i == x) y else i

This would allow you to write

list map replace(x, y)

Or, for infix syntax:

class ReplaceWith[T](x: T) {
   def replaceWith(y: T) = (i: T) => if (i == x) y else i
}
object ReplaceWith {
   implicit def any2replaceWith[T](x: T) = new ReplaceWith(x)
}

// now you can write
list map (x replaceWith y)

Another solution is to use a Map:

list map Map(x -> y).withDefault(identity)

With a utility function:

scala> def replace[T](pairs: (T, T)*) = Map(pairs: _*).withDefault(identity)
replace: [T](pairs: (T, T)*)scala.collection.immutable.Map[T,T]

scala> List(1,2,3) map replace(1 -> -1, 3 -> 4)
res0: List[Int] = List(-1, 2, 4)

Upvotes: 7

DarrenWang
DarrenWang

Reputation: 478

list.map(i => if (i==x) y else i)

how about this?

Upvotes: 9

incrop
incrop

Reputation: 2738

You can create custom method for replacing:

class RichIterable[E] (col: Iterable[E]) {
    def replace(pairs: (E, E)*): Iterable[E] = col map {
        a => pairs.find(_._1 == a) match {
            case None => a
            case Some(p) => p._2
        }
    }
}

object RichIterable {
    implicit def iterable2RichIterable[A](col: Iterable[A]) = 
        new RichIterable(col)
}

Replacing elements should then be easy:

scala> import RichIterable._
import RichIterable._
scala> List(1, 2, 3, 4, 5, 4, 3, 4, 7).replace(3 -> 30, 4 -> 40)
res1: Iterable[Int] = List(1, 2, 30, 40, 5, 40, 30, 40, 7)

Upvotes: 2

Related Questions