Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created July 30, 2015 18:46
Show Gist options
  • Save fancellu/9fa1df784e41596b0143 to your computer and use it in GitHub Desktop.
Save fancellu/9fa1df784e41596b0143 to your computer and use it in GitHub Desktop.
Drop Nth element from List alternative soltuion
http://aperiodic.net/phil/scala/s-99/p16.scala
Mine
def dropNth[A](n: Int, ls: List[A])=
ls.grouped(n).flatMap(_.take(n-1)).toList
---------
val li=List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)
dropNth(1,li) //> res1: List[Symbol] = List()
dropNth(99,li) //> res2: List[Symbol] = List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k)
dropNth(2,li) //> res2: List[Symbol] = List('a, 'c, 'e, 'g, 'i, 'k)
dropNth(3,li) //> res3: List[Symbol] = List('a, 'b, 'd, 'e, 'g, 'h, 'j, 'k)
dropNth(3,List('a)) //> res4: List[Symbol] = List('a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment