Permalink
Browse files

(fix) Changes the signature for Either.try, now taking a thunk

  • Loading branch information...
robotlolita committed Oct 22, 2016
1 parent 7a1ef33 commit 5676d39238e821925eb292e47d3328c429ff07e7
Showing with 21 additions and 5 deletions.
  1. +19 −3 src/data/either/try.js
  2. +2 −2 test/specs-src/data.either.js
View
@@ -3,18 +3,34 @@ const { Left, Right } = require('./either');
/*~
* Runs a function that may raise an exception, trapping it, and returning
* an Either representing the result.
*
* ## Example::
*
* function succ(nat) {
* if (nat < 0) {
* throw `Not a natural number: ${nat}`;
* } else {
* return nat + 1;
* }
* }
*
* const { Left, Right } = require('folktale/data/either');
*
* _try(() => succ(-1)); // ==> Left('Not a natural number: -1')
* _try(() => succ(1)); // ==> Right(2)
*
* ---
* category: Handling exceptions
* stability: experimental
* authors:
* - "@boris-marinov"
*
* type: |
* forall a, b: ((Any...) => b :: throws a) => Either a b
* forall a, b: (() => b :: throws a) => Either a b
*/
const _try = (f) => (...args) => {
const _try = (f) => {
try {
return Right(f(...args));
return Right(f());
} catch (e) {
return Left(e);
}
@@ -18,11 +18,11 @@ describe('Data.Either', function() {
describe('constructors', () => {
property('try#Left', 'json', (a) => {
return _.try((a) => {throw a })(a).equals(_.Left(a))
return _.try(() => {throw a }).equals(_.Left(a))
});
property('try#Right', 'json', (a) => {
return _.try(() => (a))(a).equals(_.Right(a))
return _.try(() => (a)).equals(_.Right(a))
});
});

0 comments on commit 5676d39

Please sign in to comment.