by shigemk2

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

collectというか、パターンマッチの謎挙動

scala> val apple = "apple"
apple: String = apple

scala> val Apple = "Apple"
Apple: String = Apple

scala> Seq("apple", "orange").collect {case apple => "got an apple" }
res0: Seq[String] = List(got an apple, got an apple)

scala> Seq("apple", "orange").collect {case Apple => "got an apple"  }
res1: Seq[String] = List()

scala> Seq("apple", "orange").collect {case "apple" => "got an apple"  }
res2: Seq[String] = List(got an apple)

最初の挙動がよくわからないけど、

collectのソースコードはこんな感じっぽい

  def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
    val b = bf(repr)
    foreach(pf.runWith(b += _))
    b.result
  }

もしかして、既にappleの中身がパースされてくっついているとかそういう落ちだろうか。最初のcollectの挙動が良くわからない。