scala - How to avoid using a var when doing multiple string replacements in a string -
this tried:
def dostringreplcements(originalstr: string, replacementsmap: map[string,string]): string = { var newstr = originalstr replacementsmap.foreach { pair => newstr = newstr.replaceallliterally(pair._1, pair._2) } newstr }
but functional programming style recommends avoiding vars how do vals?
consider using foldleft
function on map.
e.g.
replacementsmap.foldleft(originalstr){ case (accumulator, (target, replacement)) => accumulator.replaceallliterally(target, replacement) }
Comments
Post a Comment