Skip to content

Instantly share code, notes, and snippets.

@haingdc
haingdc / rabbit.js
Created March 13, 2018 04:59
A Gentle Introduction to Functional JavaScript: Part 3
var getWhiteRabbit = document.getElementById.bind(document, 'white-rabbit');
var rabbit = getWhiteRabbit();
@haingdc
haingdc / batstar.js
Created March 13, 2018 05:00
A Gentle Introduction to Functional JavaScript: Part 3
var twinkleBat = twinkle.bind(null, 'bat');
var twinkleStar = twinkle.bind(null, 'star', 'are');
@haingdc
haingdc / composeTwo.js
Created March 13, 2018 05:01
A Gentle Introduction to Functional JavaScript: Part 3
var composeTwo = function(funA, funB) {
return function(x) {
return funA(funB(x));
}
}
var nohow = function(sentence) {
return sentence + ', nohow!';
}
var contrariwise = function(sentence) {
@haingdc
haingdc / compose.js
Created March 13, 2018 05:02
A Gentle Introduction to Functional JavaScript: Part 3
var compose = function() {
var args = arguments;
var start = args.length - 1;
return function() {
var i = start;
var result = args[start].apply(this, arguments);
i = i - 1;
while (i >= 0) {
result = args[i].call(this, result);
i = i - 1;
@haingdc
haingdc / func.js
Created March 13, 2018 05:03
A Gentle Introduction to Functional JavaScript: Part 3
var funC = compose(contrariwise, nohow);
@haingdc
haingdc / poem.js
Created March 13, 2018 05:04
A Gentle Introduction to Functional JavaScript: Part 3
var poem = 'Twas brillig, and the slithy toves\n' +
'Did gyre and gimble in the wabe;\n' +
'All mimsy were the borogoves,\n' +
'And the mome raths outgrabe.';
@haingdc
haingdc / partialPoem.js
Created March 13, 2018 05:05
A Gentle Introduction to Functional JavaScript: Part 3
var replace = function(find, replacement, str) {
return str.replace(find, replacement);
}
var wrapWith = function(tag, str) {
return '<' + tag + '>' + str + '</' + tag + '>';
}
var addBreaks = partial(replace, '\n', '<br/>\n');
var replaceBrillig = partial(replace, 'brillig', 'four o’clock in the afternoon');
@haingdc
haingdc / modifyPoem.js
Created March 13, 2018 05:05
A Gentle Introduction to Functional JavaScript: Part 3
var modifyPoem = pipe(replaceBrillig, addBreaks, wrapP, wrapBlockquote);
@haingdc
haingdc / formatNameCurry.js
Created March 13, 2018 05:06
A Gentle Introduction to Functional JavaScript: Part 3
var formatName = function(firstname, surname, nickname) {
return firstname + ' “' + nickname + '” ' + surname;
}
var formatNameCurried = curry(formatName); // 🍛 cơm cà-ri
var james = formatNameCurried('James'); // ⚓ fix firstname
console.log(james('Sinclair', 'Mad Hatter')); // 📞 gọi với surname, nickname
//=> James “Mad Hatter” Sinclair
@haingdc
haingdc / formatNameCurried.js
Created March 13, 2018 05:07
A Gentle Introduction to Functional JavaScript: Part 3
formatNameCurried('a'); // ‘formatName’ không được gọi, trả về hàm cà-ri
formatNameCurried('a')('b'); // ‘formatName’ không được gọi, trả về hàm cà-ri
formatNameCurried('a', 'b'); // ‘formatName’ không được gọi, trả về hàm cà-ri
formatNameCurried('a')('b')('c'); // ⇨ a “c” b
formatNameCurried('a', 'b', 'c'); // ⇨ a “c” b
formatNameCurried('a', 'b')('c'); // ⇨ a “c” b
formatNameCurried('a')('b', 'c'); // ⇨ a “c” b