X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fhope_result.erl;h=9d4235be1001d1d388c03305bd16749d5a4937bf;hb=fef301085ff208638f7c07c1fb9048f3deaf01bd;hp=930f2e054d3fb61731c88a58410841c2f4adbb38;hpb=64617423d513e37494369d637bee5ff357de791b;p=hope.git diff --git a/src/hope_result.erl b/src/hope_result.erl index 930f2e0..9d4235b 100644 --- a/src/hope_result.erl +++ b/src/hope_result.erl @@ -9,11 +9,15 @@ ]). -export( + % Generic monad interface [ return/1 - , map/2 + , map/2 % map/2 is alias for map_ok/2 + , pipe/2 + + % Specific to hope_result:t() + , map_ok/2 , map_error/2 , tag_error/2 - , pipe/2 , lift_exn/1 , lift_exn/2 , lift_map_exn/3 @@ -41,9 +45,14 @@ return(X) -> -spec map(t(A, Error), fun((A) -> (B))) -> t(B, Error). -map({ok, X}, F) -> +map({_, _}=T, F) -> + map_ok(T, F). + +-spec map_ok(t(A, Error), fun((A) -> (B))) -> + t(B, Error). +map_ok({ok, X}, F) -> {ok, F(X)}; -map({error, _}=Error, _) -> +map_ok({error, _}=Error, _) -> Error. -spec map_error(t(A, B), fun((B) -> (C))) -> @@ -109,7 +118,7 @@ lift_map_exn(F, MapOk, MapError) when is_function(F, 1) -> % Applying maps separately as to not unintentionally catch an exception % raised in a map. case Result - of {ok , _}=Ok -> map (Ok , MapOk) + of {ok , _}=Ok -> map_ok (Ok , MapOk) ; {error, _}=Error -> map_error(Error, MapError) end end.