SlideShare a Scribd company logo
High Order Functions
Ahmet Keskin
iOS Developer @trendyol.com
Closures and functions are first
class types in swift
Functions and closures are first-class citizens in Swift because you can
treat then like a normal value. For example, you can;
❖ Assign a function/closure to a local variable
❖ Pass a function/closure as an argument
❖ Return a function/closure
Assign a function/closure to a local variable
Pass a function/closure as an argument
Return a function/closure
Lets start with map(_:)
Returns an array containing the results of mapping the given closure over the sequence’s
elements.
Declaration;
Parameters;
transform ->
A mapping closure. transform accepts an element of this sequence as its parameter and returns a
transformed value of the same or of a different type.
Return Value;
An array containing the transformed elements of this sequence.
Map in Array
Map in Dictionary
Highorderfunctions in swift
compactMap(_:)
Returns an array containing the non-nil results of calling the given transformation with each
element of this sequence.
Declaration;
Parameters;
transform ->
A closure that accepts an element of this sequence as its argument and returns an optional value.
Return Value;
An array of the non-nil results of calling transform with each element of the sequence.
CompactMap in Array
flatMap(_:)
Returns the concatenated results of mapping the given transformation over this sequence.
Declaration;
Discussion;
Use this method to receive a single-level sequence when your transformation produces a
sequence or collection for each element. Calling flatMap(_:) on a sequence s is equivalent to
calling s.map(transform).joined().
flatMap in Array
filter(_:)
Returns an array containing, in order, the elements of the sequence that satisfy the given
predicate.
Declaration;
Parameters;
isIncluded ->
A closure that takes an element of the sequence as its argument and returns a Boolean value
indicating whether the element should be included in the returned array.
Return Value;
An array of the elements that isIncluded allowed.
filter in Array
filter in Dictionary
sorted(_:)
Returns the elements of the sequence, sorted.
Declaration;
Return Value;
A sorted array of the sequence’s elements.
reduce(_:_:)
Returns the result of combining the elements of the sequence using the given closure.
Declaration;
Return Value;
The final accumulated value. If the sequence has no elements, the result is initialResult.
Parameters;
initialResult ->
The value to use as the initial accumulating value. initialResult is passed to nextPartialResult the first time the closure is
executed.
nextPartialResult ->
A closure that combines an accumulating value and an element of the sequence into a new accumulating value, to be used
in the next call of the nextPartialResult closure or returned to the caller.
Highorderfunctions in swift
Dinlediğiniz için teşekkürler
Ahmet Keskin
iOS Developer @trendyol.com
ahmet.keskin@trendyol.com
https://github.com/AhmettKeskin
https://medium.com/@ahmetkeskin
https://twitter.com/_Ahmettkeskin

More Related Content

PPTX
Sorting two numbers
PPT
composing procedures
PPT
Data Structures - Searching & sorting
PPT
best first-sort
PPT
recursive problem_solving
PDF
PDF
Functional programming 101
Sorting two numbers
composing procedures
Data Structures - Searching & sorting
best first-sort
recursive problem_solving
Functional programming 101

What's hot (20)

PDF
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
PDF
CSEC Mathematics Review - Introduction To Functions & Relations
PPTX
Insertion sort
PDF
Functional programming java
PPTX
Static variable
PPT
Odersky week1 notes
PPT
evaluating recursive_applications
PPTX
Stacks IN DATA STRUCTURES
PPTX
Functions
PPTX
Function Parameters
PDF
U nit i data structure-converted
PDF
Unit ii data structure-converted
DOCX
C programming language working with functions 1
PPTX
Insertion sort
DOCX
Selection sort lab mannual
PPTX
Classes function overloading
PPTX
Address calculation-sort
PDF
Applications of stack
PPTX
DBMS - Relational Algebra
PPTX
Data structures and algorithms lab3
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
CSEC Mathematics Review - Introduction To Functions & Relations
Insertion sort
Functional programming java
Static variable
Odersky week1 notes
evaluating recursive_applications
Stacks IN DATA STRUCTURES
Functions
Function Parameters
U nit i data structure-converted
Unit ii data structure-converted
C programming language working with functions 1
Insertion sort
Selection sort lab mannual
Classes function overloading
Address calculation-sort
Applications of stack
DBMS - Relational Algebra
Data structures and algorithms lab3
Ad

Similar to Highorderfunctions in swift (20)

