Swift Functional Programming - Second Edition
eBook - ePub

Swift Functional Programming - Second Edition

Dr. Fatih Nayebi

Share book
  1. 316 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Swift Functional Programming - Second Edition

Dr. Fatih Nayebi

Book details
Book preview
Table of contents
Citations

About This Book

Bring the power of functional programming to Swift to develop clean, smart, scalable and reliable applications.About This Book• Written for the latest version of Swift, this is a comprehensive guide that introduces iOS, Web and macOS developers to the all-new world of functional programming that has so far been alien to them• Get familiar with using functional programming alongside existing OOP techniques so you can get the best of both worlds and develop clean, robust, and scalable code• Develop a case study on example backend API with Swift and Vapor Framework and an iOS application with Functional Programming, Protocol-Oriented Programming, Functional Reactive Programming, and Object-Oriented Programming techniquesWho This Book Is ForMeant for a reader who knows object-oriented programming, has some experience with Objective-C/Swift programming languages and wants to further enhance his skills with functional programming techniques with Swift 3.x.What You Will Learn• Understand what functional programming is and why it matters• Understand custom operators, function composition, currying, recursion, and memoization• Explore algebraic data types, pattern matching, generics, associated type protocols, and type erasure• Get acquainted with higher-kinded types and higher-order functions using practical examples• Get familiar with functional and non-functional ways to deal with optionals• Make use of functional data structures such as semigroup, monoid, binary search tree, linked list, stack, and lazy list• Understand the importance of immutability, copy constructors, and lenses• Develop a backend API with Vapor• Create an iOS app by combining FP, OOP, FRP, and POP paradigmsIn DetailSwift is a multi-paradigm programming language enabling you to tackle different problems in various ways. Understanding each paradigm and knowing when and how to utilize and combine them can lead to a better code base. Functional programming (FP) is an important paradigm that empowers us with declarative development and makes applications more suitable for testing, as well as performant and elegant. This book aims to simplify the FP paradigms, making them easily understandable and usable, by showing you how to solve many of your day-to-day development problems using Swift FP.It starts with the basics of FP, and you will go through all the core concepts of Swift and the building blocks of FP. You will also go through important aspects, such as function composition and currying, custom operator definition, monads, functors, applicative functors, memoization, lenses, algebraic data types, type erasure, functional data structures, functional reactive programming (FRP), and protocol-oriented programming(POP). You will then learn to combine those techniques to develop a fully functional iOS application from scratchStyle and approachAn easy-to-follow guide that is 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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Swift Functional Programming - Second Edition an online PDF/ePUB?
Yes, you can access Swift Functional Programming - Second Edition by Dr. Fatih Nayebi in PDF and/or ePUB format, as well as other popular books in Informatik & Programmierung von Mobilgeräten. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781787283459

Case Study - Developing an iOS Application with FP and OOP Paradigms

In the previous chapters, we covered a variety of concepts and techniques. We started with FP paradigms and explored related topics in detail. Also, in the previous chapter, we covered other paradigms such as OOP, FRP, and POP, and mixed them together. In this chapter, we will create a simple application using those paradigms.
Most iOS applications need a backend to be able to provide advanced functionalities such as integration with other systems. In this chapter, we will create a simple backend with Swift that is going to be used as a Todo application API. Then, we will develop an iOS application that will leverage our backend and provide some essential functionality such as listing and updating Todo items coming from the backend. Also, our iOS application will be able to create new Todo items. Our iOS application development will include the FP, OOP, POP, and FRP paradigms.
This chapter will cover the following topics:
  • Requirements
  • High-level design
  • Backend development
    • Environment configuration
    • Swift Package Manager
    • Vapor
    • Application development
  • Frontend development
    • CocoaPods dependency management configuration
    • Third-party libraries
    • Backend communication
    • JSON parsing and model mapping
    • State management
    • Listing items with a UITableView
    • Updating and creating items
    • Filtering items

Requirements

This section presents the requirements for our case study. Since the focus of this book is not on requirement engineering, we will define very simple requirements. This section does not present best practices for requirement engineering.
The requirements for the iOS application user are as follows:
  • Users should be able to list Todo items
  • Users should be able to see the details of each item
  • Users should be able to modify items
  • Users should be able to create a new item
  • Users should be able to filter items by their status

High-level design

This section explains the high-level design of the frontend and backend.

Frontend

Application design follows a slightly different version of the Model-View-Controller (MVC) pattern, with the addition of the Actions, Store, State, and Communication layers to simplify the controller layer of the traditional iOS application MVC pattern. All application layers are explained in the following sections.

Models

Plain old model structures. These models do not have any logic and only consist of properties. There are four types of model:
  • TodoRequest: This is a struct that is used in backend request calls and conforms to RequestProtocol
  • Todo: This is a struct that represents Todo data, and uses the Argo and Curry libraries to decode the object from JSON
  • TodoViewModel and TodosViewModel: These structs represent data and are used in views and shown to the user
  • TodoLens: These lenses modify the Todo model
All the aforementioned models are immutable value types.

Views

We have two View subclasses: one to provide a custom UITableViewCell called TodoTableViewCell, and a subclass of UIView named FooterView.
Both of these Views are subclasses of iOS SDK-provided classes. Besides these classes, we will have our UIViewController scenes in the storyboard.

ViewController

ViewController is a subclass of UIViewController or UITableViewController, and it connects views to logic:
  • MasterViewController: This is a subclass of UITableViewController to present Todo items
  • DetailsViewController: This is a subclass of UIViewController to present details of each Todo item to the user
To develop iOS applications, we have to rely on iOS SDK-provided classes such as UIViewController and UITableViewController. The ViewController and UIView subclasses are the only classes that will be used in this case study.

State

In iOS application development, we need to handle states. We use the Delta, ReactiveCocoa, and ReactiveSwift libraries to manage our Todo app's State.
Delta takes an App that has custom State management spread throughout all the ViewControllers and simplifies it by providing a simple interface to change State and subscribe to its changes.
ReactiveSwift offers composable, declarative, and flexible primitives that are built around the concept of streams of values over time. These primitives can be used to uniformly represent observation patterns. ReactiveCocoa wraps various aspects of Cocoa frameworks with the declarative ReactiveSwift primitives.
We will implement a State struct to observable properties.

Store

Our Store struct will wrap State struct and provide properties to observe its changes. Store struct conforms to the Delta library's StoreType protocol, which defines the storage of an observable state and dispatch methods to modify it. Also, Store struct uses the ReactiveSwift library's MutableProperty value and allows observation of its changes in a thread-safe manner.

Actions

Actions are structs that conform to the ActionType protocol from the Delta library. ActionType is used when we want to make modifications to the Store struct's State. All changes to the Store struct go through this type.
We will develop the following Actions in the application:
  • ClearCompletedTodosAction...

Table of contents