Skip to content

Instantly share code, notes, and snippets.

@brianhsu
Created April 24, 2019 13:33
Show Gist options
  • Save brianhsu/58037e703e165eeb10dfd0d26f5f0073 to your computer and use it in GitHub Desktop.
Save brianhsu/58037e703e165eeb10dfd0d26f5f0073 to your computer and use it in GitHub Desktop.
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