Created
July 30, 2015 18:46
-
-
Save fancellu/9fa1df784e41596b0143 to your computer and use it in GitHub Desktop.
Drop Nth element from List alternative soltuion
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
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