by shigemk2

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

Scalaでdiffコマンドを使ってみたい その2

www.shigemk2.com

の続き。なんとなく。startsWithとかmapなどを使ってみたかった。使ってみたかっただけ。

scala> val a = "diff a.txt b.txt".lineStream_!
a: Stream[String] = Stream(1c1, ?)

scala> a
res3: Stream[String] = Stream(1c1, ?)

scala> a.toList
res4: List[String] = List(1c1, < ddd, ---, > ccc, 11158292d11158291, < eee)

scala> a.filter(x => x startsWith "<")
res5: scala.collection.immutable.Stream[String] = Stream(< ddd, ?)

scala> a.filter(x => x startsWith "<").toList
res6: List[String] = List(< ddd, < eee)

scala> a.filter(x => x startsWith "<").map(x => x.replaceAll("< ", "")).toList
res7: List[String] = List(ddd, eee)

scala> val a = "diff --suppress-common-lines -y a.txt b.txt".lineStream_!
a: Stream[String] = Stream(ddd                                                        | ccc, ?)

Scalaのちょっとしたやり取り MapやListに追加とか差分とか

ListにListでマイナスはできないっぽい。

逆にMapにプラスは出来る。

複数のListの差分をとりたいときはdiffを使う。

scala> List(1,2,3) - List(2,3)
<console>:11: error: value - is not a member of List[Int]
       List(1,2,3) - List(2,3)
                   ^

scala> val a = Map("hoge" -> 1)
a: scala.collection.immutable.Map[String,Int] = Map(hoge -> 1)

scala> a + ("fuga" -> 2)
res1: scala.collection.immutable.Map[String,Int] = Map(hoge -> 1, fuga -> 2)

scala> List(1,2,3,4) diff List(1)
res2: List[Int] = List(2, 3, 4)