-
Notifications
You must be signed in to change notification settings - Fork 5
/
MaybeFunctions.hs
33 lines (22 loc) · 1.27 KB
/
MaybeFunctions.hs
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
module MaybeFunctions where
import Data.Char
import Data.List
-- | Maybe is just like Optional or Option from Scala and Java.
-- With Maybe we define the type [Maybe] follow by the type [T] that it will contain.
-- | In case we want to fill a Maybe with data we just need to add [Just] as Some in Scala just before the data "Hello effect"
someValue = Just "Hello effect" :: Maybe String
-- | In case we want to create an empty Maybe we just need to add [Nothing] as None in Scala
emptyValue = Nothing :: Maybe String
-- | The pure way to extract the value from a Maybe is using patter matching,
-- where we can just create the case for [Just value] or [Nothing], as we normally would do from Scala.
valueOfMaybeFilled = case someValue of
Just value -> value
Nothing -> "Default value"
valueOfMaybeNothing = case emptyValue of
Just value -> value
Nothing -> "Default value"
-- | In this next example we use the operator [find] which it return a [Mayvbe] type depending if is able or not
-- to find an element in the list.
people = ["Paul","Peter", "John", "Sussan"] :: [String]
outputFindJust = find (isGuy "John") people where isGuy name = \person -> person == name
outputFindNoting = find (isGuy "Heisemberg") people where isGuy name = \person -> person == name