diff --git a/README.md b/README.md index 307e140..3686be4 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ PRs are welcome! - [pipe](#pipe) - [shave](#shave) - [curry2,3,4](#curry234) + - [curryRight2](#curryright2) - [uncurry2,3,4](#uncurry234) - [flip](#flip) - [implode](#implode) @@ -375,6 +376,22 @@ gβ(1)(2, 3)(4); // => 3 gβ(1, 2)(3)(4); // => 3 ``` +### curryRight2 + +Curry a function from the right – split its parameters into 2 lists. Apply the second list of parameters first, without changing the order within the lists. + +```js +const curryRight2 = require('1-liners/curryRight2'); + +const g = (a, b, c, d) => a + b * c - d; +g(1, 2, 3, 4); // => 3 + +const gλ = curryRight2(g); +gλ(4)(1, 2, 3); // => 3 +gλ(3, 4)(1, 2); // => 3 +gλ(2, 3, 4)(1); // => 3 +``` + ### uncurry2,3,4 Uncurry a function – collapse 2, 3 or 4 lists of parameters into one. diff --git a/module/curryRight2.js b/module/curryRight2.js new file mode 100644 index 0000000..858eab5 --- /dev/null +++ b/module/curryRight2.js @@ -0,0 +1 @@ +export default (f) => (...a) => (...b) => f(...b, ...a); diff --git a/tests/curryRight2.js b/tests/curryRight2.js new file mode 100644 index 0000000..7dfd2e6 --- /dev/null +++ b/tests/curryRight2.js @@ -0,0 +1,20 @@ +import {equal} from 'assert'; +import curryRight2 from '../curryRight2'; + +test('#curryRight2', () => { + const g = (a, b, c, d) => a + b * c - d; + const gλ = curryRight2(g); + + equal( + gλ(4)(1, 2, 3), + 3 + ); + equal( + gλ(3, 4)(1, 2), + 3 + ); + equal( + gλ(2, 3, 4)(1), + 3 + ); +});