Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift Functional Programming

You're reading from   Swift Functional Programming Ease the creation, testing, and maintenance of Swift codes

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781787284500
Length 316 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Dr. Fatih Nayebi Dr. Fatih Nayebi
Author Profile Icon Dr. Fatih Nayebi
Dr. Fatih Nayebi
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with Functional Programming in Swift 2. Functions and Closures FREE CHAPTER 3. Types and Type Casting 4. Enumerations and Pattern Matching 5. Generics and Associated Type Protocols 6. Map, Filter, and Reduce 7. Dealing with Optionals 8. Functional Data Structures 9. Importance of Immutability 10. Best of Both Worlds and Combining FP Paradigms with OOP 11. Case Study - Developing an iOS Application with FP and OOP Paradigms

First-class functions


In the Function types section of this chapter, we saw that we can define function types and store and pass functions around. In practice, this means that Swift treats functions as values. To explain this, we will need to examine a couple of examples:

let name: String = "Grace" 

In this code example, we create a constant of the String type name and store a value ("Grace") into it.

When we define a function, we need to specify the type of parameter, as follows:

func sayHello(name: String) { 
    print("Hello, \(name)") 
} 

In this example, our name parameter is of the String type. This parameter could be any other value type or reference type. Simply, it could be Int, Double, Dictionary, Array, Set, or it could be an object type such as an instance of class, struct, or enum.

Now, let's call this function:

sayHello(name: "Your name") // or 
sayHello(name: name) 

Here, we pass a value for this parameter. In other words, we pass one of the previously mentioned types with their respective...

You have been reading a chapter from
Swift Functional Programming - Second Edition
Published in: Apr 2017
Publisher: Packt
ISBN-13: 9781787284500
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime