Introduction to Scala Part3: Option & Pattern Matching
Bubu
4,465 views
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
squareRoot
getOption
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content
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] = ???
}
Enter to Rename, Shift+Enter to Preview