Skip to content

Instantly share code, notes, and snippets.

@kdkanishka
Created November 5, 2020 07:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdkanishka/40b5aa89fad66627cc095eb1a555de98 to your computer and use it in GitHub Desktop.
Save kdkanishka/40b5aa89fad66627cc095eb1a555de98 to your computer and use it in GitHub Desktop.
object Test extends App {
val list = List(10, 20, 30, 40, 50)
println(list)
val ls2 = calc(list)
println(ls2)
if (list.length != ls2.length) {
println("Unable to process it successfully")
}
def calc(list: List[Int]): List[Int] = {
if (!list.isEmpty) {
val processResult = process(list.head)
val tail = list.tail
processResult match {
case Success(calcNum) => calcNum :: calc(tail)
case _ => Nil
}
} else {
Nil
}
}
def process(num: Int): Try[Int] = {
Try {
if (num == 30)
throw new Exception("oops")
else num + 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment