SlideShare a Scribd company logo
CLOSURE

&

HIGHER-ORDER FUNCTION
let us: Go!

2017 Spring
• Closure
• Higher-Order Functions in Swift





CLOSURE
CLOSURE
•
•
• , , ,
• :
CLOSURE
• ,
• return
•
•
CLOSURE
CLOSURE AS PARAMETER
func sorted(by areInIncreasingOrder: (E, E) -> Bool) -> [E]


(Type, Type) -> Type







CLOSURE
func backwards(left: String, right: String) -> Bool {
print("(left) (right) ")
return left > right
}
let names: [String] = ["hana", "eric", "yagom", "kim"]
let reversed: [String] = names.sorted(by: backwards)
print(reversed)



CLOSURE
let reversed: [String]
reversed = names.sorted (by: { (left: String, right:
String) -> Bool in
return left > right })
print(reversed)
// ["yagom", "kim", "hana", "eric"]





CLOSURE
//
let reversed: [String] = names.sorted() { (left: String,
right: String) -> Bool in
return left > right
}
// sorted(by:) .
let reversed: [String] = names.sorted { (left: String,
right: String) -> Bool in
return left > right
}



CLOSURE
// .
let reversed: [String] = names.sorted { (left, right) in
return left > right
}









CLOSURE
//
let reversed: [String] = names.sorted {
return $0 > $1
}
//
let reversed: [String] = names.sorted { $0 > $1 }







HIGHER-ORDER FUNCTIONS
•
• map, filter, reduce



MAP
• (transform)










TRANSFORM USING 

FOR STATEMENT
let numbers: [Int] = [0, 1, 2, 3, 4]
var doubledNumbers: [Int] = [Int]()
var strings: [String] = [String]()
// for
for number in numbers {
doubledNumbers.append(number * 2)
strings.append("(number)")
}
print(doubledNumbers) // [0, 2, 4, 6, 8]
print(strings) // ["0", "1", "2", "3", "4"]
MAP EXAMPLE
// map
doubledNumbers = numbers.map({ (number: Int) -> Int in
return number * 2
})


strings = numbers.map({ (number: Int) -> String in 

return "(number)"
})
print(doubledNumbers) // [0, 2, 4, 6, 8]
print(strings) // ["0", "1", "2", "3", "4"]

MAP EXAMPLE
// , , (return)
//
doubledNumbers = numbers.map { $0 * 2 }
print(doubledNumbers) // [0, 2, 4, 6, 8]





FILTER
• 





FILTER USING 

FOR STATEMENT
let numbers: [Int] = [0, 1, 2, 3, 4, 5]
var evenNumbers: [Int] = [Int]()
// for
for number in numbers {
if number % 2 != 0 { continue }
evenNumbers.append(number)
}
print(evenNumbers) // [0, 2, 4]



FILTER EXAMPLE
let numbers: [Int] = [0, 1, 2, 3, 4, 5]
let evenNumbers: [Int] = numbers.filter { (number: Int)
-> Bool in
return number % 2 == 0
}
print(evenNumbers) // [0, 2, 4]







FILTER EXAMPLE
let oddNumbers: [Int] = numbers.filter {
$0 % 2 != 0
}
print(oddNumbers) // [1, 3, 5]







REDUCE
• 





REDUCE USING
FOR STATEMENT
let numbers: [Int] = [2, 8, 15]
var sum: Int = 0
for number in numbers {
sum += number
}
print(sum) // 25





REDUCE EXAMPLE
let numbers: [Int] = [2, 8, 15]
// 0 .
let sum: Int = numbers.reduce(0, { (first: Int, second:
Int) -> Int in
print("(first) + (second)")
return first + second
})
print(sum) // 25
!
REDUCE EXAMPLE
let numbers: [Int] = [2, 8, 15]
// 0 .
let subtract: Int = numbers.reduce(0, { (first: Int,
second: Int) -> Int in
print("(first) - (second)")
return first - second
})
print(subtract) // -25





REDUCE EXAMPLE
// 3 .
let sumFromThree = numbers.reduce(3) { $0 + $1 }
print(sumFromThree)
// 28





/*
var sum: Int = 3
for number in numbers {
sum += number
}
*/

Q&A

More Related Content

PDF
Swift에서 꼬리재귀 사용기 (Tail Recursion)
PDF
Swift 함수 커링 사용하기
PDF
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
PDF
Python programming : Standard Input and Output
PDF
7 Habits For a More Functional Swift
PDF
iRODS Rule Language Cheat Sheet
PDF
20191116 custom operators in swift
PPTX
F# Presentation
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift 함수 커링 사용하기
Stanfy MadCode Meetup #9: Functional Programming 101 with Swift
Python programming : Standard Input and Output
7 Habits For a More Functional Swift
iRODS Rule Language Cheat Sheet
20191116 custom operators in swift
F# Presentation

What's hot (19)

