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)

PPTX
Transform Your Business with a Software ERP System
PPT
Introduction Database Management System for Course Database
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
top salesforce developer skills in 2025.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
System and Network Administraation Chapter 3
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Designing Intelligence for the Shop Floor.pdf
Transform Your Business with a Software ERP System
Introduction Database Management System for Course Database
VVF-Customer-Presentation2025-Ver1.9.pptx
Operating system designcfffgfgggggggvggggggggg
Wondershare Filmora 15 Crack With Activation Key [2025
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
top salesforce developer skills in 2025.pdf
history of c programming in notes for students .pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
System and Network Administraation Chapter 3
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
wealthsignaloriginal-com-DS-text-... (1).pdf
Digital Systems & Binary Numbers (comprehensive )
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PTS Company Brochure 2025 (1).pdf.......
Navsoft: AI-Powered Business Solutions & Custom Software Development
Designing Intelligence for the Shop Floor.pdf

Closure, Higher-order function in Swift