Skip to content

Instantly share code, notes, and snippets.

@VictoryWangCN
Last active November 19, 2019 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VictoryWangCN/9af950a5e6a7c41c0e830c3f76160fca to your computer and use it in GitHub Desktop.
Save VictoryWangCN/9af950a5e6a7c41c0e830c3f76160fca to your computer and use it in GitHub Desktop.
object L {
def main(args: Array[String]): Unit = {
val oneToTenList = (1 to 10).toList
val oneList = List(1)
assert("none" == checker(11)(oneToTenList))
assert("end of 10" == checker(10)(oneToTenList))
assert("contains 9" == checker(9)(oneToTenList))
assert("start with 1" == checker(1)(oneToTenList))
assert("none" == checker(0)(oneList))
assert("start or end of 1" == checker(1)(oneList))
assert("none" == checker(1)(Nil))
}
def checker(item: Int): List[Int] => String = {
case Nil => "none"
case h :: Nil if h equals item => s"start or end of $item"
case h :: _ if h equals item => s"start with $item"
case _ :: rest => contain(item)(rest)
}
def contain(item: Int): List[Int] => String = {
case Nil => "none"
case h :: Nil if h.equals(item) => s"end of $item"
case h :: rest => if (h.equals(item)) s"contains $item" else contain(item)(rest)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment