Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Created April 27, 2017 18:46
Show Gist options
  • Select an option

  • Save ktilcu/b53c838e1558cf2aaa35f5f170bb3996 to your computer and use it in GitHub Desktop.

Select an option

Save ktilcu/b53c838e1558cf2aaa35f5f170bb3996 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.22.1/ramda.min.js"></script>
<script src="https://cdn.rawgit.com/zloirock/core-js/master/client/shim.min.js"></script>
<script src="https://wzrd.in/standalone/tape@latest"></script>
<script src="https://wzrd.in/standalone/tap-browser-color@latest"></script>
<script src="https://wzrd.in/standalone/tape-catch@latest"></script>
</head>
<body>
<script>
window.test = tape;
tapBrowserColor();
</script>
<script id="jsbin-javascript">
// This is a regular function
'use strict';
var add = function add(a, b) {
return a + b;
};
add(1, 2); // 3
// This is a curried funciton
var addC = function addC(a) {
return function (b) {
return a + b;
};
};
addC(1)(2); // 3
// The fun part about currying is that you can set up functions
// that are seeded with info so you can reuse them.
var addTen = addC(10);
var devTimeHoursEstimate = addTen(2); // 12
var vacationCostInThousadns = addTen(4); // 14
// Instead of writing all the code to curry functions manually
// we can use some libraries that dynamically curry
var greet = function greet(greeting, greetee) {
return greeting + ' ' + greetee;
};
greet('hello', 'world'); // 'hello world'
var hello = R.curry(greet)('hello');
hello('Dan'); // 'hello Dan'
hello('Kyle'); // 'hello Kyle'
// Often I find that I need to run data through a pipeline. Thats
// where this becomes more useful.
// Imagine a situation where we have a list of numbers.
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
// We want to do some Maths to them. Like add 2 multiply by 3
// subtract 1 and divide by 2.
// There is one school of thought where we make a for loop and
// iterate while doing all the operations. First run:
var forMathNumbers = [];
for (var i = -1, len = numbers.length; ++i < len;) {
var mathed = ((numbers[i] + 2) * 3 - 1) / 2;
forMathNumbers.push(mathed);
}
// Instead, I think we compose functions. We start by mapping
// through the array.
var mathedNumbers = numbers.map(function (number) {
return number + 2;
}).map(function (number) {
return number * 3;
}).map(function (number) {
return number - 1;
}).map(function (number) {
return number / 2;
});
// [4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5]
// Thats fine and dandy but none of these functions can be reused
// for future maths. Lets fix that.
var ad = R.curry(function (a, b) {
return a + b;
});
var sub = R.curry(function (a, b) {
return a - b;
});
var mult = R.curry(function (a, b) {
return a * b;
});
// using curryRight which provides arguments from right to left
// because we know the divisor and will get the dividend later.
// We should change the signature of the function to b,a or do
// b/a. Really i just wanted to explain curryRight too.
var div = R.curryRight(function (a, b) {
return a / b;
});
var reMathedNumbers = numbers.map(ad(2)).map(mult(3)).map(sub(1)).map(div(2));
// [4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5]
// Now we have reusable functions and concuse and readable code.
// Thanks to curry. Now I'm hungry. Maybe some Thai.
</script>
<script id="jsbin-source-javascript" type="text/javascript">// This is a regular function
const add = (a, b) => a + b;
add(1,2) // 3
// This is a curried funciton
const addC = a => b => a + b;
addC(1)(2) // 3
// The fun part about currying is that you can set up functions
// that are seeded with info so you can reuse them.
var addTen = addC(10);
var devTimeHoursEstimate = addTen(2); // 12
var vacationCostInThousadns = addTen(4) // 14
// Instead of writing all the code to curry functions manually
// we can use some libraries that dynamically curry
const greet = (greeting, greetee) => `${greeting} ${greetee}`;
greet('hello','world'); // 'hello world'
const hello = R.curry(greet)('hello');
hello('Dan') // 'hello Dan'
hello('Kyle') // 'hello Kyle'
// Often I find that I need to run data through a pipeline. Thats
// where this becomes more useful.
// Imagine a situation where we have a list of numbers.
const numbers = [1,2,3,4,5,6,7,8];
// We want to do some Maths to them. Like add 2 multiply by 3
// subtract 1 and divide by 2.
// There is one school of thought where we make a for loop and
// iterate while doing all the operations. First run:
var forMathNumbers = [];
for (var i = -1, len = numbers.length; ++i < len;) {
var mathed = ((((numbers[i] + 2) * 3) - 1) / 2);
forMathNumbers.push(mathed);
}
// Instead, I think we compose functions. We start by mapping
// through the array.
const mathedNumbers = numbers
.map(number => number + 2)
.map(number => number * 3)
.map(number => number - 1)
.map(number => number / 2);
// [4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5]
// Thats fine and dandy but none of these functions can be reused
// for future maths. Lets fix that.
const ad = R.curry((a,b) => a+b);
const sub = R.curry((a,b) => a-b);
const mult = R.curry((a,b) => a*b);
// using curryRight which provides arguments from right to left
// because we know the divisor and will get the dividend later.
// We should change the signature of the function to b,a or do
// b/a. Really i just wanted to explain curryRight too.
const div = R.curryRight((a,b) => a/b);
const reMathedNumbers = numbers
.map(ad(2))
.map(mult(3))
.map(sub(1))
.map(div(2));
// [4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5]
// Now we have reusable functions and concuse and readable code.
// Thanks to curry. Now I'm hungry. Maybe some Thai.</script></body>
</html>
// This is a regular function
'use strict';
var add = function add(a, b) {
return a + b;
};
add(1, 2); // 3
// This is a curried funciton
var addC = function addC(a) {
return function (b) {
return a + b;
};
};
addC(1)(2); // 3
// The fun part about currying is that you can set up functions
// that are seeded with info so you can reuse them.
var addTen = addC(10);
var devTimeHoursEstimate = addTen(2); // 12
var vacationCostInThousadns = addTen(4); // 14
// Instead of writing all the code to curry functions manually
// we can use some libraries that dynamically curry
var greet = function greet(greeting, greetee) {
return greeting + ' ' + greetee;
};
greet('hello', 'world'); // 'hello world'
var hello = R.curry(greet)('hello');
hello('Dan'); // 'hello Dan'
hello('Kyle'); // 'hello Kyle'
// Often I find that I need to run data through a pipeline. Thats
// where this becomes more useful.
// Imagine a situation where we have a list of numbers.
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
// We want to do some Maths to them. Like add 2 multiply by 3
// subtract 1 and divide by 2.
// There is one school of thought where we make a for loop and
// iterate while doing all the operations. First run:
var forMathNumbers = [];
for (var i = -1, len = numbers.length; ++i < len;) {
var mathed = ((numbers[i] + 2) * 3 - 1) / 2;
forMathNumbers.push(mathed);
}
// Instead, I think we compose functions. We start by mapping
// through the array.
var mathedNumbers = numbers.map(function (number) {
return number + 2;
}).map(function (number) {
return number * 3;
}).map(function (number) {
return number - 1;
}).map(function (number) {
return number / 2;
});
// [4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5]
// Thats fine and dandy but none of these functions can be reused
// for future maths. Lets fix that.
var ad = R.curry(function (a, b) {
return a + b;
});
var sub = R.curry(function (a, b) {
return a - b;
});
var mult = R.curry(function (a, b) {
return a * b;
});
// using curryRight which provides arguments from right to left
// because we know the divisor and will get the dividend later.
// We should change the signature of the function to b,a or do
// b/a. Really i just wanted to explain curryRight too.
var div = R.curryRight(function (a, b) {
return a / b;
});
var reMathedNumbers = numbers.map(ad(2)).map(mult(3)).map(sub(1)).map(div(2));
// [4, 5.5, 7, 8.5, 10, 11.5, 13, 14.5]
// Now we have reusable functions and concuse and readable code.
// Thanks to curry. Now I'm hungry. Maybe some Thai.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment