# Maybe¶

It is often necessary to model values which can be undefined. This can be done using the type Maybe a, where a is the type of the value which might be undefined. For example, Maybe String can be used to represent the values which are either text strings, or undefined.

The type is defined with two constructors:

type Maybe a
| Some a
| None


So a value of type Maybe String will either be of the form Some "a text string" (representing the actual string values), or None (representing the undefined value).

## maybe : b -> (a -> b) -> Maybe a -> b¶

The maybe function takes a default value, a function, and a Maybe value. The default value is returned if the Maybe value is None, otherwise the result of applying the function to the value inside the Some is returned.

### Examples¶

val a = maybe 0 (\x -> x + 5) (Some 2)
//    = 7
val b = maybe 0 (\x -> x + 5) None
//    = 0


## fromMaybe : a -> Maybe a -> a¶

The fromMaybe function extracts the value from a Maybe, using the default value for the None case.

### Examples¶

val a = fromMaybe 0 (Some 5)
//    = 5
val b = fromMaybe 0 None
//    = 0


## Maybe::map : (a -> b) -> Maybe a -> Maybe b¶

Lift any function to a function in Maybe.

### Examples¶

val a = Maybe::map (\m -> m * 2) (Some 5)
//    = Some 10
val b = Maybe::map (\m -> m * 2) None
//    = None


## Maybe::isSome : Maybe a -> Bool¶

Returns True if the input is a Some, returns False otherwise.

### Examples¶

val a = Maybe::isSome (Some 2)
//    = True
val b = Maybe::isSome (Some "a text string")
//    = True
val c = Maybe::isSome None
//    = False


## Maybe::any : (a -> Bool) -> Maybe a -> Bool¶

Returns True if, and only if, the given Maybe has a value, and this value satisfies the given predicate.

### Examples¶

val a = Maybe::any (\n -> n > 4) (Some 5)
//    = True
val b = Maybe::any (\n -> n > 4) (Some 2)
//    = False
val c = Maybe::any (\n -> n > 4) None
//    = False


## Maybe::all : (a -> Bool) -> Maybe a -> Bool¶

Returns True if the given Maybe has no value, or if it has a value which satisfies the given predicate.

### Examples¶

val a = Maybe::all (\x -> x >= 1) None
//    = True
val b = Maybe::all (\x -> x >= 1) (Some 2)
//    = True
val c = Maybe::all (\x -> x >= 1) (Some 0)
//    = False


## Maybe::bind : (a -> Maybe b) -> Maybe a -> Maybe b¶

Applies a function that returns a Maybe to a Maybe value if that value is Some. None otherwise

### Examples¶

val a = Maybe::bind (\x -> Some (x + 1)) None
//    = None
val b = Maybe::bind (\x -> None) (Some 1)
//    = None
val c = Maybe::bind (\x -> Some (x + 1)) (Some 1)
//    = Some 2