Created
July 2, 2016 11:07
-
-
Save fmsf/412e4c161c52920207e95fc2ce8ae6fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sum = (a, b) => a + b; | |
const not = (value) => !value; | |
const isEven = (number) => number % 2 == 0; | |
console.log("sum(4, 2) == " + sum(4,2)); | |
console.log("not(true) == " + not(true)); | |
console.log("isEven(4) == " + isEven(4)); | |
console.log("isEven(7) == " + isEven(7)); | |
const twice = (fn) => (a) => fn(a, a); | |
const double = twice(sum); | |
console.log("double(5) == " + double(2)); | |
console.log("double(5) == " + double(5)); | |
console.log("double(5) == " + double(10)); | |
const apply = (fn1) => (fn2) => (number) => fn1(fn2(number)); | |
const negate = apply(not); | |
const isOdd = negate(isEven); | |
console.log("isOdd(4) == " + isOdd(4)); | |
console.log("isOdd(7) == " + isOdd(7)); | |
console.log("isOdd(4) == " + apply(not)(isEven)(4)); | |
console.log("isOdd(7) == " + apply(not)(isEven)(7)); | |
/* | |
$.get("/getUser", { | |
userName: "maria" | |
}, (data) => setPageTitle(data.fullName)); | |
// user properties library | |
const getFullName = (fn) => (data) => fn(data.fullName); | |
$.get("/getUser", { userName: "maria" }, getFullName(setPageTitle)); | |
*/ | |
const sum2 = (a) => (b) => a + b; | |
console.log("sum2(4)(2) == " + sum2(4)(2)); | |
add10 = sum2(10); | |
console.log("add10(5) == " + add10(5)); | |
const biggerThan = (a) => (b) => a < b; | |
console.log("biggerThan(1)(10) == " + biggerThan(1)(10)); | |
const map = (fn) => (list) => { | |
let newList = []; | |
for( let i = 0; i < list.length; i++) { | |
newList.push(fn(list[i])); | |
} | |
return newList; | |
}; | |
console.log("map(add10)([1,2,3]) == " + map(add10)([1,2,3])); | |
const add10ToList = map(add10); | |
console.log("add10ToList([5,10,15]) == " + add10ToList([5,10,15])); | |
const reduce = (fn) => (list) => { | |
let result = fn(list[0], list[1]); | |
for( let i = 2; i < list.length; i++) { | |
result = fn(result, list[i]); | |
} | |
return result; | |
}; | |
console.log("reduce(sum)([1,2,3,4,5]) == " + reduce(sum)([1,2,3,4,5])); | |
const sumAll = reduce(sum); | |
console.log("sumAll([1,2,3,4,5]) == " + sumAll([1,2,3,4,5])); | |
const filter = (fn) => (list) => { | |
let newList = []; | |
for(let i = 0; i < list.length; i++) { | |
if(fn(list[i])) { | |
newList.push(list[i]); | |
} | |
} | |
return newList; | |
}; | |
const getOdd = filter(isOdd); | |
console.log("getOdd([1,2,3,4,5]) == " + getOdd([1,2,3,4,5])); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
talk given in http://commitporto.com/ 2016