SlideShare uma empresa Scribd logo
1 de 47
Baixar para ler offline
Contratos fortes
com programação
funcional
Henrique Morbin
iOS @ iFood
Rafael Machado
iOS @ iFood
Programação
Funcional
Pureza
Referencial
transparency
Imutabilidade
Recursão
?
“
É simples, mas não é fácil
O que é?
“Apoia-se em funções, modeladas como funções
matemáticas
Em sua essência, programas são uma combinação de
expressões
Fluxo de cadastro
BackendInput de nome Input de email Input de senha
class Registration {
var name: String?
var email: String?
var password: String?
}
typealias Name = String
typealias Email = String
typealias Password = String
class Registration {
var name: Name?
var email: Email?
var password: Password?
}
class FistStepViewController {
func continueButtonTouched(_ sender: UIButton) {
let registration = Registration()
registration.name = textField.text
router.navigateToSecondStep(registration)
}
}
BackendInput de nome Input de email Input de senha
class SecondStepViewController {
func continueButtonTouched(_ sender: UIButton) {
registration.email = textField.text
router.navigateToThirdStep(registration)
}
}
BackendInput de nome Input de email Input de senha
class ThirdStepViewController {
func registerButtonTouched(_ sender: UIButton) {
registration.password = textField.text
network.postRegistration(registration)
}
}
BackendInput de nome Input de email Input de senha
class Network {
func postRegistration(_ registration: Registration) {
guard let name = registration.name,
let email = registration.email,
let password = registration.password else {
NSLog("Something is wrong, registration *must* not be empty")
return
}
Alamofire.request(
url: registrationUrl,
method: .post,
parameters: [
"name": name,
"email": email,
"password": password
]
)
}
}
BackendInput de nome Input de email Input de senha
Contratos fortes
sem programação
funcional
struct FirstStepModel {
let name: Name
}
struct SecondStepModel {
let name: Name
let email: Email
init(firstStep: FirstStepModel, email: Email) {
self.name = firstStep.name
self.email = email
}
}
struct ThirdStepModel {
let name: Name
let email: Email
let password: Password
init(secondStep: SecondStepModel, password: Password) {
self.name = secondStep.name
self.email = secondStep.email
self.password = password
}
}
struct ThirdStepModel {
let name: Name
let email: Email
let password: Password
init(secondStep: SecondStepModel, password: Password) {
self.name = secondStep.name
self.email = secondStep.email
self.password = password
}
}
Checkpoint
Curry
“ Converte uma função que recebe vários
argumentos para uma sequência de funções, cada
uma recebendo com um único argumento
Registration.init
(Name, Email, Password) -> Registration
(Name) -> (Email) -> (Password) -> Registration
(A) -> (B) -> (C) -> D
func curry<A, B, C, D>
func curry<A, B, C, D>(_ f: @escaping (A, B, C) -> D)
func curry<A, B, C, D>(_ f: @escaping (A, B, C) -> D)
-> (A) -> (B) -> (C) -> D {
}
func curry<A, B, C, D>(_ f: @escaping (A, B, C) -> D)
-> (A) -> (B) -> (C) -> D {
return { a in
}
}
func curry<A, B, C, D>(_ f: @escaping (A, B, C) -> D)
-> (A) -> (B) -> (C) -> D {
return { a in
{ b in
}
}
}
func curry<A, B, C, D>(_ f: @escaping (A, B, C) -> D)
-> (A) -> (B) -> (C) -> D {
return { a in
{ b in
{ c in
return f(a, b, c)
}
}
}
}
struct Registration {
let name: Name
let email: Email
let password: Password
}
typealias InitialStep = (Name) -> (Email) -> (Password) -> Registration
typealias FirstStep = (Email) -> (Password) -> Registration
typealias SecondStep = (Password) -> Registration
class FistStepViewController {
func continueButtonTouched(_ sender: UIButton) {
let initialData: InitialStep = curry(Registration.init)
let partialData = initialData(textField.text!)
router.navigateToSecondStep(partialData)
}
}
BackendInput de nome Input de email Input de senha
class SecondStepViewController {
func continueButtonTouched(_ sender: UIButton) {
let partialData = self.partialData(textField.text!)
router.navigateToThirdStep(partialData)
}
}
BackendInput de nome Input de email Input de senha
class ThirdStepViewController {
func registerButtonTouched(_ sender: UIButton) {
let registration = self.partialData(textField.text!)
network.postRegistration(registration)
}
}
BackendInput de nome Input de email Input de senha
BackendInput de nome Input de email Input de senha
class Network {
func postRegistration(_ registration: Registration) {
Alamofire.request(
url: registrationUrl,
method: .post,
parameters: [
"name": registration.name,
"email": registration.email,
"password": registration.password
]
)
}
}
Revisão
?
Obrigado

