2013-02-04 04:13:08 +00:00
|
|
|
/*! Maybe
|
|
|
|
!*/
|
|
|
|
|
|
|
|
/*[Definition]*/
|
|
|
|
|
|
|
|
/** data Maybe a = Just a | Nothing
|
|
|
|
The Maybe datatype. Useful when a computation may or may not
|
|
|
|
result in a value (e.g. logarithm is defined only for positive numbers).
|
|
|
|
**/
|
|
|
|
|
|
|
|
/*[Basic Utilities]*/
|
|
|
|
|
|
|
|
/** maybe : b -> (a -> b) -> Maybe a -> b
|
|
|
|
Apply a function to the contents of a `Maybe`.
|
|
|
|
Return default when given `Nothing`.
|
|
|
|
**/
|
|
|
|
/** isJust : Maybe a -> Bool
|
|
|
|
Check if constructed with `Just`.
|
|
|
|
**/
|
|
|
|
/** isNothing : Maybe a -> Bool
|
|
|
|
Check if constructed with `Nothing`.
|
|
|
|
**/
|
|
|
|
|
|
|
|
/*[Maybe with Lists]*/
|
|
|
|
|
|
|
|
/** cons : Maybe a -> [a] -> [a]
|
|
|
|
If `Just`, adds the value to the front of the list.
|
|
|
|
If `Nothing`, list is unchanged.
|
|
|
|
**/
|
|
|
|
/** justs : [Maybe a] -> [a]
|
|
|
|
Filters out Nothings and extracts the remaining values.
|
|
|
|
**/
|
2012-10-16 06:03:14 +00:00
|
|
|
|
|
|
|
Elm.Maybe = function() {
|
|
|
|
function consMaybe(x) { return function(xs) {
|
|
|
|
if (x[0] === "Just") return ["Cons", x[1], xs];
|
|
|
|
return xs;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function mapCons(f) { return function(y) { return function(xs) {
|
|
|
|
var x = f(y);
|
|
|
|
if (x[0] === "Just") return ["Cons", x[1], xs];
|
|
|
|
return xs;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function maybe(b) { return function(f) { return function(m) {
|
|
|
|
if (m[0] === "Just") return f(m[1]);
|
|
|
|
return b;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {Just : function(x) { return ["Just",x]; },
|
|
|
|
Nothing : ["Nothing"],
|
2013-01-09 10:07:46 +00:00
|
|
|
justs : Elm.List.foldr(consMaybe)(["Nil"]),
|
2012-10-16 06:03:14 +00:00
|
|
|
isJust : function(m) { return m[0] === "Just"; },
|
|
|
|
isNothing : function(m) { return m[0] === "Nothing"; },
|
2013-01-09 10:07:46 +00:00
|
|
|
cons : consMaybe,
|
2012-10-16 06:03:14 +00:00
|
|
|
maybe : maybe
|
|
|
|
};
|
|
|
|
}();
|