gigobyte / purify

Functional programming library for TypeScript - https://gigobyte.github.io/purify/
ISC License
1.53k stars 59 forks source link

Bimap for effects for an Either #257

Closed hellos3b closed 3 years ago

hellos3b commented 3 years ago

So while using this lib, I find often my "app" revolves around an Either. Take for example, a REST request in express:

const route = (req: Request, res: Response) => {
  codec
    .decode(req.body)
    .map(transform)
    .bimap(handleError(req), res.send)
}

One thing that feels kind of wrong is using bimap for the end results/effects, as I'm not transforming the Either. I think it'd be nice to have an explicit method that doesn't transform the Either, similar to Array.forEach vs Array.map. The Fluture library uses fork which I think represents it well

Either.fork(respondError, respond)
pyrho commented 3 years ago

If this is only a semantics issue, there is also .caseOf() and .either() that achieve similar functionality without the "map" naming.

That being said, I think you may be looking for EitherAsync.run() (afaik very similar to fluture's fork()), res.send is a side effect-ing function and I feel those a better represented using the *Async variants.

disclaimer: Take my suggestions with a grain of salt, I'm by no means an expert.

gigobyte commented 3 years ago

As @pyrho mentioned, there are a lot of options and all of them are fine. I don't think there's a need for another method. If you want to be explicit about the side-effects of the handler the best solution would be:

const route = (req: Request, res: Response) => {
  codec
    .decode(req.body)
    .map(transform)
    .ifLeft(handleError(req))
    .ifRight(res.send)
}