Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Swift Functional Programming
  • Table Of Contents Toc
  • Feedback & Rating feedback
Swift Functional Programming

Swift Functional Programming

By : Nayebi
4.3 (3)
close
close
Swift Functional Programming

Swift Functional Programming

4.3 (3)
By: Nayebi

Overview of this book

Swift 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 scratch
Table of Contents (19 chapters)
close
close
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Dedication
Preface
chevron up

Preface

Functional programming (FP) is getting a lot of attention as it eases many of the difficulties faced in object-oriented programming (OOP), such as testability, maintainability, scalability, and concurrency. Swift has a lot of functional programming features that can be easily used, but most Objective-C and Swift programmers are not very familiar with these tools.

This book aims to simplify the functional programming paradigms and make it easily usable for Swift programmers, showing you how to use popular functional programming techniques to solve many of your day-to-day development problems. Whether you are new to functional programming and Swift or experienced, this book will provide you with the skills you need to design and develop high quality, easily maintainable, scalable, and efficient applications for iOS Web, macOS, tvOS, and watchOS. Through this book, you'll learn to develop extendable, smart, and maintainable code using functional programming techniques.

What this book covers

Chapter  1, Getting Started with Functional Programming in Swift, introduces functional programming paradigms by attempting to answer the questions of Why functional programming matters? and What is functional programming? It covers topics such as immutability, stateless programming, pure, first-class, and higher-order functions. Also, this chapter will introduce the Swift programming language basics as they are essential for the rest of the book.

Chapter  2, Functions and Closures, begins with the definition of functions, continues with other related topics, such as function types, and finally concludes with more advanced topics such as first-class functions, higher-order functions, function composition, custom operator definition, closures, function currying, recursion, and memoization.

Chapter  3, Types and Type Casting, takes a look at types in general by introducing different kinds of types such as concrete, abstract, product, and sum. We will cover topics such as value and reference type constants, mixing value and reference types, and copying. Then, we will discuss the characteristics of value types. We will also cover the key differences between value and reference types, and how we should decide which one to use. Finally, we will explore equality, identity, type checking, and casting topics.

Chapter  4, Enumerations and Pattern Matching, explains the enumeration definition and usage. We will cover associated and raw values and being introduced to the concept of algebraic data types. We will explore some examples to cover the sum, product, and recursion types. Also, in this chapter, we will explore patterns such as wildcard, value-binding, identifier, tuple, enumeration case, optional, type casting, and expression, along with related pattern matching examples.

Chapter  5, Generics and Associated Type Protocols, teaches us how to define and use generics. We will also understand the type of problems generics solve. Moving forward, we will explore type constraints, generic data structures, and associated type protocols with examples. We will explore type erasure by an example and finally we will learn how to extend generic types and how to subclass generic classes.

Chapter  6, Map, Filter, and Reduce, introduces the concept of higher-kinded types, Functor, Applicative Functor, and Monad. This chapter covers higher-order functions/methods such as map, flatMap, filter, and reduce in the Swift programming language with examples. The chapter continues by providing implementation of map, filter, flatMap, and flatten in terms of reduce. Then it provides, apply, join, chaining higher-order functions, and zip. Finally, it provides practical examples of higher-order function usage.

Chapter  7, Dealing with Optionals, familiarizes us with different techniques to deal with optionals. We will talk about built-in techniques to deal with optionals, such as optional binding, guard, coalescing, and optional chaining. Then, we will explore functional programming techniques to deal with optionals. Finally, this chapter will cover the error handling with an example.

Chapter  8, Functional Data Structures, introduces the concept of functional data structures and explores examples of data structures implemented in a functional way, such as Semigroup, Monoid, BST, LinkedList, Stack, and LazyList.

Chapter  9, Importance of Immutability, explores the concept of immutability. We will look at its importance and benefits with the help of examples. Then we will consider cases for mutability and go through an example to compare mutability and immutability effects on our code. Finally, we will explore copy constructors and lenses.

Chapter  10, The Best of Both Worlds - Combining FP Paradigms with OOP, covers object- oriented programming principles and paradigms. Then, we will be introduced to protocol- oriented programming. Next, we will have an introduction of functional reactive programming and explore how to mix FP with OOP paradigms.

Chapter  11, Case Study - Developing an iOS Application with the FP and OOP Paradigms, teaches us to develop a Todo backend with Vapor framework and an iOS application, employing the concepts covered in previous chapters. We will use functional programming techniques to parse and map the data, we will use functional reactive programming to reactively manage events in applications. We will also employ protocol-oriented programming and object-oriented programming techniques as well.

What you need for this book

To follow along with the examples in this book, you'll need to have an Apple computer with macOS 10.10 or higher installed. You'll also need to install Xcode 8.3 or newer with Swift 3.1 or newer.

Who this book is for

This book is for iOS, Web, and macOS developers with basic knowledge of Swift programming who are interested in functional programming techniques. Prior knowledge of object-oriented programming and iOS app development familiarity is assumed.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The VerboseClass.h file defines an interface as a subclass of the NSObject class."

A block of code is set as follows:

let numbers = [9, 29, 19, 79] 
// Imperative example
var tripledNumbers: [Int] = []
for number in numbers {
    tripledNumbers.append(number * 3)
}
print(tripledNumbers)

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Next, we will create a Single View Application project in Xcode."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub athttps://github.com/PacktPublishing/Swift-Functional-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/SwiftFunctionalProgramming_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Limited Time Offer

$10p/m for 3 months

Get online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech and supported with AI assistants
notes
bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon

Create a Note

Modal Close icon
You need to login to use this feature.

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note