Skip to content

Instantly share code, notes, and snippets.

@programadorabordo
Last active August 25, 2020 02:04
Show Gist options
  • Save programadorabordo/d1b89e60406215beeb0a6d275557f79a to your computer and use it in GitHub Desktop.
Save programadorabordo/d1b89e60406215beeb0a6d275557f79a to your computer and use it in GitHub Desktop.
Código da aula ao vivo sobre pointfree https://youtu.be/hCfiTBwhBqo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pointfree Style | JS Day | Programador a Bordo</title>
</head>
<body>
<script>
// Formas de criar funções da forma tradicional
/* function soma(a, b) {
return a + b;
}
const soma2 = function(a, b) {
return a + b;
};
const soma3 = (a, b) => a + b; */
function add(a) {
return function add2(b) {
return a + b;
}
}
const inc = add(1);
const result = inc(10);
console.group('add');
console.log(result);
console.log(inc(13));
console.log(inc(26));
console.log(inc(7));
const inc10 = add(10);
console.log(inc10(4));
console.log(inc10(11));
console.groupEnd();
function multiply(a) {
return function(b) {
return a * b;
}
}
console.group('multiply')
console.log(multiply(10)(2));
const double = multiply(2);
console.log(double(30));
console.groupEnd();
console.group('pointfree como argumento')
const numeros = [3, 5, 10, 7, 18, 9, 4];
const numerosDuplicados = numeros.map(double);
console.log(numerosDuplicados);
function isEven (valor) {
return (valor % 2) === 0;
}
const numerosPares = numeros.filter(isEven);
console.log(numerosPares);
console.groupEnd();
console.group('request');
function toJson(d) {
return d.json()
}
/*
[].map(function() {})
*/
function map(fn) {
return function(array) {
return array.map(fn);
}
}
function extract(post) {
return { id: post.id, title: post.title }
}
fetch('https://jsonplaceholder.typicode.com/posts')
.then(toJson)
.then(map(extract))
.then(console.log)
console.groupEnd('request');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment