Last active
August 29, 2015 14:10
-
-
Save gakuzzzz/ff01e878da8f98a3e645 to your computer and use it in GitHub Desktop.
文字列比較
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@annotation.tailrec | |
def diff(s1: Seq[Char], s2: Seq[Char], acc: StringBuilder = new StringBuilder()): String = { | |
(s1, s2) match { | |
case (Nil , Nil) => acc.toString | |
case (x +: _ , Nil) => acc.append(s"[$x][$$END$$]").toString | |
case (Nil , y +: _) => acc.append(s"[$$END$$][$y]").toString | |
case (x +: xs, y +: ys) if x == y => diff(xs, ys, acc.append(x)) | |
case (x +: _ , y +: _) => acc.append(s"[$x][$y]").toString | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> diff("abc", "abcd") | |
res0: String = abc[$END$][d] | |
scala> diff("abcd", "abce") | |
res1: String = abc[d][e] | |
scala> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
バグ修正&ちょっとだけ整理……SMLかぶれ色出てるかも