0%

Book Description

Bring the power of Swift functional programming to iOS, Web, macOS, watchOS and tvOS application development and build clean, smart, scalable and reliable applications

About This Book

  • Written for Swift 3 -Developers Preview version, this is a comprehensive guide that introduces iOS and OS X developers to the all-new world of functional programming that has so far been alien to them
  • Learn about first-class functions and how imperative-style patterns can be converted into functional code using some simple techniques
  • The book will get you familiar with using functional programming alongside existing OOP techniques so you can get the best of both worlds and develop clean, robust code

Who This Book Is For

The book is for developers with a basic knowledge of Swift programming aiming to incorporate functional programming paradigms in their day-to-day application development

What You Will Learn

  • First-class, higher-order, and pure functions
  • Closures and capturing values
  • Custom operators, recursion, and memoization
  • Value and reference types in Swift
  • Enumerations, algebraic data types, patterns, and pattern matching
  • Generics and associated type protocols
  • Higher-order functions such as map, flatMap filter, and reduce
  • Dealing with optionals, fmap, and apply for multiple functional mapping
  • Functional data structures such as Semigroup, Monoid, Binary Search Tree, Linked List, Stack, and Lazy List
  • Immutability, copy constructors, and lenses
  • Combining FP paradigms with OOP, FRP, and POP in your day-to-day development activities
  • Developing a backend application with Swift
  • Developing an iOS application with FP, OOP, FRP, and POP paradigms

In Detail

This book is based on Swift 3 Developer preview version and aims at simplifying the functional programming (FP) paradigms making it easily usable, by showing you how to solve many of your day-to-day development problems.

Whether you are new to functional programming and Swift or experienced, this book will strengthen the skills you need to design and develop high-quality, scalable, and efficient applications.

The book starts with functional programming concepts, the basics of Swift 3, and essential concepts such as functions, closures, optionals, enumerations, immutability, and generics in detail with coding examples.

Furthermore, this book introduces more advanced topics such as function composition, monads, functors, applicative functors, memoization, lenses, algebraic data types, functional data structures, functional reactive programming (FRP), protocol-oriented programming (POP) and mixing object-oriented programming (OOP) with functional programming (FP) paradigms.

Finally, this book provides a working code example of a front-end application developed with these techniques and its corresponding back-end application developed with Swift.

Style and approach

This is an easy-to-follow guide full of hands-on coding examples of real-world applications. Each topic is explained sequentially and placed in context, and for the more inquisitive, there are more details of the concepts used. It introduces the Swift language basics and functional programming techniques in simple, non-mathematical vocabulary with examples in Swift.

