Skip to content

Instantly share code, notes, and snippets.

View haingdc's full-sized avatar

Hai.Nguyen Github haingdc

View GitHub Profile
@haingdc
haingdc / reduce.js
Created February 28, 2018 10:49
A Gentle Introduction to Functional JavaScript: Part 2
var reduce = function(callback, initialValue, array) {
var output = initialValue;
for (var i = 0; i < array.length; i = i + 1) {
output = callback(output, array[i]);
}
return output;
};
@haingdc
haingdc / call_reduce.js
Created February 28, 2018 10:50
A Gentle Introduction to Functional JavaScript: Part 2
var total = reduce(add, 0, numbers);
var sentence = reduce(joinWord, '', words);
@haingdc
haingdc / buildIn_reduce.js
Created February 28, 2018 10:51
A Gentle Introduction to Functional JavaScript: Part 2
var total = numbers.reduce(add, 0);
var sentence = words.reduce(joinWord, '');
@haingdc
haingdc / ponies.js
Created February 28, 2018 10:56
A Gentle Introduction to Functional JavaScript: Part 2
var ponies = [
[
['name', 'Fluttershy'],
['image', 'http://tinyurl.com/gpbnlf6'],
['description', 'Fluttershy is a female Pegasus pony and one of the main characters of My Little Pony Friendship is Magic.']
],
[
['name', 'Applejack'],
['image', 'http://tinyurl.com/gkur8a6'],
['description', 'Applejack is a female Earth pony and one of the main characters of My Little Pony Friendship is Magic.']
@haingdc
haingdc / addToObject.js
Created February 28, 2018 10:57
A Gentle Introduction to Functional JavaScript: Part 2
var addToObject = function(obj, arr) {
obj[arr[0]] = arr[1]; // thêm thuộc tính cho obj
return obj;
};
@haingdc
haingdc / ponyArrayToObject.js
Created February 28, 2018 10:59
A Gentle Introduction to Functional JavaScript: Part 2
var ponyArrayToObject = function(ponyArray) {
return reduce(addToObject, {}, ponyArray);
};
@haingdc
haingdc / tinyPonies.js
Created February 28, 2018 11:01
A Gentle Introduction to Functional JavaScript: Part 2
var tidyPonies = map(ponyArrayToObject, ponies);
@haingdc
haingdc / ThomasFuchTemplate.js
Created February 28, 2018 11:07
A Gentle Introduction to Functional JavaScript: Part 2
function t(s,o){
for(var p in o)
s=s.replace(new RegExp('{'+p+'}','g'), o[p]);
return s;
}
var data = { name: "July", age: "seventeen" };
t("When {name} was just {age}!", data);
// "When July was just seventeen!"
@haingdc
haingdc / ponyToListItem.js
Created February 28, 2018 11:08
A Gentle Introduction to Functional JavaScript: Part 2
var ponyToListItem = function(pony) {
var template = '<li><img src="{image}" alt="{name}"/>' +
'<div><h3>{name}</h3><p>{description}</p>' +
'</div></li>';
return t(template, pony);
}
@haingdc
haingdc / ponyList.js
Created February 28, 2018 11:09
A Gentle Introduction to Functional JavaScript: Part 2
var ponyList = map(ponyToListItem, tidyPonies);
var html = '<ul>' + reduce(joinWord, '', ponyList) + '</ul>';