Skip to content

Instantly share code, notes, and snippets.

@letronje
Last active July 2, 2017 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save letronje/ef24f523c1e9689d9a51fe0c5f83797f to your computer and use it in GitHub Desktop.
Save letronje/ef24f523c1e9689d9a51fe0c5f83797f to your computer and use it in GitHub Desktop.
object App{
def main(args: Array[String]): Unit ={
val lists = List(
Nil,
List(),
List(1),
List(1,2),
List(1,2,3),
List(1,1),
List(1,1,1),
List(1, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 7, 8, 8, 8, 8, 8, 9, 0),
List("a", "a", "a", "b", "c", "c", "c")
)
for(list <- lists){
val deduped = dedup(list)
val reversed = reverse(list)
println(s"orig: $list, reversed: $reversed, deduped: $deduped")
println ("\n\n")
}
}
def reverse[A](list: List[A]): List[A] = {
list match {
case first :: rest => reverse(rest) :+ first
//case first :: rest => reverse(rest) ::: List(first)
//case first :: rest => reverse(rest) ++ List(first)
case Nil => Nil
}
}
def dedup[A](list: List[A]): List[A] = {
list match {
case first :: second :: rest if first == second => dedup(second :: rest)
case first :: rest => first :: dedup(rest)
case Nil => Nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment