Created
April 24, 2019 13:33
-
-
Save brianhsu/58037e703e165eeb10dfd0d26f5f0073 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
case class StringList(xs: List[String]) { | |
def prepend(element: String) = StringList(element :: xs) | |
} | |
val xs = List(1, 2, 3, "aa", "bb", "cc", 4, 5.5, 7, "dd", "ee", "ff", "gg", 8, 9.2, 10) | |
val r = xs.foldRight(List.empty[Any]) { | |
case (current: String, compactedList @ ((strList: StringList) :: xs)) => strList.prepend(current) :: xs | |
case (current: String, compactedList @ (_ :: xs)) => new StringList(current :: Nil) :: compactedList | |
case (current: Int, compactedList) => current :: compactedList | |
case (current, compactedList) => compactedList | |
} | |
println(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment