Swift Functional Programming - Second Edition
eBook - ePub

Swift Functional Programming - Second Edition

Dr. Fatih Nayebi

Compartir libro
  1. 316 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Swift Functional Programming - Second Edition

Dr. Fatih Nayebi

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Swift Functional Programming - Second Edition un PDF/ePUB en línea?
Sí, puedes acceder a Swift Functional Programming - Second Edition de Dr. Fatih Nayebi en formato PDF o ePUB, así como a otros libros populares de Computer Science y Programming Mobile Devices. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
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...

Índice