PPTX
PHP Functions & Arrays
PDF
Slaying the Dragon: Implementing a Programming Language in Ruby
PPT
Class 4 - PHP Arrays
PPTX
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
PDF
Codeware
PDF
Arrays in PHP
PPTX
PHP PPT FILE
PPT
Synapse india complain sharing info about php chaptr 26
PDF
Sorting arrays in PHP
PDF
Swift Rocks #2: Going functional
PPTX
Benefits of Kotlin
PDF
Pooya Khaloo Presentation on IWMC 2015
PDF
Swift rocks! #1
PPTX
Php & my sql
PDF
Frege is a Haskell for the JVM
PDF
Elixir in a nutshell - Fundamental Concepts
PDF
Introduction to Python
PDF
PHP Unit 4 arrays
PHP Functions & Arrays
Slaying the Dragon: Implementing a Programming Language in Ruby
Class 4 - PHP Arrays
Ciklum net sat12112011-alexander fomin-expressions and all, all, all
Codeware
Arrays in PHP
PHP PPT FILE
Synapse india complain sharing info about php chaptr 26
Sorting arrays in PHP
Swift Rocks #2: Going functional
Benefits of Kotlin
Pooya Khaloo Presentation on IWMC 2015
Swift rocks! #1
Php & my sql
Frege is a Haskell for the JVM
Elixir in a nutshell - Fundamental Concepts
Introduction to Python
PHP Unit 4 arrays
Ad

Similar to Closure, Higher-order function in Swift (20)

PPTX
Python Course Module 2 Topics and content
PDF
Python Variable Types, List, Tuple, Dictionary
PDF
Session -5for students.pdf
PPTX
Python Programming for basic beginners.pptx
PPTX
python lists with examples and explanation python lists with examples and ex...
PDF
python_avw - Unit-03.pdf
PDF
Python Fundamentals - Basic
PDF
Python Usage (5-minute-summary)
PPTX
Python 101++: Let's Get Down to Business!
PDF
Python for High School Programmers
PPTX
list and control statement.pptx
PPTX
Python- Basic. pptx with lists, tuples dictionaries and data types
PPTX
Python- Basic.pptx with data types, lists, and tuples with dictionary
PPT
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
PDF
Writeable CTEs: The Next Big Thing
PDF
Extending Operators in Perl with Operator::Util
PDF
25 MARCH1 list, generator.pdf list generator
PDF
PDF
Go Containers
Python Course Module 2 Topics and content
Python Variable Types, List, Tuple, Dictionary
Session -5for students.pdf
Python Programming for basic beginners.pptx
python lists with examples and explanation python lists with examples and ex...
python_avw - Unit-03.pdf
Python Fundamentals - Basic
Python Usage (5-minute-summary)
Python 101++: Let's Get Down to Business!
Python for High School Programmers
list and control statement.pptx
Python- Basic. pptx with lists, tuples dictionaries and data types
Python- Basic.pptx with data types, lists, and tuples with dictionary
UNIT 4 PY.ppt - DATA STRUCTURE IN PYTHON
Writeable CTEs: The Next Big Thing
Extending Operators in Perl with Operator::Util
25 MARCH1 list, generator.pdf list generator
Go Containers
Ad

Recently uploaded (20)

PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
PPTX
10 Hidden App Development Costs That Can Sink Your Startup.pptx
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
PPTX
Save Business Costs with CRM Software for Insurance Agents
PDF
How to Confidently Manage Project Budgets
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
PDF
Jenkins: An open-source automation server powering CI/CD Automation
PDF
Become an Agentblazer Champion Challenge
PDF
A REACT POMODORO TIMER WEB APPLICATION.pdf
PDF
Comprehensive Salesforce Implementation Services.pdf
PPTX
Hire Expert WordPress Developers from Brainwings Infotech
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
PDF
The Future of Smart Factories Why Embedded Analytics Leads the Way
PDF
Become an Agentblazer Champion Challenge Kickoff
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
10 Hidden App Development Costs That Can Sink Your Startup.pptx
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
Save Business Costs with CRM Software for Insurance Agents
How to Confidently Manage Project Budgets
How to Migrate SBCGlobal Email to Yahoo Easily
Mastering-Cybersecurity-The-Crucial-Role-of-Antivirus-Support-Services.pptx
Jenkins: An open-source automation server powering CI/CD Automation
Become an Agentblazer Champion Challenge
A REACT POMODORO TIMER WEB APPLICATION.pdf
Comprehensive Salesforce Implementation Services.pdf
Hire Expert WordPress Developers from Brainwings Infotech
AIRLINE PRICE API | FLIGHT API COST |
2025 Textile ERP Trends: SAP, Odoo & Oracle
Online Work Permit System for Fast Permit Processing
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
The Future of Smart Factories Why Embedded Analytics Leads the Way
Become an Agentblazer Champion Challenge Kickoff

Closure, Higher-order function in Swift