sanctuary-js / sanctuary

:see_no_evil: Refuge from unsafe JavaScript
https://sanctuary.js.org
MIT License
3.03k stars 94 forks source link

maybe, either: unify S.encase and S.encaseEither #620

Closed davidchambers closed 5 years ago

davidchambers commented 5 years ago

There's little value in having both S.encase and S.encaseEither: every use of the former could be rewritten in terms of the latter with help from S.eitherToMaybe. Thus we should remove S.encase.

This vacates the desirable name, so we should then rename S.encaseEither.

Finally, we should drop the Error -> l argument as it is unnecessary in the presence of S.mapLeft.

In summary:

--  Before
encase       ::                 (a -> b) -> a -> Maybe b
encaseEither :: (Error -> l) -> (a -> r) -> a -> Either l r

--  After
encase       ::                 (a -> b) -> a -> Either Error b

In conjunction with #618, this will reduce the number of “encase” functions from six to one. :)

toolsh commented 4 years ago

I think this is a good change. try/catch is more suited to Either than Maybe anyway.

The change is useful. However, typescript @types/sanctuary need related updates too.

import S = require("sanctuary"); {} import which = require("which"); undefined var adb = S.encase(which.sync)("adb"); undefined S.type(adb); { namespace: Just ("sanctuary-either"), name: 'Either', version: 1 } adb.value [eval].ts:5:5 - error TS2339: Property 'value' does not exist on type 'Maybe'. 5 adb.value


undefined