Skip to content

Commit 46b53ff

Browse files
committed
replace Monoid dependency with Alternative dependency for ‘filterM’
1 parent c6e12ea commit 46b53ff

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,8 @@
13331333
//.
13341334
//. Filters its second argument in accordance with the given predicate.
13351335
//.
1336-
//. This function is derived from [`empty`](#empty), [`of`](#of), and
1337-
//. [`reduce`](#reduce).
1336+
//. This function is derived from [`concat`](#concat), [`empty`](#empty),
1337+
//. [`of`](#of), and [`reduce`](#reduce).
13381338
//.
13391339
//. See also [`filterM`](#filterM).
13401340
//.
@@ -1352,12 +1352,12 @@
13521352
m);
13531353
}
13541354

1355-
//# filterM :: (Monad m, Monoid (m a)) => (a -> Boolean, m a) -> m a
1355+
//# filterM :: (Alternative m, Monad m) => (a -> Boolean, m a) -> m a
13561356
//.
13571357
//. Filters its second argument in accordance with the given predicate.
13581358
//.
1359-
//. This function is derived from [`empty`](#empty), [`of`](#of), and
1360-
//. [`chain`](#chain).
1359+
//. This function is derived from [`of`](#of), [`chain`](#chain), and
1360+
//. [`zero`](#zero).
13611361
//.
13621362
//. See also [`filter`](#filter).
13631363
//.
@@ -1367,11 +1367,20 @@
13671367
//.
13681368
//. > filterM(x => x % 2 == 1, Cons(1, Cons(2, Cons(3, Nil))))
13691369
//. Cons(1, Cons(3, Nil))
1370+
//.
1371+
//. > filterM(x => x % 2 == 1, Nothing)
1372+
//. Nothing
1373+
//.
1374+
//. > filterM(x => x % 2 == 1, Just(0))
1375+
//. Nothing
1376+
//.
1377+
//. > filterM(x => x % 2 == 1, Just(1))
1378+
//. Just(1)
13701379
//. ```
13711380
function filterM(pred, m) {
13721381
var M = m.constructor;
1373-
var e = empty(M);
1374-
return chain(function(x) { return pred(x) ? of(M, x) : e; }, m);
1382+
var z = zero(M);
1383+
return chain(function(x) { return pred(x) ? of(M, x) : z; }, m);
13751384
}
13761385

13771386
//# alt :: Alt f => (f a, f a) -> f a

test/List.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ List[FL.empty] = function() { return Nil; };
3535

3636
List[FL.of] = function(x) { return Cons(x, Nil); };
3737

38+
List[FL.zero] = List[FL.empty];
39+
3840
List.prototype[FL.equals] = function(other) {
3941
return this.tag === 'Nil' ?
4042
other.tag === 'Nil' :
@@ -67,6 +69,8 @@ List.prototype[FL.chain] = function(f) {
6769
Z.concat(f(this.head), Z.chain(f, this.tail));
6870
};
6971

72+
List.prototype[FL.alt] = List.prototype[FL.concat];
73+
7074
List.prototype[FL.reduce] = function(f, x) {
7175
return this.tag === 'Nil' ?
7276
x :

test/Maybe.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ Maybe['@@type'] = 'sanctuary-type-classes/Maybe';
1919

2020
Maybe.Nothing = new _Maybe('Nothing');
2121

22-
Maybe[FL.of] = Maybe.Just = function(x) { return new _Maybe('Just', x); };
22+
Maybe.Just = function(x) { return new _Maybe('Just', x); };
2323

24-
Maybe[FL.empty] = Maybe[FL.zero] = function() { return Maybe.Nothing; };
24+
Maybe[FL.empty] = function() { return Maybe.Nothing; };
25+
26+
Maybe[FL.of] = Maybe.Just;
27+
28+
Maybe[FL.zero] = Maybe[FL.empty];
2529

2630
Maybe.prototype[FL.equals] = function(other) {
2731
return this.isNothing ? other.isNothing
@@ -32,6 +36,10 @@ Maybe.prototype[FL.map] = function(f) {
3236
return this.isJust ? Maybe.Just(f(this.value)) : Maybe.Nothing;
3337
};
3438

39+
Maybe.prototype[FL.chain] = function(f) {
40+
return this.isJust ? f(this.value) : Maybe.Nothing;
41+
};
42+
3543
Maybe.prototype[FL.alt] = function(other) {
3644
return this.isJust ? this : other;
3745
};

test/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,9 @@ test('filterM', function() {
733733
eq(Z.filterM(odd, [1, 2, 3, 4, 5]), [1, 3, 5]);
734734
eq(Z.filterM(odd, Nil), Nil);
735735
eq(Z.filterM(odd, Cons(1, Cons(2, Cons(3, Cons(4, Cons(5, Nil)))))), Cons(1, Cons(3, Cons(5, Nil))));
736+
eq(Z.filterM(odd, Nothing), Nothing);
737+
eq(Z.filterM(odd, Just(0)), Nothing);
738+
eq(Z.filterM(odd, Just(1)), Just(1));
736739
});
737740

738741
test('alt', function() {

0 commit comments

Comments
 (0)