Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Increasing code
reusability through
partial application
Sobre
Derek Stavis
Software Engineer @ Pagar.me
github.com/derekstavis
Agenda
● Motivation
● Partial application
● ES6 implementation
● Use cases
Motivation
If you love to write generic code
function create (db, coll, doc) {
return db[coll].insert(doc)
}
Motivation
If you want to run away from points
const db = client.connect(...)
const createUser = (???) =>
create(db, ‘user...
Motivation
If you want to run away from points
const db = client.connect(...)
const createUser = (doc) =>
create(db, ‘user...
Motivation
If you want to run away from points
const db = client.connect(...)
const createUser = (user) =>
create(db, ‘use...
Motivation
You will probably love partial application
const db = client.connect(...)
const createUser =
partial(create, db...
Partial application
Fill the arguments of a function
create(db, coll, doc)
Partial application
Fill the arguments of a function
createInDB = partial(db, create)
createInDB(coll, doc)
The implementation
A rudimentar version written in ES6
(f, ...left) =>
(...right) =>
f(...left, ...right)
Use cases
Dependency injection
HOF and FCF makes DI just simple
healthCheck()
.then(partial(req.status, 200))
.catch(partial(req.sta...
Lazy evaluation
Create thunks for lazy functions
const lazyHandle =
partial(handle, req, res, next)
You said... Promises?
Use partial application with chaining
return fetchUser(id)
.then(partial(map, getName))
.then(consol...
Real use case
Pagar.me Library
https://github.com/pagarme/pagarme-js
Thanks
Upcoming SlideShare
Loading in …5
×

TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Increasing code reusability through partial application

47 views

Published on

Trilha Programação Funcional How we figured out we had a SRE team at - Increasing code reusability through partial application

Published in: Education
  • Be the first to comment

TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had a SRE team at - Increasing code reusability through partial application

  1. 1. Increasing code reusability through partial application
  2. 2. Sobre Derek Stavis Software Engineer @ Pagar.me github.com/derekstavis
  3. 3. Agenda ● Motivation ● Partial application ● ES6 implementation ● Use cases
  4. 4. Motivation If you love to write generic code function create (db, coll, doc) { return db[coll].insert(doc) }
  5. 5. Motivation If you want to run away from points const db = client.connect(...) const createUser = (???) => create(db, ‘user’, ???)
  6. 6. Motivation If you want to run away from points const db = client.connect(...) const createUser = (doc) => create(db, ‘user’, doc)
  7. 7. Motivation If you want to run away from points const db = client.connect(...) const createUser = (user) => create(db, ‘user’, user)
  8. 8. Motivation You will probably love partial application const db = client.connect(...) const createUser = partial(create, db, ‘user’)
  9. 9. Partial application Fill the arguments of a function create(db, coll, doc)
  10. 10. Partial application Fill the arguments of a function createInDB = partial(db, create) createInDB(coll, doc)
  11. 11. The implementation A rudimentar version written in ES6 (f, ...left) => (...right) => f(...left, ...right)
  12. 12. Use cases
  13. 13. Dependency injection HOF and FCF makes DI just simple healthCheck() .then(partial(req.status, 200)) .catch(partial(req.status, 500))
  14. 14. Lazy evaluation Create thunks for lazy functions const lazyHandle = partial(handle, req, res, next)
  15. 15. You said... Promises? Use partial application with chaining return fetchUser(id) .then(partial(map, getName)) .then(console.log)
  16. 16. Real use case Pagar.me Library https://github.com/pagarme/pagarme-js
  17. 17. Thanks

×