Skip to content

Instantly share code, notes, and snippets.

@dholbrook
Created January 5, 2012 06:21
Show Gist options
  • Save dholbrook/1563997 to your computer and use it in GitHub Desktop.
Save dholbrook/1563997 to your computer and use it in GitHub Desktop.
S99 Problem 8
scala> //Eliminate consecutive duplicates of list elements.
scala> val l = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)
l: List[Symbol] = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)
scala> l.foldLeft(List[Symbol]()){
| case (acc,s) if acc.isEmpty => s :: acc
| case (acc,s) if s == acc.head => acc
| case (acc,s) => s :: acc
| }.reverse
res10: List[Symbol] = List('a, 'b, 'c, 'a, 'd, 'e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment