Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fantasy-land: add S.reduce_ #682

Merged
merged 1 commit into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this signature to match the signature used in the documentation.


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]);

});