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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -