scalaでfilter&existsする時のアレ
事故りました。というか盛大な勘違い
あるリストに含まれていないものに絞りたい
1,2,3,4,5から1,2,3に属さないものが欲しいとする
diffを使えば終了
元のデータ型と一致していない場合は使えない。のでやってみる
まぁexistsとfilterで解決かな
scala> List(1,2,3).exists(_ != 1) res74: Boolean = true
わかる。(2 | 3) == 1 は true である。じゃあfilterと組み合わせて
scala> List(1,2,3,4,5).filter(x => List(1,2,3).exists(_ != x)) res76: List[Int] = List(1, 2, 3, 4, 5)
あっれっ...どうしてこうなった
追ってみる
scala> List(1,2,3).exists(_ == 1) res75: Boolean = true
わかる。1 == 1 は trueである
scala> List(1,2,3,4,5).filter(x => List(1,2,3).exists(_ == x)) res77: List[Int] = List(1, 2, 3)
うん。ここらへんで否定すべき場所はexists内ではなくfilterであることに気がつく
scala> List(1,2,3,4,5).filter(x => !List(1,2,3).exists(_ == x)) res78: List[Int] = List(4, 5)
すっきりした