Problem 04
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
package ninetyNineScalaProblems
|
||||||
|
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
|
object Problem04:
|
||||||
|
def listLength(list: List[Any]): Int =
|
||||||
|
@tailrec
|
||||||
|
def loop(cursor: List[Any], acc: Int): Int =
|
||||||
|
cursor match
|
||||||
|
case head :: tail =>
|
||||||
|
loop(tail, acc + 1)
|
||||||
|
case Nil =>
|
||||||
|
acc
|
||||||
|
|
||||||
|
loop(list, 0)
|
||||||
|
|
||||||
|
object Problem04Stdlib:
|
||||||
|
def listLength(list: List[Any]): Int =
|
||||||
|
list.length
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package ninetyNineScalaProblems
|
||||||
|
|
||||||
|
class Problem04Suite extends munit.FunSuite:
|
||||||
|
List(
|
||||||
|
(List(1, 2, 3, 4, 5), 5),
|
||||||
|
(List(1), 1),
|
||||||
|
(List(), 0)
|
||||||
|
).foreach:
|
||||||
|
case (list, expected) =>
|
||||||
|
test(s"listLength returns [$expected]"):
|
||||||
|
assertEquals(Problem04.listLength(list), expected)
|
||||||
|
assertEquals(Problem04Stdlib.listLength(list), expected)
|
||||||
Reference in New Issue
Block a user