Problem 04

This commit is contained in:
2026-03-19 15:39:13 +01:00
parent 6504948100
commit 6864ee48c7
2 changed files with 31 additions and 0 deletions
@@ -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)