Javascript to Elm

15: Maybe, Just, and Nothing

Informações:

Synopsis

  Maybe Type type Maybe a = Just a | Nothing Historical It’s just a union type, nothing scary, right? It’s gonna be Just with a value, or Nothing. Notice that I have typed these with capital letters. They are types, not values. The Maybe type encapsulates an optional value. A value of type Maybe a either contains a value of type a (represented as Just a), or it is empty (represented as Nothing). Using Maybe is a good way to deal with errors or exceptional cases without resorting to drastic measures such as error. The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing. A richer error monad can be built using the Either type. What did you say? Mona-wanta-who-now? Monads in Haskell can be thought of as composable computation descriptions. More info - Lambdacast Practical How do I get something out of this thing to work with ? The maybe module to the rescue! Let’s look at a simple example > fruit = ["apple", "banana", "mango"] ["apple","banana"