Skip to content

Commit

Permalink
Merge pull request #682 from sanctuary-js/davidchambers/reduce
Browse files Browse the repository at this point in the history
fantasy-land: add S.reduce_
  • Loading branch information
davidchambers authored Jul 25, 2020
2 parents 3047999 + 962c211 commit 354f571
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
29 changes: 27 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,10 +1109,15 @@
//. > S.flip (Cons (Math.floor) (Cons (Math.ceil) (Nil))) (1.5)
//. Cons (1) (Cons (2) (Nil))
//. ```
function flip(functor) {
return function(x) {
return Z.flip (functor, x);
};
}
_.flip = {
consts: {f: [Z.Functor]},
types: [f ($.Fn (a) (b)), a, f (b)],
impl: curry2 (Z.flip)
impl: flip
};

//# bimap :: Bifunctor f => (a -> b) -> (c -> d) -> f a c -> f b d
Expand Down Expand Up @@ -1229,6 +1234,8 @@
//. value if the Foldable is empty; the result of the final application
//. otherwise.
//.
//. See also [`reduce_`](#reduce_).
//.
//. ```javascript
//. > S.reduce (S.add) (0) ([1, 2, 3, 4, 5])
//. 15
Expand All @@ -1247,10 +1254,28 @@
}
_.reduce = {
consts: {f: [Z.Foldable]},
types: [$.Fn (a) ($.Fn (b) (a)), a, f (b), a],
types: [$.Fn (b) ($.Fn (a) (b)), b, f (a), b],
impl: reduce
};

//# reduce_ :: Foldable f => (a -> b -> b) -> b -> f a -> b
//.
//. Variant of [`reduce`](#reduce) that takes a reducing function with
//. arguments flipped.
//.
//. ```javascript
//. > S.reduce_ (S.append) ([]) (Cons (1) (Cons (2) (Cons (3) (Nil))))
//. [1, 2, 3]
//.
//. > S.reduce_ (S.prepend) ([]) (Cons (1) (Cons (2) (Cons (3) (Nil))))
//. [3, 2, 1]
//. ```
_.reduce_ = {
consts: {f: [Z.Foldable]},
types: [$.Fn (a) ($.Fn (b) (b)), b, f (a), b],
impl: B (reduce) (flip)
};

//# traverse :: (Applicative f, Traversable t) => TypeRep f -> (a -> f b) -> t a -> f (t b)
//.
//. Curried version of [`Z.traverse`][].
Expand Down
2 changes: 1 addition & 1 deletion test/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const eq = require ('./internal/eq');

test ('reduce', () => {

eq (S.show (S.reduce)) ('reduce :: Foldable f => (a -> b -> a) -> a -> f b -> a');
eq (S.show (S.reduce)) ('reduce :: Foldable f => (b -> a -> b) -> b -> f a -> b');

eq (S.reduce (S.concat) ('x') ([])) ('x');
eq (S.reduce (S.concat) ('x') (['A', 'B', 'C'])) ('xABC');
Expand Down
17 changes: 17 additions & 0 deletions test/reduce_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const S = require ('..');

const eq = require ('./internal/eq');


test ('reduce_', () => {

eq (S.show (S.reduce_)) ('reduce_ :: Foldable f => (a -> b -> b) -> b -> f a -> b');

eq (S.reduce_ (S.append) ([]) ([])) ([]);
eq (S.reduce_ (S.append) ([]) ([1, 2, 3])) ([1, 2, 3]);
eq (S.reduce_ (S.prepend) ([]) ([])) ([]);
eq (S.reduce_ (S.prepend) ([]) ([1, 2, 3])) ([3, 2, 1]);

});

0 comments on commit 354f571

Please sign in to comment.