Created
November 5, 2020 07:56
-
-
Save kdkanishka/40b5aa89fad66627cc095eb1a555de98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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