Skip to content

Instantly share code, notes, and snippets.

@bpinedah
bpinedah / wait-promise.js
Created December 19, 2018 00:23
Pipes article
const wait = delay => {
return new Promise(resolve => {
setTimeout(() => {
resolve('Hello');
}, delay);
});
};
wait(5000).then(r => console.log(r));
@bpinedah
bpinedah / compose-full.js
Last active December 11, 2018 22:29
Currying article
// Data example
const items = [{name: 'Bruce' }, {name: 'Fabs'}, {name: 'Bruce'}, {name: 'Gaby'}];
/**
* @description Filter by one where condition
* @param w Where condition function
* @returns {Function} filter function
*/
const filter = w => a => a.filter(w);
@bpinedah
bpinedah / compose.js
Last active December 11, 2018 22:33
Currying article
/**
* @description Compose function to compose functions
* @param fns Functions to reduce on composition
* @returns {Function} Compose function
*/
const compose = (...fns) => x => fns.reduceRight((f, m) => m(f), x);
// Combine functions
compose(
map(applier('age', 33)),
@bpinedah
bpinedah / without-compose.js
Created December 10, 2018 22:11
Currying article
// Data example
const items = [{name: 'Bruce' }, {name: 'Fabs'}, {name: 'Bruce'}, {name: 'Gaby'}];
/**
* @description Filter by one where condition
* @param w Where condition function
* @returns {Function} filter function
*/
const filter = w => a => a.filter(w);
@bpinedah
bpinedah / filter-map-good-way.js
Created December 10, 2018 21:22
Currying article
const items = [{name: 'Bruce' }, {name: 'Fabs'}, {name: 'Bruce'}, {name: 'Gaby'}];
const filter = w => a => a.filter(w);
const where = (k, v) => i => i[k] === v;
const map = fnMap => f => f.map(fnMap);
const applier = (k, v) => x => x[k] = v;
map(applier('lastname', 'Wayne'))(filter(where('name', 'Bruce'))(items))
console.log(items);
/*
[{ name: 'Bruce', lastname: 'Wayne' },
@bpinedah
bpinedah / filter-map-bad-way.js
Created December 10, 2018 21:14
Currying article
const items = [{name: 'Bruce' }, {name: 'Fabs'}, {name: 'Bruce'}, {name: 'Gaby'}];
const filter = function(key, value){
return function(i){
return i[key] === value;
}
}
const applier = function(key, value){
return function(i){
i[key] = value;
}
@bpinedah
bpinedah / filter-map-ugly-way.js
Last active December 10, 2018 21:08
Currying article
const items = [{name: 'Bruce' }, {name: 'Fabs'}, {name: 'Bruce'}, {name: 'Gaby'}];
items.filter(function(i){
return i.name === 'Bruce';
}).map(function(i){
i.lastname = 'Wayne';
});
console.log(items);
@bpinedah
bpinedah / map-good-way.js
Last active December 10, 2018 20:21
Currying article
const list = [1, 2, 3];
const map = arr => fn => arr.map(fn);
const double = i => i * 2;
map(list)(double); //[2, 4, 6]
@bpinedah
bpinedah / map-bad-way.js
Created December 10, 2018 20:14
Currying article
const list = [1, 2, 3];
function double(i) {
return i * 2;
}
function map (arr, fn) {
return arr.map(fn);
}
map(list, double); //[2, 4, 6]
@bpinedah
bpinedah / map-uggly-way.js
Created December 10, 2018 19:53
Currying article
const list = [1, 2, 3];
function map(arr) {
return arr.map(function double(i){
return i * 2;
})
}
map(list); // [2, 4, 6]