Functional Programming explained to my grandma
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
No More NPE
Has you ever spent hours trying to find where a NPE is coming from?
This will never happen again if you use options! (🎉 they exist in Java 🎉)
To keep it simple, an option is a wrapper which accepts two kinds of content:
- A content "value" which will be returned each time when it's possible
- A "null" value which will be return when no correct value can be returned
In Scala, an option has no concrete type, but two implementations:
Some(object) wrapper for a non-null value
None wrapper for a null value
You can initialize an option to a concrete value or to null and it will return the right wrapper. An option has two opposite methods:
isEmpty true if the option is a none
isDefined true if the option has a value
So let's define the divide function which manages double division with the case where the divisor is equal to 0
Discover our saviour
In addition to providing us with a nice wrapper for null values, option gives us two interesting methods:
getOrElse option.getOrElse(12) return the value of the option if it's defined, or a default value if it's empty
fold option.fold(0)(x => x*x) apply the function passed in parameter if the option is defined or has a default value otherwise
Let's define the two functions:
- Secure divide: a function which calls divide and returns the value of the option if it's possible, 0 otherwise
- Weird divide: a function which calls divide and returns the value of the division squared, or -1
More fancy functionalities
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
22
23
24
25
26
27
28
29
30
package example
/**
* Created by charlotte on 14/05/17.
*/
object OptionType {
val x : String = "Hello, Maybe?"
var maybe = Option(x) // Some
var maybeNot = Option(null) // None
/**
* is defined check if the option has a value
* @return true for maybe, false for maybeNot
*/
def isDefined (option :Option[String]) : Boolean ={
option.isDefined
}
/**
* is defined check if the option has no value
* @return false for maybe, true for maybeNot
*/
def isEmpty (option : Option[String]) : Boolean ={
option.isEmpty
}
Enter to Rename, Shift+Enter to Preview