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
// String | |
console.log( | |
map(a => a + 1, "12") | |
); | |
// ["11", "21"] | |
// NodeList | |
console.log( | |
map(el => el.nodeName, document.querySelectorAll('head *')) | |
); |
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
// Iterator 2 | |
console.log( | |
map( | |
([key, val]) => [key.toUpperCase(), val + 1], | |
new Map([['a', 1], ['b', 2]]).entries()) | |
); | |
// [["A", 2], ["B", 3]] | |
console.log( | |
new Map(map( |
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 { reduce, log } = Functional; | |
const add = (a, b) => a + b; | |
log( reduce(add, [1, 2]) ); // 3 | |
log( reduce(add, 0, [1, 2]) ); // 3 | |
log( reduce(add, 10, [1, 2]) ); // 13 | |
log( reduce(add, 100, { a: 1, b: 2 }) ); // 103 | |
log( reduce(add, 200, new Map([['a', 1], ['b', 2]])) ); // 203 | |
log( reduce(add, 300, new Set([1, 2])) ); // 303 |
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 info = { | |
title: "Function", | |
list: new Map([ | |
['map', 'Functor f => (a -> b) - f a -> f b'], | |
['reduce', 'Collection c => ((a, b) -> b) -> b -> c a -> a'] | |
]) | |
}; | |
log( | |
reduce( |
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 isAny = a => a !== undefined; | |
const some = map(isAny, find); | |
log( some(a => a > 2, [1, 2, 4, 5]) ); | |
// true | |
log( some(a => a < 2, [1, 2, 4, 5]) ); | |
// true | |
log( some(a => a > 10, [1, 2, 4, 5]) ); | |
// false |
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 { match } = Functional; | |
const a = 2; | |
const b = match (a) | |
.case(1) (_=> '1이군요!') | |
.case(2) (_=> '2네요?') | |
.else (_=> '1도 2도 아니군요.'); | |
console.log(b); |
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 f2 = a => | |
match (a) | |
.case(1) ( | |
a => a + 10, | |
a => a + 100, | |
a => a + 1000) | |
.case(2) ( | |
a => a + 20, | |
a => a + 200, | |
a => a + 2000) |
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 { isMatch } = Functional; | |
console.log( isMatch({ a: 1 }, { a: 1, b: 2 }) ); // true | |
console.log( isMatch([2], [1, 2, 3]) ); // true | |
console.log( isMatch(5, 5) ); // true | |
console.log( isMatch("a", "a") ); // true | |
console.log( isMatch({ a: 1, b: 3 }, { a: 1, b: 2 }) ); // false | |
console.log( isMatch(5, 50) ); // false | |
console.log( isMatch("a", "aaa") ); // false |
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 { map, log } = Functional; | |
function some1(f, coll) { | |
const bools = map(f, coll); | |
for (const bool of bools) if (bool) return true; | |
return false; | |
} | |
log( some1(a => a > 15, [10, 20, 30, 40]) ); | |
// true |
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
function some2(f, coll) { | |
for (const val of coll) if (f(val)) return true; | |
return false; | |
} | |
log( some2(a => a > 15, [10, 20, 30, 40]) ); | |
// true |