// input
val xs = (1, "aaa")::(1, "bbb")::(1, "ccc")::(2, "abc")::(3, "xxx")::(3, "kkk")::(1, "abcde")::(1, "qwee")::Nil
// output
val expect = List((1, "aaa"), (2, "abc"), (3, "xxx"), (1, "abcde"))
// fold
val resultFold = xs.foldLeft(List[(Int, String)]()){
case (Nil, t) => List(t)
case (ys@((i, s)::_), t@(j, q)) => if(i == j) ys else t::ys
}.reverse
assert(expect == resultFold)
// invert
val resultInvert = xs.head :: (xs, xs.tail).invert.collect{ case ((i, x), (j, y)) if i != j => (j, y) }
assert(expect == resultInvert )
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
00