by DanWaterworth · 11y ago · 0 comments
flatMap :: Either e a -> (a -> Either e b) -> Either e b
flatMap x f =
case x of
Left _ -> x
Right x' -> f x'
In the case expression, you have to write `Left err -> Left err`, because `x` here has the type `Either e a`, but `Either e b` is expected. func map<U>(f: T -> U) -> Result<U> {
switch self {
case let .Value(value):
return Result<U>.Value(Box(f(value.unbox)))
case let .Error(error):
// self is Result<T>, but the return type must be Result<U>
return self
}
}