by shigemk2

当面は技術的なことしか書かない

zipとかtoMapとか

zipで作ったタプルリストでtoMapするなど。

scala> val a = List("hoge", "fuga", "bar")
a: List[String] = List(hoge, fuga, bar)

scala> val b = List("1", "2", "3")
b: List[String] = List(1, 2, 3)

scala> a zip b
res0: List[(String, String)] = List((hoge,1), (fuga,2), (bar,3))

scala> a zip b toMap
warning: there was one feature warning; re-run with -feature for details
res1: scala.collection.immutable.Map[String,String] = Map(hoge -> 1, fuga -> 2, bar -> 3)

scala> List('a,'b,'c).zipWithIndex
res2: List[(Symbol, Int)] = List(('a,0), ('b,1), ('c,2))

scala> List('a,'b,'c).zipWithIndex toMap
warning: there was one feature warning; re-run with -feature for details
res3: scala.collection.immutable.Map[Symbol,Int] = Map('a -> 0, 'b -> 1, 'c -> 2)

Scalaコレクションメソッドメモ(Hishidama's Scala collection method Memo)