Table of Contents

  1. Swift 3 Functional Programming
    1. Swift 3 Functional Programming
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
      2. Free access for Packt account holders
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    7. 1. Getting Started with Functional Programming in Swift
      1. Why functional programming matters?
      2. What is functional programming?
      3. The Swift programming language
        1. Swift features
          1. Modern syntax
          2. Type safety and type inference
          3. Immutability
          4. Stateless programming
          5. First-class functions
          6. Higher-order functions
          7. Pattern matching
          8. Generics
          9. Closures
          10. Subscripts
          11. Optional chaining
          12. Extensions
          13. Objective-C and Swift bridging headers
          14. Automatic Reference Counting
          15. REPL and Playground
        2. Language basics
          1. Type safety and type inference
          2. Type annotation
          3. Type aliases
          4. Immutability
          5. Tuples
          6. Optionals
          7. Basic operators
          8. Strings and characters
            1. Immutability
            2. String literals
            3. Empty Strings
            4. Concatenating strings and characters
            5. String interpolation
            6. String comparison
          9. Collections
          10. Control flows
            1. for loops
            2. while loops
            3. stride
            4. if
            5. switch
            6. guard
          11. Functions
          12. Closures
          13. Map, filter, and reduce
            1. Map
            2. Filter
            3. Reduce
          14. Enumerations
          15. Generics
          16. Classes and structures
            1. Classes versus structures
            2. Choosing between classes and structures
            3. Identity operators
          17. Properties
            1. Property observers
          18. Methods
          19. Subscripts
          20. Inheritance
          21. Initialization
          22. Deinitialization
          23. Automatic Reference Counting
          24. Optionals and optional chaining
          25. Error handling
          26. Type casting
          27. Any and AnyObject
          28. Nested types
          29. Extensions
          30. Protocols
            1. Protocols as types
            2. Protocol extensions
          31. Access control
      4. Summary
    8. 2. Functions and Closures
      1. What is a function?
      2. The general syntax of functions and methods
        1. Best practices in function definition
        2. Calling functions
      3. Defining and using function parameters
      4. Defining and using variadic functions
      5. Returning values from functions
      6. Pure functions
      7. Function types
      8. Defining and using nested functions
      9. First-class functions
      10. Higher-order functions
      11. Function composition
        1. Custom operators
          1. Allowed operators
          2. Custom operator definition
        2. A composed function with a custom operator
      12. Closures
        1. Closure syntax
        2. Capturing values
      13. Function currying
      14. Recursion
        1. Tail recursion
      15. Memoization
      16. Summary
    9. 3. Types and Type casting
      1. Value versus reference types
        1. Value and reference type constants
        2. Mixing value and reference types
        3. Copying
        4. Copying reference types
        5. Value type characteristics
          1. Behavior
          2. Isolation
          3. Interchangeability
          4. Testability
          5. Threats
        6. Using value and reference types
      2. Equality versus identity
      3. Equatable and Comparable
      4. Type checking and casting
      5. Summary
    10. 4. Enumerations and Pattern Matching
      1. Defining enumerations
      2. Associated values
      3. Raw values
      4. Algebraic data types
        1. Simple types
        2. Composite types
        3. Composite type with variants
        4. The algebra of data types
      5. Pattern matching
      6. Patterns and pattern matching
        1. The wildcard pattern
        2. The value-binding pattern
        3. The identifier pattern
        4. The tuple pattern
        5. The enumeration case pattern
        6. The optional pattern
        7. Type casting patterns
        8. The expression pattern
      7. Summary
    11. 5. Generics and Associated Type Protocols
      1. What are generics and what kind of problems do they solve?
      2. Type constraints
        1. Where clauses
      3. Generic data structures
      4. Associated type protocols
      5. Extending generic types
      6. Subclassing generic classes
      7. Summary
    12. 6. Map, Filter, and Reduce
      1. Functor
      2. Applicative Functor
      3. Monad
      4. Map
      5. FlatMap and flatten
      6. Filter
      7. Reduce
        1. The map function in terms of reduce
        2. The filter function in terms of reduce
        3. The flatMap function in terms of reduce
        4. The flatten function in terms of reduce
      8. Apply
      9. Join
      10. Chaining higher-order functions
      11. Zip
      12. Practical examples
        1. Sum of an array
        2. Product of an array
        3. Removing nil values from an array
        4. Removing duplicates in an array
        5. Partitioning an array
      13. Summary
    13. 7. Dealing with Optionals
      1. Optional types
      2. Unwrapping optionals
        1. Force unwrapping
        2. nil checking
        3. Optional binding
      3. Guard
      4. Implicitly unwrapped optionals
      5. Error handling to avoid optionals
        1. try!
        2. try?
      6. Nil-coalescing
      7. Optional chaining
      8. Dealing with optionals functionally
        1. Optional mapping
        2. Multiple optional value mapping
      9. Summary
    14. 8. Functional Data Structures
      1. Semigroup
      2. Monoid
      3. Trees
        1. Contains
        2. Binary Search Tree
          1. Contains
          2. Size
          3. Elements
          4. Empty
      4. Lists
        1. Empty LinkedList
        2. Cons
        3. Contains
        4. Size
        5. Elements
        6. isEmpty
        7. map, filter, and reduce
      5. Stacks
      6. Lazy list
      7. Summary
    15. 9. Importance of Immutability
      1. Immutability
      2. Immutable variables
        1. Weak versus strong immutability
      3. Reference types versus value types
      4. The benefits of immutability
        1. Thread safety
        2. Referential transparency
        3. Low coupling
        4. Avoiding temporal coupling
        5. Avoiding identity mutability
        6. Failure atomicity
        7. Parallelization
        8. Exception handling and error management
        9. Caching
        10. State comparison
        11. Compiler optimization
      5. Cases for mutability
      6. An example
        1. Side-effects and unintended consequences
        2. Testability
      7. Copy constructors and lenses
        1. Copy constructor
        2. Lens
          1. Lens composition
      8. Summary
    16. 10. The Best of Both Worlds – Combining FP Paradigms with OOP
      1. OOP paradigms
        1. Objects
        2. Classes
        3. Inheritance
          1. Overriding
          2. Design constraints
            1. Singleness
            2. Static
            3. Visibility
            4. Composite reuse
          3. Issues and alternatives
          4. When to inherit
        4. Polymorphism
        5. Dynamic binding
      2. OOP design principles
        1. SRP
          1. The FP counterpart
        2. OCP
          1. The FP counterpart
        3. LSP
          1. The FP counterpart
        4. ISP
          1. The FP counterpart
        5. DIP
          1. The FP counterpart
        6. DDD
          1. Concepts
          2. Premise
          3. Building blocks
            1. Aggregate
            2. Immutable value objects
            3. Domain events
            4. Intention-revealing interface
            5. Side-effect-free functions
            6. Assertions
            7. Conceptual contours
            8. Closure of operations
          4. Declarative design
      3. Protocol-oriented programming (POP)
        1. POP paradigms
          1. Protocol composition
          2. Protocol extensions
          3. Protocol inheritance
          4. Associated types
          5. Conforming to a protocol
      4. Functional Reactive Programming (FRP)
        1. Building blocks of FRP
          1. Signals
          2. Pipes
          3. Signal producers
          4. Buffers
          5. Observers
          6. Actions
          7. Properties
          8. Disposables
          9. Schedulers
        2. An example
      5. Mixing OOP and FP
        1. Problems
          1. Granularity mismatch
        2. FP paradigm availability
          1. First-class values
          2. Closures
          3. FP-OOP interrelation tools
        3. FP support
        4. Effects of having FP capabilities in OOP
          1. Idiomatic effects
          2. Architectural effects
        5. OOP design patterns - an FP perspective
          1. Strategy pattern
          2. Command pattern
          3. Observer pattern
          4. Virtual proxy pattern
          5. Visitor pattern
      6. Summary
    17. 11. Case Study – Developing an iOS Application with the FP and OOP Paradigms
      1. Requirements
      2. High-level design
        1. Frontend
          1. Models
          2. Views
          3. ViewController
          4. State
          5. Store
          6. Actions
          7. Manager
          8. Communication
          9. Communication between layers
          10. Third-party libraries
          11. Cross-cutting concerns
            1. Error management and exception handling
            2. Crash reporting
            3. Analytics
          12. Tools
        2. Backend
          1. Vapor
            1. Routing
            2. JSON
            3. Requesting data
          2. SPM
      3. Backend development
        1. Model
        2. Store
        3. Controller
          1. Posting a new Todo item
          2. Getting a list of Todo items
          3. Getting a specific Todo item
          4. Deleting an item and deleting all Todo items
          5. Updating a Todo item
      4. iOS application development
        1. Configuration
        2. Models
          1. Operators
            1. <^>
            2. <*>
            3. <|
            4. <|?
            5. <||
          2. Using Argo models
          3. ViewModel
        3. Communication
          1. Request protocol
          2. Conforming to a request protocol
          3. WebServiceManager
          4. Creating a Todo item
          5. Listing Todo items
        4. Lens
        5. State
        6. Store
        7. Action
        8. Views
        9. ViewController
          1. MasterViewController
            1. IBActions
            2. TableView Delegates and DataSource
          2. DetailsViewController
      5. Summary