Skip to content

Instantly share code, notes, and snippets.

@indongyoo
Created June 22, 2019 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indongyoo/d081556f8596145ad5e608b7d7137a75 to your computer and use it in GitHub Desktop.
Save indongyoo/d081556f8596145ad5e608b7d7137a75 to your computer and use it in GitHub Desktop.
const log = a => console.log(a);
// 0.
// map :: Functor f => (a -> b) -> f a -> f b
// 1. Array.prototype.map
// Array r => (a -> b) -> r a -> r b
// [1] : r a
// a => a + 1 : (a -> b)
// rb : r b
// forEach : r b 안에 있는 값 b에 접근
const rb = [1].map(a => a + 1);
rb.forEach(b => log(b)); // 2
const rb2 = [1].map(a => [a + 1]);
rb2.forEach(log); // [2]
rb2.forEach(fb => fb.forEach(log)); // 2
// 2. Array.prototype.flatMap
// Array r => (a -> b) -> r a -> r b
// Array r => (a -> r b) -> r a -> r b
const rb3 = [1].flatMap(a => a + 1);
rb3.forEach(b => log(b)); // 2
const rb4 = [1].flatMap(a => [a + 1]);
rb4.forEach(b => log(b)); // 2
const rb5 = [1].flatMap(a => [[a + 1]]);
rb5.forEach(b => log(b)); // [2]
// 3. Promise.prototype.then
// Promise p => (a -> b) -> p a -> p b
// Promise p => (a -> p b) -> p a -> p b
// Promise p => (a -> p p b) -> p a -> p b
// Promise.resolve(1) : p a
const pb = Promise.resolve(1).then(a => a + 1);
pb.then(b => log(b)); // 2
const pb2 = Promise.resolve(1).then(a => Promise.resolve(a + 1));
pb2.then(b => log(b)); // 2
const pb3 = Promise.resolve(1).then(a => Promise.resolve(Promise.resolve(a + 1)));
pb3.then(b => log(b)); // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment