on a3801ac help sign in
elk/libraries/Maybe.elm
33 lines 893 B history raw
2module Maybe where
4import List as List
6-- The Maybe datatype. Useful when a computation may or may not
7-- result in a value (e.g. logarithm is defined only for positive numbers).
8data Maybe a = Just a | Nothing
10-- Apply a function to the contents of a `Maybe`.
11-- Return default when given `Nothing`.
12maybe : b -> (a -> b) -> Maybe a -> b
13maybe b f m = case m of
14 Just v -> f v
15 Nothing -> b
17-- Check if constructed with `Just`.
18isJust : Maybe a -> Bool
19isJust = maybe False (\_ -> True)
21-- Check if constructed with `Nothing`.
22isNothing : Maybe a -> Bool
23isNothing = not . isJust
26-- If `Just`, adds the value to the front of the list.
27-- If `Nothing`, list is unchanged.
28cons : Maybe a -> [a] -> [a]
29cons mx xs = maybe xs (\x -> x :: xs) mx
31-- Filters out Nothings and extracts the remaining values.
32justs : [Maybe a] -> [a]
33justs = List.foldr cons []