PDF
JSDC 2014 - functional java script, why or why not
PPTX
A Skeptics guide to functional style javascript
PPTX
ECMA5 and ES6 Promises
PPTX
Functional Programming in Javascript - IL Tech Talks week
PPTX
8558537werr.pptx
PDF
Map, Reduce and Filter in Swift
PDF
Closures
PPTX
Academy PRO: ES2015
PDF
7 Habits For a More Functional Swift
PDF
Introduction to Functional Programming (w/ JS)
PDF
TI1220 Lecture 6: First-class Functions
PDF
Javascript
PDF
ES6: The future is now
PDF
Closure, Higher-order function in Swift
PDF
Functional Programming: An Introduction
PDF
An introduction to functional programming with Swift
PDF
Whats new in ES2019
PPTX
ES6 Overview
ODP
EcmaScript 6
PDF
Introduction to ECMAScript 2015
JSDC 2014 - functional java script, why or why not
A Skeptics guide to functional style javascript
ECMA5 and ES6 Promises
Functional Programming in Javascript - IL Tech Talks week
8558537werr.pptx
Map, Reduce and Filter in Swift
Closures
Academy PRO: ES2015
7 Habits For a More Functional Swift
Introduction to Functional Programming (w/ JS)
TI1220 Lecture 6: First-class Functions
Javascript
ES6: The future is now
Closure, Higher-order function in Swift
Functional Programming: An Introduction
An introduction to functional programming with Swift
Whats new in ES2019
ES6 Overview
EcmaScript 6
Introduction to ECMAScript 2015
Ad

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
iTop VPN Crack Latest Version Full Key 2025
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Cost to Outsource Software Development in 2025
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Nekopoi APK 2025 free lastest update
Odoo Companies in India – Driving Business Transformation.pdf
iTop VPN Crack Latest Version Full Key 2025
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Cost to Outsource Software Development in 2025
Design an Analysis of Algorithms I-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
17 Powerful Integrations Your Next-Gen MLM Software Needs
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
wealthsignaloriginal-com-DS-text-... (1).pdf
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx

Highorderfunctions in swift

  • 1. High Order Functions Ahmet Keskin iOS Developer @trendyol.com
  • 2. Closures and functions are first class types in swift Functions and closures are first-class citizens in Swift because you can treat then like a normal value. For example, you can; ❖ Assign a function/closure to a local variable ❖ Pass a function/closure as an argument ❖ Return a function/closure
  • 3. Assign a function/closure to a local variable
  • 4. Pass a function/closure as an argument
  • 6. Lets start with map(_:) Returns an array containing the results of mapping the given closure over the sequence’s elements. Declaration; Parameters; transform -> A mapping closure. transform accepts an element of this sequence as its parameter and returns a transformed value of the same or of a different type. Return Value; An array containing the transformed elements of this sequence.
  • 10. compactMap(_:) Returns an array containing the non-nil results of calling the given transformation with each element of this sequence. Declaration; Parameters; transform -> A closure that accepts an element of this sequence as its argument and returns an optional value. Return Value; An array of the non-nil results of calling transform with each element of the sequence.
  • 12. flatMap(_:) Returns the concatenated results of mapping the given transformation over this sequence. Declaration; Discussion; Use this method to receive a single-level sequence when your transformation produces a sequence or collection for each element. Calling flatMap(_:) on a sequence s is equivalent to calling s.map(transform).joined().
  • 14. filter(_:) Returns an array containing, in order, the elements of the sequence that satisfy the given predicate. Declaration; Parameters; isIncluded -> A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned array. Return Value; An array of the elements that isIncluded allowed.
  • 17. sorted(_:) Returns the elements of the sequence, sorted. Declaration; Return Value; A sorted array of the sequence’s elements.
  • 18. reduce(_:_:) Returns the result of combining the elements of the sequence using the given closure. Declaration; Return Value; The final accumulated value. If the sequence has no elements, the result is initialResult. Parameters; initialResult -> The value to use as the initial accumulating value. initialResult is passed to nextPartialResult the first time the closure is executed. nextPartialResult -> A closure that combines an accumulating value and an element of the sequence into a new accumulating value, to be used in the next call of the nextPartialResult closure or returned to the caller.
  • 20. Dinlediğiniz için teşekkürler Ahmet Keskin iOS Developer @trendyol.com ahmet.keskin@trendyol.com https://github.com/AhmettKeskin https://medium.com/@ahmetkeskin https://twitter.com/_Ahmettkeskin