ANA LUIZA BASTOSgithub.com/anabastos@naluhh@anapbastosSoftware Developer na Quanto ecientista da computação naPUC-SPanabastos.me
View Slide
JAVASCRIPT FUNCIONAL COMRAMDA
JS pode ser usadocomo umalinguagemfuncional?
Javascript tem funções deprimeira ordem (HOF)
Quando falamos de funcionalnessas linguagens falamos de“Mantenabilidade”“Menos bugs”“Declaratividade”
JS vem aos poucosadotando elementos delinguagens funcionais
PROPOSALS
FlatmapPartial ApplicationPipeline operatorPattern Matching
RAMDA
Biblioteca que foi pensadapara tornar mais fácil ojavascript funcional
● 100% imutável● ganho em performance● legibilidade● point-free / tacit programming
● Lists(map, filter, reduce, contains, replace,passAll, crop, flatten, find)● Maths(inc, add, mean, sum)● String(split, replace)● Logics(equals, cond, not)● Relation(intersection, clamp, gt, lt)● Functions(curry, pipe, compose, ifElse, etc)
TODAS AS FUNÇÕESSÃO CURRIED
const add = (x, y) => x + yadd(1, 2); // 3add(1) // Error
const add = x => y => x + yadd(1, 2); // 3add(1) // Function
R.add(1, 2); // 3R.add(1) // Function
CURRY(PROPOSAL PARTIALAPPLICATION)
const curryAdd = R.curry((x, y) => x + y))curryAdd(1, 2); // 3curryAdd(1)(2); // 3
PIPE / COMPOSE(PROPOSAL PIPELINE OPERATOR)
PIPE / COMPOSE:compõe funções de formasequencialfunction1 function2INPUT OUTPUT
EXEMPLINHO
Happy Hour:● Soma o consumido● Adiciona os 10%● Divide entre os colegas
const conta = [9, 9, 9, 9, 15]
const divideConta = R.pipe(R.sum, // 51R.mul(1.1), // 56,1R.divide(6) // 9,35)divideConta(conta)
PIPECOMPOSE
const ListOfItems = R.compose(Box,List,ListItems)([{...}, {...}, {...}])
PIPEP / COMPOSEP
then().then().then().then()
const asyncStuff = R.pipeP(getId,getUserById,asyncStuff)// Promise
IFELSE(maybe monad)
const findName = R.ifElse(R.has('name'),R.prop('name'),R.always('no name'),)({ id: 123, name: “ana”})// “ana”
FLATTER(PROPOSAL FLATMAP)
R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, 12]]]]);// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
DATA HANDLING
EVOLVE
{name: “Ana”email: “[email protected]”,phone: “11 98698-3010”,address: “rua da aparecida 304 apt34 ”,}
R.evolve({email: R.toLower,phone: R.replace(‘-’, ‘’),})(data)// {// name: “Ana”// email: “[email protected]”,// phone: “11 9869983010”// address: ...// }
APPLY SPEC
const createObj = R.applySpec({counter: R.inc,userData: { phone: R.trim},})createObj(0, “ 98499-1900”)// {1, userData{ phone: “9849919000”}}
TryCatchCondMemoizationLenses
BOM TOOLBOX
COOKBOOK
● Rambda - github.com/selfrefactor/rambda● Ramda-fantasy - github.com/ramda/ramda-fantasy● Thinking Ramda -randycoulman.com/blog/2016/05/24/thinking-in-ramda-getting-started● Ramda - Derek Stavis(Pagar.me talks)
OBRIGADA :)speakerdeck.com/anabastosanabastos.megithub.com/anabastos@naluhh@anapbastos