Scalaコレクションメソッドメモ(Hishidama's Scala collection method Memo)
filterとmapを組み合わせたやつなので、こんな感じでfilterぽくかけるしmapぽくもかける。 Traversableなので、ListでもMapでもいける。
scala> val m = Map("name" -> "hoge", "currency" -> Some("USD")) m: scala.collection.immutable.Map[String,java.io.Serializable] = Map(name -> hoge, currency -> Some(USD)) scala> m.collect { case(k, Some(v)) => k -> v} res0: scala.collection.immutable.Map[String,Any] = Map(currency -> USD) scala> m.collect { case(k, Some(v)) => k -> v } res1: scala.collection.immutable.Map[String,Any] = Map(currency -> USD) scala> m.collect { case(k, Some(v)) => k -> v.toString.length } res5: scala.collection.immutable.Map[String,Int] = Map(currency -> 3)