Problem 01
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
/.bsp/
|
||||
target/
|
||||
.bloop
|
||||
.metals
|
||||
.vscode
|
||||
/project
|
||||
@@ -0,0 +1,17 @@
|
||||
import Dependencies._
|
||||
|
||||
ThisBuild / scalaVersion := "3.8.2"
|
||||
ThisBuild / version := "0.1.0-SNAPSHOT"
|
||||
ThisBuild / organization := "com.example"
|
||||
ThisBuild / organizationName := "example"
|
||||
|
||||
lazy val root = (project in file("."))
|
||||
.settings(
|
||||
name := "ninetyNineScalaProblems",
|
||||
libraryDependencies += munit % Test
|
||||
)
|
||||
|
||||
// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.
|
||||
|
||||
|
||||
libraryDependencies += "org.scalameta" %% "munit" % "1.0.4" % Test
|
||||
@@ -0,0 +1,13 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
object Problem01:
|
||||
def lastElement(list: List[Any]): Option[Any] =
|
||||
list match
|
||||
case Nil => None
|
||||
case _ => Some(list(list.length - 1))
|
||||
|
||||
object Problem01Stdlib:
|
||||
def lastElement(list: List[Any]): Option[Any] =
|
||||
list match
|
||||
case Nil => None
|
||||
case _ => Some(list.last)
|
||||
@@ -0,0 +1,12 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
class Problem01Suite extends munit.FunSuite:
|
||||
List(
|
||||
(List(1, 2, 3, 4, 5), Some(5)),
|
||||
(List(1), Some(1)),
|
||||
(List(), None)
|
||||
).foreach:
|
||||
case (list, expected) =>
|
||||
test(s"lastElement returns [$expected]"):
|
||||
assertEquals(Problem01.lastElement(list), expected, "Problem01")
|
||||
assertEquals(Problem01Stdlib.lastElement(list), expected, "Problem01Stdlib")
|
||||
Reference in New Issue
Block a user