Last active
April 17, 2017 15:14
-
-
Save MarcoWorms/39e9c62a13be879158d6e2e7fb4cb04e 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
// -------------------------------------------------------- | |
// "No javascript qualquer variável pode ser uma função ou o resultado de uma função" | |
function umaFuncao () { | |
console.log('faz coisas aqui dentro') | |
return 'e sempre deveria retornar alguma coisa' | |
} | |
const funcao = umaFuncao | |
funcao === umaFuncao // true | |
const resultadoDeUmaFuncao = funcao() // 'e sempre deveria retornar alguma coisa' | |
// -------------------------------------------------------- | |
// Imutabilidade e Funções puras | |
var contador = 0 | |
function incrementar () { | |
contador = contador + 1 | |
return contador | |
} | |
incrementar() // 1 | |
incrementar() // 2 | |
incrementar() // 3 | |
function incrementarPuro (counter) { | |
return counter + 1 | |
} | |
incrementarPuro(0) // 1 | |
incrementarPuro(1) // 2 | |
incrementarPuro(2) // 3 | |
incrementarPuro(0) // 1 | |
incrementarPuro(incrementarPuro(incrementarPuro(0))) // 3 | |
function tambemPura (counter) { | |
// counter não imutável | |
counter = incrementarPuro(counter) | |
counter = incrementarPuro(counter) | |
counter = incrementarPuro(counter) | |
return counter | |
} | |
tambemPura(0) // 3 | |
function maisUmaPura (counter) { | |
// counter imutável! | |
return incrementarPuro(incrementarPuro(incrementarPuro(counter))) | |
} | |
maisUmaPura(0) // 3 | |
// -------------------------------------------------------- | |
// This + Javascript = wtf | |
function logThis (x, y) { | |
console.log(this, x, y) | |
} | |
logThis() // Window undefined undefined | |
function logThisStrict (x, y) { | |
'use strict' | |
console.log(this, x, y) | |
} | |
logThisStrict() // undefined undefined undefined | |
const logThisBound = logThis.bind('algo') | |
logThisBound() // 'algo' undefined undefined | |
const logThat = logThis.bind(null, 'laranja') | |
logThat() // null laranja undefined | |
const anotherLogThat = logThat.bind(null, 'verde') | |
anotherLogThat() // null laranja verde | |
// -------------------------------------------------------- | |
// Função de soma + bind para fazer aplicação parcial de parâmetros | |
function addSemArrowFunction (x, y) { | |
return x + y | |
} | |
const add = (x, y) => x + y | |
add(2, 3) // 5 | |
const add5 = add.bind(null, 5) | |
add5(10) // 15 | |
// Sem bind | |
const addCurried = x => y => x + y | |
// function addCurried (x) { | |
// return function (y) { | |
// return x + y | |
// } | |
// } | |
const add6 = addCurried(6) | |
add6(10) // 16 | |
addCurried(2)(3) // 5 | |
addCurried(2, 3) // y => x + y | |
// Seria bom se os 2 funcionassem :) | |
// -------------------------------------------------------- | |
// Ramda - Todas as funções são puras, não mutam a entrada, e sempre retornam alguma coisa! | |
import { curry, prop, propEq, filter } from 'ramda' | |
const add = curry((x, y) => x + y) | |
const add5 = add(5) | |
add5(10) // 15 | |
const transaction = { amount: 1000, status: 'paid' } | |
prop('status', transaction) // 'paid' | |
propEq('status', 'paid', transaction) // true | |
const transactions = [ | |
{ amount: 1000, status: 'paid' }, | |
{ amount: 2400, status: 'refunded' }, | |
{ amount: 3000, status: 'paid' }, | |
{ amount: 4000, status: 'paid' }, | |
{ amount: 5500, status: 'refunded' }, | |
] | |
// Filtrando transações com status paid | |
// sem ramda | |
const statusPaid = transaction => transaction.status === 'paid' | |
const uglyPaidOnly = transactions => transactions.filter(statusPaid) | |
uglyPaidOnly(transactions) | |
// usando o filter do ramda | |
const statusPaid = transaction => transaction.status === 'paid' | |
const paidOnly = filter(statusPaid) | |
paidOnly(transactions) // ou seja, executamos o filter assim: filter(statusPaid, transactions) | |
// propEq | |
const statusPaid = propEq('status', 'paid') | |
const paidOnly = filter(statusPaid) | |
paidOnly(transactions) | |
// -------------------------------------------------------- | |
// Ramda + promises = <3 | |
fetch('rota de transactions') | |
.then(transaction => transactions.filter(transaction => transaction.status === 'paid')) | |
const statusIs = propEq('status') | |
fetch('rota de transactions') | |
.then(transaction => transactions.filter(statusIs('paid'))) | |
fetch('rota de transactions') | |
.then(filter(statusIs('paid'))) | |
fetch('rota de transactions') | |
.then(filter(statusIs('refunded'))) | |
// -------------------------------------------------------- | |
// Componha funções a partir das que você ja tem | |
import { pipe, add } from 'ramda' | |
const double = x => x * 2 | |
const add10AndDouble = pipe(add(10), double) | |
add10AndDouble(5) // 30 | |
Promise.resolve(5) | |
.then(add(10) | |
.then(double) | |
.then(console.log) // 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment