2. TEXT
About me…
▸ Jordi Pradel
▸ Agile software developer
▸ Learning functional programming
▸ @agile_jordi
3. FP IS JUST PROGRAMMING WITH FUNCTIONS. FUNCTIONS ARE:
1. TOTAL: THEY RETURN AN OUTPUT FOR EVERY INPUT.
2. DETERMINISTIC: THEY RETURN THE SAME OUTPUT FOR THE SAME
INPUT.
3. PURE: THEIR ONLY EFFECT IS COMPUTING THE OUTPUT.
THE REST IS JUST COMPOSITION YOU CAN LEARN OVER TIME.
John De Goes
TEXT
11. IMPERATIVE
class DocumentService(
documentRepository: DocumentRepository,
transactionController: TransactionController) {
def saveDocumentIfNotFound(document: Document): Unit = {
transactionController.inTransaction {
conn =>
val documentExists =
documentRepository.getDocument(document.id)(conn).nonEmpty
if (documentExists) {
documentRepository.updateDocument(document)(conn)
} else {
documentRepository.insertDocument(document)(conn)
}
}
}
}
12. IMPERATIVE
class TransactionController(dataSource: DataSource) {
def inTransaction[T](f: Connection => T): T = {
val conn = dataSource.getConnection()
try {
conn.beginTransaction()
val res = f(conn)
conn.commit()
res
} catch {
case NonFatal(t) =>
//TODO: Proper error handling of rollback
conn.rollback()
throw t
}
}
}
20. FUNCTIONAL
▸ I was preparing a blog post or a talk about pure functional
error handling
▸ It was about typeclasses like ApplicativeError,
MonadError… (similar to how Future handles errors)
▸ I may still talk about them
▸ But suddenly….