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

Curry: Add functions curry2 through curry5 #289

Merged
merged 1 commit into from
Nov 21, 2016
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
84 changes: 84 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
var b = $.TypeVariable('b');
var c = $.TypeVariable('c');
var d = $.TypeVariable('d');
var e = $.TypeVariable('e');
var f = $.TypeVariable('f');
var l = $.TypeVariable('l');
var r = $.TypeVariable('r');
Expand Down Expand Up @@ -632,6 +633,89 @@

//. ### Function

//# curry2 :: ((a, b) -> c) -> a -> b -> c
//.
//. Curries the given binary function.
//.
//. ```javascript
Copy link
Member

Choose a reason for hiding this comment

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

Please include an "empty" (//.) line between the description and the code block.

//. > R.map(S.curry2(Math.pow)(10), [1, 2, 3])
//. [10, 100, 1000]
//.
//. > R.map(S.curry2(Math.pow, 10), [1, 2, 3])
//. [10, 100, 1000]
//. ```
function curry2(f, x, y) {
return f(x, y);
}
S.curry2 = def('curry2', {}, [$.Function, a, b, c], curry2);

//# curry3 :: ((a, b, c) -> d) -> a -> b -> c -> d
//.
//. Curries the given ternary function.
//.
//. ```javascript
//. > global.replaceString = S.curry3((what, replacement, string) =>
//. . string.replace(what, replacement)
//. . )
//. replaceString
//.
//. > replaceString('banana')('orange')('banana icecream')
//. 'orange icecream'
//.
//. > replaceString('banana', 'orange', 'banana icecream')
//. 'orange icecream'
//. ```
function curry3(f, x, y, z) {
return f(x, y, z);
}
S.curry3 = def('curry3', {}, [$.Function, a, b, c, d], curry3);

//# curry4 :: ((a, b, c, d) -> e) -> a -> b -> c -> d -> e
//.
//. Curries the given quaternary function.
//.
//. ```javascript
//. > global.createRect = S.curry4((x, y, width, height) =>
//. . ({x, y, width, height})
//. . )
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer this formatting:

> global.createRect = S.curry4((x, y, width, height) =>
.   ({x, y, width, height})
. )

//. createRect
//.
//. > createRect(0)(0)(10)(10)
//. {x: 0, y: 0, width: 10, height: 10}
//.
//. > createRect(0, 0, 10, 10)
//. {x: 0, y: 0, width: 10, height: 10}
//. ```
function curry4(f, w, x, y, z) {
return f(w, x, y, z);
}
S.curry4 = def('curry4', {}, [$.Function, a, b, c, d, e], curry4);

//# curry5 :: ((a, b, c, d, e) -> f) -> a -> b -> c -> d -> e -> f
//.
//. Curries the given quinary function.
//.
//. ```javascript
//. > global.toUrl = S.curry5((protocol, creds, hostname, port, pathname) =>
//. . protocol + '//' +
//. . S.maybe('', _ => _.username + ':' + _.password + '@', creds) +
//. . hostname +
//. . S.maybe('', S.concat(':'), port) +
//. . pathname
//. . )
//. toUrl
//.
//. > toUrl('https:')(S.Nothing)('example.com')(S.Just('443'))('/foo/bar')
//. 'https://example.com:443/foo/bar'
//.
//. > toUrl('https:', S.Nothing, 'example.com', S.Just('443'), '/foo/bar')
//. 'https://example.com:443/foo/bar'
//. ```
Copy link
Member

Choose a reason for hiding this comment

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

Here's an example which does not seem contrived:

> global.toUrl = S.curry5((protocol, creds, hostname, port, pathname) =>
.   protocol + '//' +
.   S.maybe('', _ => _.username + ':' + _.password + '@', creds) +
.   hostname +
.   S.maybe('', S.concat(':'), port) +
.   pathname
. )

> toUrl('https:')(S.Nothing)('example.com')(S.Just('443'))('/foo/bar')
'https://example.com:443/foo/bar'

> toUrl('https:', S.Nothing, 'example.com', S.Just('443'), '/foo/bar')
'https://example.com:443/foo/bar'

function curry5(f, v, w, x, y, z) {
return f(v, w, x, y, z);
}
S.curry5 = def('curry5', {}, [$.Function, a, b, c, d, e, r], curry5);

//# flip :: ((a, b) -> c) -> b -> a -> c
//.
//. Takes a binary function and two values, and returns the result of
Expand Down
17 changes: 17 additions & 0 deletions test/curry2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var S = require('..');

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


test('curry2', function() {

eq(typeof S.curry2, 'function');
eq(S.curry2.length, 3);

var curried = S.curry2(function(x, y) { return x + y; });
eq(curried(1, 2), 3);
eq(curried(1)(2), 3);

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

var S = require('..');

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


test('curry3', function() {

eq(typeof S.curry3, 'function');
eq(S.curry3.length, 4);

var curried = S.curry3(function(x, y, z) { return x + y + z; });
eq(curried(1, 2, 3), 6);
eq(curried(1, 2)(3), 6);
eq(curried(1)(2, 3), 6);
Copy link
Member

Choose a reason for hiding this comment

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

For consistency, let's use the script to generate the assertions for curry2 and curry3 as well. You have all the combinations, but my obsessive streak wants to see them consistently ordered. 😜

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also changed them to use + instead of * in the curried function. ;)

Copy link
Member

Choose a reason for hiding this comment

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

Works for me. 👍

eq(curried(1)(2)(3), 6);

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

var S = require('..');

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


test('curry4', function() {

eq(typeof S.curry4, 'function');
eq(S.curry4.length, 5);

var curried = S.curry4(function(w, x, y, z) { return w + x + y + z; });
eq(curried(1, 2, 3, 4), 10);
eq(curried(1, 2, 3)(4), 10);
eq(curried(1, 2)(3, 4), 10);
eq(curried(1, 2)(3)(4), 10);
eq(curried(1)(2, 3, 4), 10);
eq(curried(1)(2, 3)(4), 10);
eq(curried(1)(2)(3, 4), 10);
eq(curried(1)(2)(3)(4), 10);

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

var S = require('..');

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


test('curry5', function() {

eq(typeof S.curry5, 'function');
eq(S.curry5.length, 6);

var curried = S.curry5(function(v, w, x, y, z) { return v + w + x + y + z; });
eq(curried(1, 2, 3, 4, 5), 15);
eq(curried(1, 2, 3, 4)(5), 15);
eq(curried(1, 2, 3)(4, 5), 15);
eq(curried(1, 2, 3)(4)(5), 15);
eq(curried(1, 2)(3, 4, 5), 15);
eq(curried(1, 2)(3, 4)(5), 15);
eq(curried(1, 2)(3)(4, 5), 15);
eq(curried(1, 2)(3)(4)(5), 15);
eq(curried(1)(2, 3, 4, 5), 15);
eq(curried(1)(2, 3, 4)(5), 15);
eq(curried(1)(2, 3)(4, 5), 15);
Copy link
Member

Choose a reason for hiding this comment

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

We might as well enumerate all the combinations. I wrote a script to make this easier:

'use strict';

const pow2 = exp => Math.pow(2, exp);

const generateAssertions = (xs, f) => {
  const l = xs.length;
  return Array(pow2(l - 1)).join('x').split('x').reduce((s, _, m) =>
    xs.reduce((s, x, n) =>
      `${s}${x}${n === l - 1 ? '' : m % pow2(l - n - 1) >= pow2(l - n - 2) ? ')(' : ', '}`,
      `${s}  eq(curried(`
    ) + `), ${f(...xs)});\n`,
    ''
  );
};

process.stdout.write(generateAssertions([1, 2, 3, 4, 5], (v, w, x, y, z) => v + w + x + y + z));
$ node generate-assertions.js
  eq(curried(1, 2, 3, 4, 5), 15);
  eq(curried(1, 2, 3, 4)(5), 15);
  eq(curried(1, 2, 3)(4, 5), 15);
  eq(curried(1, 2, 3)(4)(5), 15);
  eq(curried(1, 2)(3, 4, 5), 15);
  eq(curried(1, 2)(3, 4)(5), 15);
  eq(curried(1, 2)(3)(4, 5), 15);
  eq(curried(1, 2)(3)(4)(5), 15);
  eq(curried(1)(2, 3, 4, 5), 15);
  eq(curried(1)(2, 3, 4)(5), 15);
  eq(curried(1)(2, 3)(4, 5), 15);
  eq(curried(1)(2, 3)(4)(5), 15);
  eq(curried(1)(2)(3, 4, 5), 15);
  eq(curried(1)(2)(3, 4)(5), 15);
  eq(curried(1)(2)(3)(4, 5), 15);
  eq(curried(1)(2)(3)(4)(5), 15);

You could run this for each of the other 🍛 functions as well.

eq(curried(1)(2, 3)(4)(5), 15);
eq(curried(1)(2)(3, 4, 5), 15);
eq(curried(1)(2)(3, 4)(5), 15);
eq(curried(1)(2)(3)(4, 5), 15);
eq(curried(1)(2)(3)(4)(5), 15);

});
Copy link
Member

Choose a reason for hiding this comment

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

Could we have an empty line after the last assertion? Sorry for the pedantry. No more requests, I promise!

Please use git commit --amend when making the change. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I deserve every word of it.