monet / monet.js

monet.js - Monadic types library for JavaScript
https://monet.github.io/monet.js/
MIT License
1.6k stars 114 forks source link

Either from exception #210

Closed rparree closed 4 years ago

rparree commented 5 years ago

Hi,

Is there a way of creating an Either from an expression that might yield an exception? Or perhaps are there plans to add a Try (with a toEither)? If there's demand for that (either a method like below or a full Try type), i could look into that and send a pull request.

function fromTry(fn){
    try{
        return Either.right(fn())
    } catch(e) {
        return Either.left(e)
    }
}
cwmyers commented 5 years ago

Yeah that would be awesome. I'm surprised I hadn't done that already. Let's start with your suggestion and then see whether a first class Try type makes sense.

rparree commented 5 years ago

Ok i will do so this coming weekend.

ulfryk commented 5 years ago

Just to make things consistent with other stuff here, let's make it Either.fromTry(…) ;)

ulfryk commented 5 years ago

Also some work (alias attempt) is already started https://github.com/monet/monet.js/pull/189

(but seems to be abandoned)

ulfryk commented 5 years ago

snapshot released as monet@0.9.0-422

wookieb commented 4 years ago

How about this? Can we merge it and release 0.9.1?

ulfryk commented 4 years ago

@wookieb it's merged wil be released with 0.9.1