Home Page Icon
Home Page
Table of Contents for
Table of Contents
Close
Table of Contents
by Dr. Fatih Nayebi
Swift 3 Functional Programming
Swift 3 Functional Programming
Swift 3 Functional Programming
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started with Functional Programming in Swift
Why functional programming matters?
What is functional programming?
The Swift programming language
Swift features
Modern syntax
Type safety and type inference
Immutability
Stateless programming
First-class functions
Higher-order functions
Pattern matching
Generics
Closures
Subscripts
Optional chaining
Extensions
Objective-C and Swift bridging headers
Automatic Reference Counting
REPL and Playground
Language basics
Type safety and type inference
Type annotation
Type aliases
Immutability
Tuples
Optionals
Basic operators
Strings and characters
Immutability
String literals
Empty Strings
Concatenating strings and characters
String interpolation
String comparison
Collections
Control flows
for loops
while loops
stride
if
switch
guard
Functions
Closures
Map, filter, and reduce
Map
Filter
Reduce
Enumerations
Generics
Classes and structures
Classes versus structures
Choosing between classes and structures
Identity operators
Properties
Property observers
Methods
Subscripts
Inheritance
Initialization
Deinitialization
Automatic Reference Counting
Optionals and optional chaining
Error handling
Type casting
Any and AnyObject
Nested types
Extensions
Protocols
Protocols as types
Protocol extensions
Access control
Summary
2. Functions and Closures
What is a function?
The general syntax of functions and methods
Best practices in function definition
Calling functions
Defining and using function parameters
Defining and using variadic functions
Returning values from functions
Pure functions
Function types
Defining and using nested functions
First-class functions
Higher-order functions
Function composition
Custom operators
Allowed operators
Custom operator definition
A composed function with a custom operator
Closures
Closure syntax
Capturing values
Function currying
Recursion
Tail recursion
Memoization
Summary
3. Types and Type casting
Value versus reference types
Value and reference type constants
Mixing value and reference types
Copying
Copying reference types
Value type characteristics
Behavior
Isolation
Interchangeability
Testability
Threats
Using value and reference types
Equality versus identity
Equatable and Comparable
Type checking and casting
Summary
4. Enumerations and Pattern Matching
Defining enumerations
Associated values
Raw values
Algebraic data types
Simple types
Composite types
Composite type with variants
The algebra of data types
Pattern matching
Patterns and pattern matching
The wildcard pattern
The value-binding pattern
The identifier pattern
The tuple pattern
The enumeration case pattern
The optional pattern
Type casting patterns
The expression pattern
Summary
5. Generics and Associated Type Protocols
What are generics and what kind of problems do they solve?
Type constraints
Where clauses
Generic data structures
Associated type protocols
Extending generic types
Subclassing generic classes
Summary
6. Map, Filter, and Reduce
Functor
Applicative Functor
Monad
Map
FlatMap and flatten
Filter
Reduce
The map function in terms of reduce
The filter function in terms of reduce
The flatMap function in terms of reduce
The flatten function in terms of reduce
Apply
Join
Chaining higher-order functions
Zip
Practical examples
Sum of an array
Product of an array
Removing nil values from an array
Removing duplicates in an array
Partitioning an array
Summary
7. Dealing with Optionals
Optional types
Unwrapping optionals
Force unwrapping
nil checking
Optional binding
Guard
Implicitly unwrapped optionals
Error handling to avoid optionals
try!
try?
Nil-coalescing
Optional chaining
Dealing with optionals functionally
Optional mapping
Multiple optional value mapping
Summary
8. Functional Data Structures
Semigroup
Monoid
Trees
Contains
Binary Search Tree
Contains
Size
Elements
Empty
Lists
Empty LinkedList
Cons
Contains
Size
Elements
isEmpty
map, filter, and reduce
Stacks
Lazy list
Summary
9. Importance of Immutability
Immutability
Immutable variables
Weak versus strong immutability
Reference types versus value types
The benefits of immutability
Thread safety
Referential transparency
Low coupling
Avoiding temporal coupling
Avoiding identity mutability
Failure atomicity
Parallelization
Exception handling and error management
Caching
State comparison
Compiler optimization
Cases for mutability
An example
Side-effects and unintended consequences
Testability
Copy constructors and lenses
Copy constructor
Lens
Lens composition
Summary
10. The Best of Both Worlds – Combining FP Paradigms with OOP
OOP paradigms
Objects
Classes
Inheritance
Overriding
Design constraints
Singleness
Static
Visibility
Composite reuse
Issues and alternatives
When to inherit
Polymorphism
Dynamic binding
OOP design principles
SRP
The FP counterpart
OCP
The FP counterpart
LSP
The FP counterpart
ISP
The FP counterpart
DIP
The FP counterpart
DDD
Concepts
Premise
Building blocks
Aggregate
Immutable value objects
Domain events
Intention-revealing interface
Side-effect-free functions
Assertions
Conceptual contours
Closure of operations
Declarative design
Protocol-oriented programming (POP)
POP paradigms
Protocol composition
Protocol extensions
Protocol inheritance
Associated types
Conforming to a protocol
Functional Reactive Programming (FRP)
Building blocks of FRP
Signals
Pipes
Signal producers
Buffers
Observers
Actions
Properties
Disposables
Schedulers
An example
Mixing OOP and FP
Problems
Granularity mismatch
FP paradigm availability
First-class values
Closures
FP-OOP interrelation tools
FP support
Effects of having FP capabilities in OOP
Idiomatic effects
Architectural effects
OOP design patterns - an FP perspective
Strategy pattern
Command pattern
Observer pattern
Virtual proxy pattern
Visitor pattern
Summary
11. Case Study – Developing an iOS Application with the FP and OOP Paradigms
Requirements
High-level design
Frontend
Models
Views
ViewController
State
Store
Actions
Manager
Communication
Communication between layers
Third-party libraries
Cross-cutting concerns
Error management and exception handling
Crash reporting
Analytics
Tools
Backend
Vapor
Routing
JSON
Requesting data
SPM
Backend development
Model
Store
Controller
Posting a new Todo item
Getting a list of Todo items
Getting a specific Todo item
Deleting an item and deleting all Todo items
Updating a Todo item
iOS application development
Configuration
Models
Operators
<^>
<*>
<|
<|?
<||
Using Argo models
ViewModel
Communication
Request protocol
Conforming to a request protocol
WebServiceManager
Creating a Todo item
Listing Todo items
Lens
State
Store
Action
Views
ViewController
MasterViewController
IBActions
TableView Delegates and DataSource
DetailsViewController
Summary
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Cover
Next
Next Chapter
Swift 3 Functional Programming
Table of Contents
Swift 3 Functional Programming
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started with Functional Programming in Swift
Why functional programming matters?
What is functional programming?
The Swift programming language
Swift features
Modern syntax
Type safety and type inference
Immutability
Stateless programming
First-class functions
Higher-order functions
Pattern matching
Generics
Closures
Subscripts
Optional chaining
Extensions
Objective-C and Swift bridging headers
Automatic Reference Counting
REPL and Playground
Language basics
Type safety and type inference
Type annotation
Type aliases
Immutability
Tuples
Optionals
Basic operators
Strings and characters
Immutability
String literals
Empty Strings
Concatenating strings and characters
String interpolation
String comparison
Collections
Control flows
for loops
while loops
stride
if
switch
guard
Functions
Closures
Map, filter, and reduce
Map
Filter
Reduce
Enumerations
Generics
Classes and structures
Classes versus structures
Choosing between classes and structures
Identity operators
Properties
Property observers
Methods
Subscripts
Inheritance
Initialization
Deinitialization
Automatic Reference Counting
Optionals and optional chaining
Error handling
Type casting
Any and AnyObject
Nested types
Extensions
Protocols
Protocols as types
Protocol extensions
Access control
Summary
2. Functions and Closures
What is a function?
The general syntax of functions and methods
Best practices in function definition
Calling functions
Defining and using function parameters
Defining and using variadic functions
Returning values from functions
Pure functions
Function types
Defining and using nested functions
First-class functions
Higher-order functions
Function composition
Custom operators
Allowed operators
Custom operator definition
A composed function with a custom operator
Closures
Closure syntax
Capturing values
Function currying
Recursion
Tail recursion
Memoization
Summary
3. Types and Type casting
Value versus reference types
Value and reference type constants
Mixing value and reference types
Copying
Copying reference types
Value type characteristics
Behavior
Isolation
Interchangeability
Testability
Threats
Using value and reference types
Equality versus identity
Equatable and Comparable
Type checking and casting
Summary
4. Enumerations and Pattern Matching
Defining enumerations
Associated values
Raw values
Algebraic data types
Simple types
Composite types
Composite type with variants
The algebra of data types
Pattern matching
Patterns and pattern matching
The wildcard pattern
The value-binding pattern
The identifier pattern
The tuple pattern
The enumeration case pattern
The optional pattern
Type casting patterns
The expression pattern
Summary
5. Generics and Associated Type Protocols
What are generics and what kind of problems do they solve?
Type constraints
Where clauses
Generic data structures
Associated type protocols
Extending generic types
Subclassing generic classes
Summary
6. Map, Filter, and Reduce
Functor
Applicative Functor
Monad
Map
FlatMap and flatten
Filter
Reduce
The map function in terms of reduce
The filter function in terms of reduce
The flatMap function in terms of reduce
The flatten function in terms of reduce
Apply
Join
Chaining higher-order functions
Zip
Practical examples
Sum of an array
Product of an array
Removing nil values from an array
Removing duplicates in an array
Partitioning an array
Summary
7. Dealing with Optionals
Optional types
Unwrapping optionals
Force unwrapping
nil checking
Optional binding
Guard
Implicitly unwrapped optionals
Error handling to avoid optionals
try!
try?
Nil-coalescing
Optional chaining
Dealing with optionals functionally
Optional mapping
Multiple optional value mapping
Summary
8. Functional Data Structures
Semigroup
Monoid
Trees
Contains
Binary Search Tree
Contains
Size
Elements
Empty
Lists
Empty LinkedList
Cons
Contains
Size
Elements
isEmpty
map, filter, and reduce
Stacks
Lazy list
Summary
9. Importance of Immutability
Immutability
Immutable variables
Weak versus strong immutability
Reference types versus value types
The benefits of immutability
Thread safety
Referential transparency
Low coupling
Avoiding temporal coupling
Avoiding identity mutability
Failure atomicity
Parallelization
Exception handling and error management
Caching
State comparison
Compiler optimization
Cases for mutability
An example
Side-effects and unintended consequences
Testability
Copy constructors and lenses
Copy constructor
Lens
Lens composition
Summary
10. The Best of Both Worlds – Combining FP Paradigms with OOP
OOP paradigms
Objects
Classes
Inheritance
Overriding
Design constraints
Singleness
Static
Visibility
Composite reuse
Issues and alternatives
When to inherit
Polymorphism
Dynamic binding
OOP design principles
SRP
The FP counterpart
OCP
The FP counterpart
LSP
The FP counterpart
ISP
The FP counterpart
DIP
The FP counterpart
DDD
Concepts
Premise
Building blocks
Aggregate
Immutable value objects
Domain events
Intention-revealing interface
Side-effect-free functions
Assertions
Conceptual contours
Closure of operations
Declarative design
Protocol-oriented programming (POP)
POP paradigms
Protocol composition
Protocol extensions
Protocol inheritance
Associated types
Conforming to a protocol
Functional Reactive Programming (FRP)
Building blocks of FRP
Signals
Pipes
Signal producers
Buffers
Observers
Actions
Properties
Disposables
Schedulers
An example
Mixing OOP and FP
Problems
Granularity mismatch
FP paradigm availability
First-class values
Closures
FP-OOP interrelation tools
FP support
Effects of having FP capabilities in OOP
Idiomatic effects
Architectural effects
OOP design patterns - an FP perspective
Strategy pattern
Command pattern
Observer pattern
Virtual proxy pattern
Visitor pattern
Summary
11. Case Study – Developing an iOS Application with the FP and OOP Paradigms
Requirements
High-level design
Frontend
Models
Views
ViewController
State
Store
Actions
Manager
Communication
Communication between layers
Third-party libraries
Cross-cutting concerns
Error management and exception handling
Crash reporting
Analytics
Tools
Backend
Vapor
Routing
JSON
Requesting data
SPM
Backend development
Model
Store
Controller
Posting a new Todo item
Getting a list of Todo items
Getting a specific Todo item
Deleting an item and deleting all Todo items
Updating a Todo item
iOS application development
Configuration
Models
Operators
<^>
<*>
<|
<|?
<||
Using Argo models
ViewModel
Communication
Request protocol
Conforming to a request protocol
WebServiceManager
Creating a Todo item
Listing Todo items
Lens
State
Store
Action
Views
ViewController
MasterViewController
IBActions
TableView Delegates and DataSource
DetailsViewController
Summary
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset