Problem 16
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object Problem16:
|
||||
def dropEveryNthElement(n: Int, list: List[Any]): List[Any] =
|
||||
@tailrec
|
||||
def loop(cursor: List[Any], acc: List[Any], count: Int): List[Any] =
|
||||
cursor match
|
||||
case e :: tail if count == n =>
|
||||
loop(tail, acc, 1)
|
||||
case e :: tail =>
|
||||
loop(tail, e :: acc, count + 1)
|
||||
case Nil =>
|
||||
acc
|
||||
|
||||
loop(list, List(), 1).reverse
|
||||
Reference in New Issue
Block a user