Skip to content

Commit

Permalink
Rename S.flip to S.flip_ and S.C to S.flip
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Feb 4, 2017
1 parent d71fc72 commit a82f202
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 50 deletions.
44 changes: 15 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,28 +571,6 @@
}
S.T = def('T', {}, [a, Fn(a, b), b], T);

//# C :: (a -> b -> c) -> b -> a -> c
//.
//. The C combinator. Takes a curried binary function and two values, and
//. returns the result of applying the function to the values in reverse
//. order. Equivalent to Haskell's `flip` function.
//.
//. This function is very similar to [`flip`](#flip), except that its first
//. argument must be curried. This allows it to work with manually curried
//. functions.
//.
//. ```javascript
//. > S.C(S.concat, 'foo', 'bar')
//. 'barfoo'
//.
//. > Z.map(S.C(S.concat, '?'), ['foo', 'bar', 'baz'])
//. ['foo?', 'bar?', 'baz?']
//. ```
function C(f, x, y) {
return f(y)(x);
}
S.C = def('C', {}, [Fn(a, Fn(b, c)), b, a, c], C);

//# S :: (a -> b -> c) -> (a -> b) -> a -> c
//.
//. The S combinator. Takes a curried binary function, a unary function,
Expand Down Expand Up @@ -711,21 +689,29 @@
[$.Function([a, b, c, d, e, r]), a, b, c, d, e, r],
curry5);

//# flip :: ((a, b) -> c) -> b -> a -> c
//# flip :: (a -> b -> c) -> b -> a -> c
//.
//. Takes a binary function and two values, and returns the result of
//. applying the function to the values in reverse order.
//. Takes a curried binary function and two values, and returns the
//. result of applying the function to the values in reverse order.
//.
//. See also [`C`](#C).
//. This is the C combinator from combinatory logic.
//.
//. ```javascript
//. > Z.map(S.flip(Math.pow)(2), [1, 2, 3, 4, 5])
//. [1, 4, 9, 16, 25]
//. > S.flip(S.concat, 'foo', 'bar')
//. 'barfoo'
//. ```
function flip(f, x, y) {
return f(y)(x);
}
S.flip = def('flip', {}, [Fn(a, Fn(b, c)), b, a, c], flip);

//# flip_ :: ((a, b) -> c) -> b -> a -> c
//.
//. Variant of [`flip`](#flip) which takes an uncurried binary function.
function flip_(f, x, y) {
return f(y, x);
}
S.flip = def('flip', {}, [$.Function([a, b, c]), b, a, c], flip);
S.flip_ = def('flip_', {}, [$.Function([a, b, c]), b, a, c], flip_);

//# lift :: Functor f => (a -> b) -> f a -> f b
//.
Expand Down
18 changes: 0 additions & 18 deletions test/C.js

This file was deleted.

6 changes: 3 additions & 3 deletions test/flip.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ test('flip', function() {

eq(typeof S.flip, 'function');
eq(S.flip.length, 3);
eq(S.flip.toString(), 'flip :: ((a, b) -> c) -> b -> a -> c');
eq(S.flip.toString(), 'flip :: (a -> b -> c) -> b -> a -> c');

eq(map(S.flip(Math.pow, 2))([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25]);
eq(S.flip(S.indexOf, ['a', 'b', 'c', 'd'], 'c'), S.Just(2));
eq(S.flip(S.concat, 'foo', 'bar'), 'barfoo');
eq(map(S.flip(S.concat, '!'))(['BAM', 'POW', 'KA-POW']), ['BAM!', 'POW!', 'KA-POW!']);

});
18 changes: 18 additions & 0 deletions test/flip_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var S = require('..');

var eq = require('./internal/eq');
var map = require('./internal/map');


test('flip_', function() {

eq(typeof S.flip_, 'function');
eq(S.flip_.length, 3);
eq(S.flip_.toString(), 'flip_ :: ((a, b) -> c) -> b -> a -> c');

eq(map(S.flip_(Math.pow, 2))([1, 2, 3, 4, 5]), [1, 4, 9, 16, 25]);
eq(S.flip_(S.indexOf, ['a', 'b', 'c', 'd'], 'c'), S.Just(2));

});

0 comments on commit a82f202

Please sign in to comment.