:: Maybe Int -> Int -> Int package:rio

O(1) Indexing
O(1) Unsafe indexing without bounds checking
The fromMaybe function takes a default value and and Maybe value. If the Maybe is Nothing, it returns the default values; otherwise, it returns the value contained in the Maybe.

Examples

Basic usage:
>>> fromMaybe "" (Just "Hello, World!")
"Hello, World!"
>>> fromMaybe "" Nothing
""
Read an integer from a string using readMaybe. If we fail to parse an integer, we want to return 0 by default:
>>> import Text.Read ( readMaybe )

>>> fromMaybe 0 (readMaybe "5")
5

>>> fromMaybe 0 (readMaybe "")
0
O(1) Safe indexing
O(n) Append an element
Flipped version of <$. Using ApplicativeDo: 'as $> b' can be understood as the do expression
do as
pure b
with an inferred Functor constraint.

Examples

Replace the contents of a Maybe Int with a constant String:
>>> Nothing $> "foo"
Nothing

>>> Just 90210 $> "foo"
Just "foo"
Replace the contents of an Either Int Int with a constant String, resulting in an Either Int String:
>>> Left 8675309 $> "foo"
Left 8675309

>>> Right 8675309 $> "foo"
Right "foo"
Replace each element of a list with a constant String:
>>> [1,2,3] $> "foo"
["foo","foo","foo"]
Replace the second element of a pair with a constant String:
>>> (1,2) $> "foo"
(1,"foo")
O(1) Indexing in a monad. The monad allows operations to be strict in the vector when necessary. Suppose vector copying is implemented like this:
copy mv v = ... write mv i (v ! i) ...
For lazy vectors, v ! i would not be evaluated which means that mv would unnecessarily retain a reference to v in each element written. With indexM, copying can be implemented like this instead:
copy mv v = ... do
x <- indexM v i
write mv i x
Here, no references to v are retained because indexing (but not the elements) is evaluated eagerly.
Assumed complexity: O(1) Yield the element at the given position in a monad. No range checks are performed. The monad allows us to be strict in the vector if we want. Suppose we had
unsafeIndex :: v a -> Int -> a
instead. Now, if we wanted to copy a vector, we'd do something like
copy mv v ... = ... unsafeWrite mv i (unsafeIndex v i) ...
For lazy vectors, the indexing would not be evaluated which means that we would retain a reference to the original vector in each element we write. This is not what we want! With basicUnsafeIndexM, we can do
copy mv v ... = ... case basicUnsafeIndexM v i of
Box x -> unsafeWrite mv i x ...
which does not have this problem because indexing (but not the returned element!) is evaluated immediately.