# Functional Programming explained to my grandma

CCavalier

13.1K views

## 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

Create your playground on Tech.io

This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.

Suggested playgrounds

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

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

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

}

val alwaysTrue = maybe.isDefined!=maybe.isEmpty

/**

* try to define a function which returns amt / divisor

* @param amt amount to divide

* @param divisor

* @return an option non if the divisor is 0, a non empty option else

*/

def divide (amt : Double, divisor: Double): Option[Double] ={

???

}

/**

* if divide is possible, return the result of divide, else return 0

*/

def secureDivide(amt: Double, divisor: Double):Double ={

???

}

/**

* if divide is possible return the square of the result, -1 else

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!

JOIN US ON DISCORD Online Participants