Scaling iOS Development with FRP

0ebf471a3ae8df42a84f93a7efbbdbd0?s=47 Ash Furrow
February 23, 2016

Scaling iOS Development with FRP

0ebf471a3ae8df42a84f93a7efbbdbd0?s=128

Ash Furrow

February 23, 2016
Tweet

Transcript

  1. 2.
  2. 3.
  3. 4.
  4. 5.

    BNE ADD1 BRA NONE1 ADD1 INY ;increment our counter of

    1's NONE1 LDAA TEMP ;reload accumulator A LSL MASK ;Shift the mask's 1 bit left BNE LOOP1 ;If we haven't finished our loop, branch LDAA #$01 ;load new mask into A STAA MASK ;store the reset mask into MASK TSX ;pull of return address and store in X PULA ;pull off A STAA TEMP ;store the value into temp TXS ;push return address back onto the stack LOOP2 LDAA TEMP ;Load A into TEMP ANDA MASK ;logical AND MASK with A BNE ADD2 ;add one if we need to BRA NONE2 ADD2 INY ;increment our counter of 1's NONE2 LDAA TEMP LSL MASK ;shift our mask left by one BNE LOOP2 ;loop back until we've exhausted positions
  5. 6.
  6. 7.

    Agenda 1. Simplicity is the goal of software development 2.

    Statefulness is an enemy of simplicity 3. FRP conceals state rather than eliminate it
  7. 8.
  8. 11.

    What is simple? • Focused • Uncombined • Has one

    role, one purpose, one concept, etc… • Simple is not necessarily easy
  9. 12.

    let numbers = [1, 5, 4, -1, 7] var sum

    = 0 for var i = 0; i < numbers.count; i++ { sum += numbers[i] } print(sum)
  10. 13.

    let numbers = [1, 5, 4, -1, 7] let sum

    = numbers.reduce(0, combine: +) print(sum)
  11. 19.

    –Harold Abelson, SICP “Programs must be written for people to

    read, and only incidentally for machines to execute.”
  12. 20.

    Example • Form validation • Submit button is disabled until

    data entry is valid • Also disabled while the form is being submitted
  13. 21.

    formDidChange(form: Form) { submitButton.enabled = validate(form) } submitForm(form: Form) {

    submitButton.enabled = false network.submitForm(form) { submitButton.enabled = true } }
  14. 22.

    formDidChange(form: Form) { submitButton.enabled = validate(form) } submitForm(form: Form) {

    submitButton.enabled = false network.submitForm(form) { submitButton.enabled = validate(form) } }
  15. 23.

    formDidChange(form: Form) { submitButton.enabled = validate(form) } submitForm(form: Form) {

    submitButton.enabled = false network.submitForm(form) { // Wait, do we reset the form first? Maybe. submitButton.enabled = validate(form) } }
  16. 24.

    let handler = FormHandler(callback: { enabled in self.submitButton.enabled = enabled

    } formDidChange(form: Form) { handler.update(form) } submitForm(form: Form) { handler.submit(form) }
  17. 27.
  18. 33.

    class FormHandler { let callback: Bool -> Void private let

    submitter = FormSubmitter() private let validator = FormValidator() private let resetter = FormResetter() init(callback: Bool -> Void) { self.callback = callback } }
  19. 42.
  20. 43.

    struct FormHandler { let callback: Bool -> Void private let

    submitter = FormSubmitter() private let validator = FormValidator() private let resetter = FormResetter() ...
  21. 51.
  22. 53.
  23. 58.

    –Edsger Dijkstra “The purpose of abstraction is not to be

    vague, but to create a new semantic level in which one can be absolutely precise.”
  24. 62.

    class LoginNetworkModel { private let _loginStatus = Variable(UserStatus.NotLoggedIn) var loginStatus:

    Observable<UserStatus> { return _loginStatus.asObservable() } ... }
  25. 63.

    class LoginNetworkModel { private let _loginStatus = Variable(UserStatus.NotLoggedIn) var loginStatus:

    Observable<UserStatus> { return _loginStatus.asObservable() } ... }
  26. 64.

    class LoginNetworkModel { private let _loginStatus = Variable(UserStatus.NotLoggedIn) var loginStatus:

    Observable<UserStatus> { return _loginStatus.asObservable() } ... }
  27. 66.

    loginNetworkModel .loginStatus .subscribeNext { [weak self] result in switch result

    { case .NotLoggedIn: self?.failedToLogin() case .LoggedIn: self?.welcomeUser() }
 }
  28. 67.

    loginNetworkModel .loginStatus .subscribeNext { [weak self] result in switch result

    { case .NotLoggedIn: self?.failedToLogin() case .LoggedIn: self?.welcomeUser() }
 }
  29. 68.

    loginNetworkModel .loginStatus .subscribeNext { [weak self] result in switch result

    { case .NotLoggedIn: self?.failedToLogin() case .LoggedIn: self?.welcomeUser() }
 }
  30. 73.

    class LoginNetworkModel { func login(username: String, password: String) -> Observable<UserStatus>

    { return network .authUsername(username, password: password) .map { response in ... } } }
  31. 74.

    class LoginNetworkModel { func login(username: String, password: String) -> Observable<UserStatus>

    { return network .authUsername(username, password: password) .map { $0.statusCode } .map { statusCode -> UserStatus in statusCode == 200 ? .LoggedIn : .NotLoggedIn } } }
  32. 77.

    loginNetworkModel .login("ashfurrow", password: ...) .subscribeNext { [weak self] result in

    switch result { case .NotLoggedIn: self?.failedToLogin() case .LoggedIn: self?.welcomeUser() }
 }
  33. 83.

    class LoginViewController: UIViewController { let disposeBag = DisposeBag() ... loginNetworkModel

    .login("ashfurrow", password: ...) .subscribeNext { ... } .addDisposableTo(self.disposeBag) ...
  34. 85.

    Requirements Change! • View controller needs access to full user

    info • Let’s modify our network model
  35. 86.

    class LoginNetworkModel { func login(username: String, password: String) -> Observable<User>

    { return network .authUsername(username, password: password) .filterSuccessfulStatusCodes() .mapToJSON() .mapToObject(User) } }
  36. 87.

    loginNetworkModel .login("ashfurrow", password: …) .onError { [weak self] error in

    self?.handleFailure(error) } .subscribeNext { ... } .addDisposableTo(self.disposeBag)
  37. 89.

    network .accessToken // Observable<AccessToken> .flatMap { token in switch token

    { case .Expired: return network.fetchToken() case .Valid: return just(token) } } .flatMap { token in return networking.performRequest(...) }
  38. 90.

    network .accessToken // Observable<AccessToken> .flatMap { token in switch token

    { case .Expired: return network.fetchToken() case .Valid: return just(token) } } .flatMap { token in return networking.performRequest(...) }
  39. 91.

    network .accessToken // Observable<AccessToken> .flatMap { token in switch token

    { case .Expired: return network.fetchToken() case .Valid: return just(token) } } .flatMap { token in return networking.performRequest(...) }
  40. 92.

    network .accessToken // Observable<AccessToken> .flatMap { token in switch token

    { case .Expired: return network.fetchToken() case .Valid: return just(token) } } .flatMap { token in return networking.performRequest(...) }
  41. 93.

    Benefits • Token not validated until needed • Inject behaviour

    into existing networking pipeline • Transparently • Example: github.com/Moya/Moya
  42. 98.

    extension FormHandler { var enabled: Observable<Bool> { return [submitter.submitting, validator.valid]

    .combineLatest { values in return ( submitting: values[0], valid: values[1] ) // Create a tuple } .map { (submitting, valid) in return submitting == false && valid == true } } }
  43. 99.

    extension FormHandler { var enabled: Observable<Bool> { return [submitter.submitting, validator.valid]

    .combineLatest { values in return ( submitting: values[0], valid: values[1] ) // Create a tuple } .map { (submitting, valid) in return submitting == false && valid == true } } }
  44. 100.

    extension FormHandler { var enabled: Observable<Bool> { return [submitter.submitting, validator.valid]

    .combineLatest { values in return ( submitting: values[0], valid: values[1] ) // Create a tuple } .map { (submitting, valid) in return submitting == false && valid == true } } }
  45. 101.

    extension FormHandler { var enabled: Observable<Bool> { return [submitter.submitting, validator.valid]

    .combineLatest { values in return ( submitting: values[0], valid: values[1] ) // Create a tuple } .map { (submitting, valid) in return submitting == false && valid == true } } }
  46. 106.

    Wrap-up 1. Strive for simplicity because brains don’t scale 2.

    State increases incidental complexity 3. Rather than eliminate state, FRP abstract it away
  47. 108.