0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jsのreplace関数みたいに高階関数渡す感じのreplace処理をScalaで模索

0
Last updated at Posted at 2018-01-31

jsのコレっぽく

js
function replace(str) {
  var dic = { "&amp;" : "&", "&lt;" : "<", "&gt;" : ">" }
  return str.replace(/&lt;|&gt;|&amp;/g, s => dic[s])
}

Scalaでも書けないかな?とおもって模索したらそれっぽいかんじで出来たのでメモ。

scala
def replace(str: String) = {
  val dic = Map("&amp;" -> "&", "&lt;" -> "<", "&gt;" -> ">")
  dic.keys.mkString("|").r.replaceAllIn(str, m => dic(m.matched))
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?