Problem 18
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object Problem18:
|
||||
def slice(i: Int, k: Int, list: List[Any]): List[Any] =
|
||||
@tailrec
|
||||
def loop(cursor: List[Any], acc: List[Any], index: Int): List[Any] =
|
||||
cursor match
|
||||
case e :: tail if (i <= index && index < k) =>
|
||||
loop(tail, e :: acc, index + 1)
|
||||
case e :: tail =>
|
||||
loop(tail, acc, index + 1)
|
||||
case Nil =>
|
||||
acc
|
||||
|
||||
loop(list, List(), 0).reverse
|
||||
Reference in New Issue
Block a user