Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Poor man's functional programming

Poor man's functional programming

Getting started with new paradigms requires us to change thinking and make mistakes along the way. Functional programming from the eyes of the beginner, along with practical steps to gradually change how we write our code.

Miguel Angel Quinones Garcia

September 18, 2015
Tweet

More Decks by Miguel Angel Quinones Garcia

Other Decks in Programming

Transcript

  1. !

  2. !

  3. !

  4. !

  5. TOOLS NOT RELIGIONS UI AND IOS SWIFT IS NOT PURE

    FP LANGUAGE SIDE EFFECTS ARE NECESSARY
  6. FUNCTIONS EVERYWHERE! struct Person { let name: String let surname:

    String } extension Person { var displayName { return "\(name) \(surname)" } }
  7. FUNCTIONS EVERYWHERE! extension Person { func greeting() -> String {

    return "Hello my name is \(name)" } } let me = Person(name: "Miguel", surname: "Quinones") let john = Person(name: "John", surname: "Doe") let greeter = Person.greeting let a = greeter(me)() // Hello my name is Miguel let b = greeter(john)() // Hello my name is John
  8. KNOW THE BASIC FUNCTIONS extension CollectionType { func map<T>(@noescape transform:

    (Self.Generator.Element) throws -> T) rethrows -> [T] func filter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element] func reduce<T>(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows -> T }
  9. KNOW THE BASIC FUNCTIONS extension CollectionType { func map<T>(transform: (Element)

    -> T) -> [T] func filter(includeElement: (Element) -> Bool) -> [Element] func reduce<T>(initial: T, combine: (T, Element) -> T) -> T }
  10. KNOW THE BASIC FUNCTIONS extension SequenceType { func flatMap<S :

    SequenceType>(transform: (Self.Generator.Element) throws -> S) rethrows -> [S.Generator.Element] }
  11. KNOW THE BASIC FUNCTIONS extension SequenceType { func flatMap<S :

    SequenceType>(transform: (Self.Element) -> S) -> [S.Element] }
  12. (SMALL) EXAMPLES OF PROFIT MAPPING struct MyDataModel { // Values

    } struct MyViewModel { private let model: MyDataModel // Presentation stuff } let models: [MyDataModel] //Read from somewhere let viewModels = models.map(MyViewModel.init)
  13. (SMALL) EXAMPLES OF PROFIT struct User { let name: String

    let nickname: String? } struct UserViewModel { var displayName: String { func formatNickName(nickName: String) -> String { return "Known as \(nickName)" } return self.user.nickname.map(formatNickName) ?? "" } private let user: User }
  14. (SMALL) EXAMPLES OF PROFIT REDUCE INSTEAD OF LOOPS let anySubViewEmpty:

    Bool = self.subviews.reduce(false) { empty, view return empty || CGRectEqualToRect(view.frame, CGRectZero) }
  15. (SMALL) EXAMPLES OF PROFIT FLATMAP WITH OPTIONALS struct MyDataModel {

    // Values } extension MyDataModel { init?(json: JSON) { // Parsing } } let jsonItems: [JSON] // Fetched from a remote server let dataModels = jsonItems.flatMap(MyDataModel.init)
  16. ABUSING IT MAP INSTEAD OF LOOPS let views: [UIView] //

    No readability gain views.map { self.addSubview($0) } // Conveys purpose for view in views { self.addSubview(view) }
  17. !

  18. There is no such thing as a best solution, be

    it a tool, a language, or an operating system. — The pragmatic programmer