Swift Functional Programming - Second Edition
eBook - ePub

Swift Functional Programming - Second Edition

Dr. Fatih Nayebi

Buch teilen
  1. 316 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Swift Functional Programming - Second Edition

Dr. Fatih Nayebi

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Swift Functional Programming - Second Edition als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Swift Functional Programming - Second Edition von Dr. Fatih Nayebi im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Computer Science & Programming Mobile Devices. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
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...

Inhaltsverzeichnis