Skip to content

Instantly share code, notes, and snippets.

@cwg999
Created August 4, 2017 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwg999/2c80b4fc86fb9d0f956aa45b39178aa7 to your computer and use it in GitHub Desktop.
Save cwg999/2c80b4fc86fb9d0f956aa45b39178aa7 to your computer and use it in GitHub Desktop.
Currying Example
(function (){
const double = (x) => x*2
const output = [1,2,3].map(double);
console.log(output) // [2,4,6]
// Currying
const mult = (i) => (x) => x*i
let aMod = (arr) => (mult) => arr.map(mult);
const arr = [1,2,3];
console.log(aMod(arr)(mult(2)));
console.log(aMod(arr)(mult(3)));
// Currying alternative?
console.log(arr.map(x=>x*2))
console.log(arr.map(x=>x*3))
// Seems nicer to just use map, but see here:
const ijk = (i,j,k) => (x) => [x*i,x*j,x*k];
console.log(aMod(arr)(ijk(1,2,3)));
console.log(aMod(arr)(ijk(2,3,4)));
// versus
console.log(arr.map(x=>[x*1,x*2,x*3]))
console.log(arr.map(x=>[x*2,x*3,x*4]))
// lets change aMod a tiny bit to support multiple arrays
const aaMod = (aarr) => (fn) => aarr.map(e=>aMod(e)(fn));
arrays = [
[1,2,3],
[4,5,6]
];
console.log(aaMod(arrays)(ijk(1,2,3)))
console.log(aaMod(arrays)(ijk(2,3,4)))
// Yeah... I'm not going to write this one in basic maps.
// If you were doing any sort of linear algebra this
// would start to become very useful.
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment