Introduction to Scala Part3: Option & Pattern Matching
Bubu
2,979 views
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package fp101.tp03.option
object ExerciceOptions {
/**
* Give the positive square root of x
*
* If the argument is less than zero, the result is None
*
* hint: You can use Math.sqrt
*/
def squareRoot(x: Int): Option[Double] = if(x < 0) None else Some(Math.sqrt(x))
/**
*
* Without using l.take nor l(index), create a function that will return the Some(element) at index n
* otherwise None
*/
def getOption[T](l: List[T], index: Int): Option[T] = ???
}