Skip to content

Commit

Permalink
Merge pull request #181 from plaid/dc-justs
Browse files Browse the repository at this point in the history
maybe: rename S.catMaybes
  • Loading branch information
davidchambers committed Apr 23, 2016
2 parents b4ed682 + 108c868 commit e3c2db2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
13 changes: 8 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,16 +1201,19 @@
[b, $.Function, $Maybe(a), b],
function(x, f, maybe) { return fromMaybe(x, maybe.map(f)); });

//# catMaybes :: [Maybe a] -> [a]
//# justs :: [Maybe a] -> [a]
//.
//. Takes a list of Maybes and returns a list containing each Just's value.
//. Equivalent to Haskell's `catMaybes` function.
//.
//. See also [`lefts`](#lefts) and [`rights`](#rights).
//.
//. ```javascript
//. > S.catMaybes([S.Just('foo'), S.Nothing(), S.Just('baz')])
//. > S.justs([S.Just('foo'), S.Nothing(), S.Just('baz')])
//. ['foo', 'baz']
//. ```
var catMaybes = S.catMaybes =
def('catMaybes',
var justs = S.justs =
def('justs',
{},
[$.Array($Maybe(a)), $.Array(a)],
R.chain(maybe([], R.of)));
Expand All @@ -1233,7 +1236,7 @@
def('mapMaybe',
{},
[$.Function, $.Array(a), $.Array(b)],
meld([R.map, catMaybes]));
meld([R.map, justs]));

//# encase :: (a -> b) -> a -> Maybe b
//.
Expand Down
24 changes: 12 additions & 12 deletions test/catMaybes.js → test/justs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ var errorEq = require('./utils').errorEq;
var S = require('..');


describe('catMaybes', function() {
describe('justs', function() {

it('is a unary function', function() {
eq(typeof S.catMaybes, 'function');
eq(S.catMaybes.length, 1);
eq(typeof S.justs, 'function');
eq(S.justs.length, 1);
});

it('type checks its arguments', function() {
throws(function() { S.catMaybes({length: 0}); },
throws(function() { S.justs({length: 0}); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
'catMaybes :: Array (Maybe a) -> Array a\n' +
' ^^^^^^^^^^^^^^^\n' +
' 1\n' +
'justs :: Array (Maybe a) -> Array a\n' +
' ^^^^^^^^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) {"length": 0} :: Object, StrMap Number, StrMap FiniteNumber, StrMap Integer, StrMap ValidNumber\n' +
'\n' +
'The value at position 1 is not a member of ‘Array (Maybe a)’.\n'));
});

it('returns a list containing the value of each Just', function() {
eq(S.catMaybes([]), []);
eq(S.catMaybes([S.Nothing(), S.Nothing()]), []);
eq(S.catMaybes([S.Nothing(), S.Just('b')]), ['b']);
eq(S.catMaybes([S.Just('a'), S.Nothing()]), ['a']);
eq(S.catMaybes([S.Just('a'), S.Just('b')]), ['a', 'b']);
eq(S.justs([]), []);
eq(S.justs([S.Nothing(), S.Nothing()]), []);
eq(S.justs([S.Nothing(), S.Just('b')]), ['b']);
eq(S.justs([S.Just('a'), S.Nothing()]), ['a']);
eq(S.justs([S.Just('a'), S.Just('b')]), ['a', 'b']);
});

});

0 comments on commit e3c2db2

Please sign in to comment.