Mais conteúdo relacionado

Mais de Henrique Morbin

Você já testou os seus testes?
Você já testou os seus testes?Você já testou os seus testes?
Você já testou os seus testes?Henrique Morbin
 
Understanding stack views
Understanding stack viewsUnderstanding stack views
Understanding stack viewsHenrique Morbin
 
Understanding Auto Layout
Understanding Auto LayoutUnderstanding Auto Layout
Understanding Auto LayoutHenrique Morbin
 
7 perguntas para mudar a forma como você faz coaching
7 perguntas para mudar a forma como você faz coaching7 perguntas para mudar a forma como você faz coaching
7 perguntas para mudar a forma como você faz coachingHenrique Morbin
 
Tailor - Linter for Swift
Tailor - Linter for SwiftTailor - Linter for Swift
Tailor - Linter for SwiftHenrique Morbin
 
Hackathon Queen Mob - Tools and Helpers
Hackathon Queen Mob - Tools and HelpersHackathon Queen Mob - Tools and Helpers
Hackathon Queen Mob - Tools and HelpersHenrique Morbin
 
Parse - Backend As A Service
Parse - Backend As A ServiceParse - Backend As A Service
Parse - Backend As A ServiceHenrique Morbin
 
Introdução ao Swift por Henrique Morbin – iOS Developer
Introdução ao Swift por Henrique Morbin – iOS DeveloperIntrodução ao Swift por Henrique Morbin – iOS Developer
Introdução ao Swift por Henrique Morbin – iOS DeveloperHenrique Morbin
 

Mais de Henrique Morbin (11)

Você já testou os seus testes?
Você já testou os seus testes?Você já testou os seus testes?
Você já testou os seus testes?
 
Understanding stack views
Understanding stack viewsUnderstanding stack views
Understanding stack views
 
Understanding Auto Layout
Understanding Auto LayoutUnderstanding Auto Layout
Understanding Auto Layout
 
7 perguntas para mudar a forma como você faz coaching
7 perguntas para mudar a forma como você faz coaching7 perguntas para mudar a forma como você faz coaching
7 perguntas para mudar a forma como você faz coaching
 
Bond, swift bond
Bond, swift bondBond, swift bond
Bond, swift bond
 
Tailor - Linter for Swift
Tailor - Linter for SwiftTailor - Linter for Swift
Tailor - Linter for Swift
 
Fastlane
FastlaneFastlane
Fastlane
 
Hackathon Queen Mob - Tools and Helpers
Hackathon Queen Mob - Tools and HelpersHackathon Queen Mob - Tools and Helpers
Hackathon Queen Mob - Tools and Helpers
 
Parse - Backend As A Service
Parse - Backend As A ServiceParse - Backend As A Service
Parse - Backend As A Service
 
Cocoa Touch Framework 8
Cocoa Touch Framework 8Cocoa Touch Framework 8
Cocoa Touch Framework 8
 
Introdução ao Swift por Henrique Morbin – iOS Developer
Introdução ao Swift por Henrique Morbin – iOS DeveloperIntrodução ao Swift por Henrique Morbin – iOS Developer
Introdução ao Swift por Henrique Morbin – iOS Developer
 

Contratos Fortes com Programação Funcional