SlideShare a Scribd company logo
Functional Programming
in Swift
Presented by: Saugat Gautam
What is functional
programming?
Functional Programming
• A programming paradigm that treats computations
as the evaluation of mathematical functions and
avoids changing-state and mutable data. It is
declarative programming paradigm, which means
programming is done with expressions.
Why Functional
Programming?
Why?
• Maintainable code.
• Easier and complete testing.
• Easier parallel and concurrent programming.
Aspects of Functional
Programming
Aspects
• Immutable Data(Values)
• Pure functions
• Higher Order Functions
• Functions as arguments and as return values.
Immutable Data?
Immutable Values benefits
• Values can’t change unexpectedly
• Can be shared without worrying
• Thread safe - Values play well with observers
Example
let firstName = "Saugat"
let lastName = “Gautam"
var middleName = "Kumar"
let numbers = Array(1…10)
firstName.append("D") // Compiler Error
middleName.append("atunga") // All good
//—————————————————————————————————————
struct PersonalInfo {
var firstName: String
var lastName: String
var middleName: String
}
let emp1 = PersonalInfo(firstName: "Saugat", lastName: "Gautam", middleName:
More Benefits
• The Value of Values - Rich Hickey
http://www.infoq.com/presentations/Value-Values
• Controlling Complexity in Swift or Making Friends
with Value- Andy Matuschak
http://realm.io/news/andy-matuschak-controlling-
complexity/
http://www.objc.io/issue-16/swift-classes-vs-
structs.html
Why functional swift?
Why Functional Swift?
• Generics.
• Functional Programming Concept.
• Nice consistent syntax for functions and closures.
Can’t Objective-C do
it?
It can but we probably don’t want to.
Swift Value Types vs
Reference Types
• Value Types:
• Numbers, strings, arrays, dictionaries, enums, tuples, and
structs.
• Reference Types:
• Classes
Value Types
• Single owner
• Copied on assign
• Example
var a = 10
var b = a
// a = 10, b = 5
b = 5
// a = 10, b = 5
Reference Types
• Multiple owners
• Shared on assignment
var a = UIView()
var b = a
// a.alpha = 1, b.alpha = 1
b.alpha = 0
// a.alpha = 0; b.alpha = 0
Pure Functions
Pure Functions
• Same input always results in same output
(Referential Transparency)
• Evaluation of result does not cause any side effect
• eg. No change in database or to I/O devices
• Result value need not depend on all arguments but
it must depend on nothing other than the arguments
Pure Function
func factorial(number: Int) -> Int {
if (number == 1) {
return 1
} else {
return number * factorial(number - 1)
}
}
factorial(6)
Not Pure!!
func naturalSum(numbers:[Int]) -> Int{
var total = 0
for num in numbers {
total = total + num
}
return total
}
func sayHelloTo(name: String) {
println("Hello (name)")
}
Higher Order Functions
• Function as parameter
• Function as return value
Function as Parameter
func average(x: Int, y: Int, f:(Int -> Int)) -> Int{
return (f(x) + f(y))/2
}
let square = {(x: Int) -> Int in return x * x}
average(2, 3, f:square)
Function as Parameter
func average(x: Int, y: Int, f:(Int -> Int) = {x in return x}) -> Int{
return (f(x) + f(y))/2
}
let square = {(x: Int) -> Int in return x * x}
average(2, 3, f:square)
Function as Return
Value
func add(a: Int) -> (Int -> Int) {
return {b in a + b}
}
// partial application
let addThree = add(3)
//
let x3 = xs2.map(addThree)
Functional Tool Box
• Map
• Reduce
• Filter
Functional Tool Box
• Laziness
• Flatmap
• Currying
Functional Toolbox
• Functors, Applicative Functors
• Monads
References:
Functional Programming in Swift
http://www.objc.io/books/
Why is a Monad like a writing desk?
http:www.infoq.com/presentations/Why-is-a-Monad-Like-a-Writing-Desk
Map
Map
• Transforming data
• Normally number of output collections is equal to
input
Map Example for Array<T>
• Produce a list of the squares of the first 10 positive
numbers.
Imperative Approach
let firstTenPositiveIntegers = [Int](1…10)
var firstTenSquares = [Int]()
for number in firstTenPositiveIntegers {
firstTenSquares.append(number*number)
}
println(firstTenSquares)
// [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Problems with Imperative
Approach
• Mixes looping with transformation
• Not declarative
• What if ..
firstTenSquares.append(23)
// [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 23]
Functional Approach
let firstTenPositiveIntegers = [Int](1...10)
let firstTenSquares = firstTenPositiveIntegers.map {$0 * $0}
println(firstTenSquares)
// [1, 4, … , 100]
Benefits of Functional
Approach
• Declarative. States what we want to do. Not how we
want to do it.
• Separates the mechanism(step through and select
each item) from the transformation logic
Filter
Filter
• Transforms the initial collection to some final
collection applying some condition
• Final size may not be equal to initial size
Filter Example for Array<T>
• Requirement:
• Produce a list of even squares of the first 10
positive numbers.
Imperative Approach:
let firstTenPositiveIntegers = [Int](1...10)
var evenSquaresInFirstTen = [Int]()
for number in firstTenPositiveIntegers {
let square = number * number
if (square % 2) == 0 {
evenSquaresInFirstTen.append(square)
}
}
println(evenSquaresInFirstTen)
// [4, 16, 36, 64, 100]
Functional Approach
let firstTenPositiveIntegers = [Int](1…10)
let evenSquaresInFirstTen = firstTenPositiveIntegers.map({$0 * $0}).filter({($0%2) == 0})
println(evenSquaresInFirstTen)
// [4, 16, 36, 64, 100]
Reduce
Reduce
• Transform a collection to a single value
Reduce Example for
Array<T>
• Sum of the even squares in the first 10 positive
numbers?
Imperative Approach
let firstTenPositiveIntegers = [Int](1…10)
var sumOfEvenSquaresInFirstTen = 0
for number in firstTenPositiveIntegers {
let square = number * number
if (square % 2) == 0 {
sumOfEvenSquaresInFirstTen += square
}
}
println(sumOfEvenSquaresInFirstTen)
// 220
Functional Approach
let firstTenPositiveIntegers = [Int](1…10)
let sumOfEvenSquaresInFirstTen = firstTenPositiveIntegers.map({$0 *
.filter({$0 %2 == 0})
.reduce(0, combine: {$0 + $1})
println(sumOfEvenSquaresInFirstTen)
// 220
Functional Approach
let firstTenPositiveIntegers = [Int](1...10)
let sumOfEvenSquaresInFirstTen = firstTenPositiveIntegers.map({$0 * $0}).reduce
println(sumOfEvenSquaresInFirstTen)
// 220
Currying
Currying
• Translating the evaluation of a function that takes
multiple arguments into evaluating a sequence of
functions each with a single argument(partial
application)
Currying example
• Say we have a method that takes two inputs and
add them
func add(a: Int, b: Int) -> Int {
return a + b
}
let sum = add(2, 3) // sum = 5
Currying Example
• Now we want to add 2 to a list of numbers
• Drawbacks
• creating a closure just for passing add with default
value
• difficulty arises in case multiple default values are
needed
let xs = 1...100
let x = xs.map { add($0, 2) } // x = [3, 4, 5, 6, etc]
Currying Example
• Use currying
• Drawbacks
• need of wrapper functions for each integer
func addTwo(a: Int) -> Int {
return add(a, 2)
}
let xs = 1...100
let x = xs2.map(addTwo)
Currying Example
• Use another currying method
func add(a: Int) -> (Int -> Int) {
return {b in a + b}
}
// Using method as a whole;
add(2)(3)
// partial application
let addThree = add(3)
//
let x3 = xs2.map(addThree)
Application
Architecture
How does functional fit in?
Application Architecture
• Cocoa and Cocoa Touch are inherently imperative
• Don’t fight the framework
Infusing functional thinking
into our Cocoa applications?
• Functional Core / Imperative Shell approach
• References
• https://www.destroyallsoftware.com/screencasts/catalog/functional-core-
imperative-shell
• https://speakerdeck.com/jspahrsummers/enemy-of-the-state
Functional Core / Imperative
Shell
• Place application’s core data and logic in immutable
constructs (value types).
• Push state out to the ‘edges’. Represent state as
objects with mutable references to immutable core
constructs.
Benefits
• Allows isolated testing
• Leads to an imperative shell with few conditionals,
making reasoning about the program’s state over
time much easier
UI - Can We Functionalize
the Imperative Shell?
• Not quite there.
Some references worth mentioning.
• Reactive Cocoa
https://github.com/ReactiveCocoa/ReactiveCocoa
• React Native
http://www.reactnative.com
• Functional View Controllers
http://chris.eidhof.nl/posts/functinal-view-controllers.html
Thank You

More Related Content

What's hot (20)

PDF
SonarQube
Gnanaseelan Jeb
 
PDF
啟動 Laravel 與環境設定
Shengyou Fan
 
PPTX
Practical Application of the API Security Top Ten: A Tester's Perspective
RajniHatti
 
PDF
Vue.js
Jadson Santos
 
PDF
MongoDB
wiTTyMinds1
 
PPTX
Python basics
RANAALIMAJEEDRAJPUT
 
PDF
C# Lab Programs.pdf
Prof. Dr. K. Adisesha
 
PPT
Recursividade em C
Caique Silva
 
PPTX
Presentation php
Muhammad Saqib Malik
 
PPTX
Features of 'c' program
veer patel
 
PPTX
Flutter
Himanshu Singh
 
PPT
SonarQube Overview
Ahmed M. Gomaa
 
PPTX
Robot Framework
Onur Baskirt
 
PDF
Angular Observables & RxJS Introduction
Rahat Khanna a.k.a mAppMechanic
 
PDF
Binary Reading in C#
Yoshifumi Kawai
 
PPTX
Introduction to python history and platforms
Kirti Verma
 
PPTX
Intro to kotlin
Tomislav Homan
 
PDF
Intro to react native
ModusJesus
 
PDF
An introduction to Google test framework
Abner Chih Yi Huang
 
PDF
Introdução APIs RESTful
Douglas V. Pasqua
 
SonarQube
Gnanaseelan Jeb
 
啟動 Laravel 與環境設定
Shengyou Fan
 
Practical Application of the API Security Top Ten: A Tester's Perspective
RajniHatti
 
MongoDB
wiTTyMinds1
 
Python basics
RANAALIMAJEEDRAJPUT
 
C# Lab Programs.pdf
Prof. Dr. K. Adisesha
 
Recursividade em C
Caique Silva
 
Presentation php
Muhammad Saqib Malik
 
Features of 'c' program
veer patel
 
SonarQube Overview
Ahmed M. Gomaa
 
Robot Framework
Onur Baskirt
 
Angular Observables & RxJS Introduction
Rahat Khanna a.k.a mAppMechanic
 
Binary Reading in C#
Yoshifumi Kawai
 
Introduction to python history and platforms
Kirti Verma
 
Intro to kotlin
Tomislav Homan
 
Intro to react native
ModusJesus
 
An introduction to Google test framework
Abner Chih Yi Huang
 
Introdução APIs RESTful
Douglas V. Pasqua
 

Similar to Functional Programming in Swift (20)

PDF
Functional Swift
Geison Goes
 
PDF
Functional go
Geison Goes
 
PPTX
Functional programming
Prashant Kalkar
 
PDF
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Tech in Asia ID
 
PPTX
Functional programming
PiumiPerera7
 
PPTX
Functional Paradigm.pptx
FurretMaster
 
ODP
Functional programming
S M Asaduzzaman
 
PDF
Teach Yourself some Functional Programming with Scala
Damian Jureczko
 
PPTX
Intro f# functional_programming
Mauro Ghiani
 
PPTX
Why functional programming in C# & F#
Riccardo Terrell
 
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
PDF
7 Habits For a More Functional Swift
Jason Larsen
 
PDF
Functional Go
Geison Goes
 
PDF
Introduction to functional programming
Konrad Szydlo
 
PDF
379008-rc217-functionalprogramming
Luis Atencio
 
PDF
Functional Programming for Busy Object Oriented Programmers
Diego Freniche Brito
 
PPT
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
Innovecs
 
PPT
Introductory func prog
Oleksandr Khomenko
 
PPTX
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
PDF
Functional Programming Principles & Patterns
zupzup.org
 
Functional Swift
Geison Goes
 
Functional go
Geison Goes
 
Functional programming
Prashant Kalkar
 
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Tech in Asia ID
 
Functional programming
PiumiPerera7
 
Functional Paradigm.pptx
FurretMaster
 
Functional programming
S M Asaduzzaman
 
Teach Yourself some Functional Programming with Scala
Damian Jureczko
 
Intro f# functional_programming
Mauro Ghiani
 
Why functional programming in C# & F#
Riccardo Terrell
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
7 Habits For a More Functional Swift
Jason Larsen
 
Functional Go
Geison Goes
 
Introduction to functional programming
Konrad Szydlo
 
379008-rc217-functionalprogramming
Luis Atencio
 
Functional Programming for Busy Object Oriented Programmers
Diego Freniche Brito
 
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
Innovecs
 
Introductory func prog
Oleksandr Khomenko
 
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
Functional Programming Principles & Patterns
zupzup.org
 
Ad

Recently uploaded (20)

PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Ad

Functional Programming in Swift

  • 3. Functional Programming • A programming paradigm that treats computations as the evaluation of mathematical functions and avoids changing-state and mutable data. It is declarative programming paradigm, which means programming is done with expressions.
  • 5. Why? • Maintainable code. • Easier and complete testing. • Easier parallel and concurrent programming.
  • 7. Aspects • Immutable Data(Values) • Pure functions • Higher Order Functions • Functions as arguments and as return values.
  • 9. Immutable Values benefits • Values can’t change unexpectedly • Can be shared without worrying • Thread safe - Values play well with observers
  • 10. Example let firstName = "Saugat" let lastName = “Gautam" var middleName = "Kumar" let numbers = Array(1…10) firstName.append("D") // Compiler Error middleName.append("atunga") // All good //————————————————————————————————————— struct PersonalInfo { var firstName: String var lastName: String var middleName: String } let emp1 = PersonalInfo(firstName: "Saugat", lastName: "Gautam", middleName:
  • 11. More Benefits • The Value of Values - Rich Hickey http://www.infoq.com/presentations/Value-Values • Controlling Complexity in Swift or Making Friends with Value- Andy Matuschak http://realm.io/news/andy-matuschak-controlling- complexity/ http://www.objc.io/issue-16/swift-classes-vs- structs.html
  • 13. Why Functional Swift? • Generics. • Functional Programming Concept. • Nice consistent syntax for functions and closures.
  • 14. Can’t Objective-C do it? It can but we probably don’t want to.
  • 15. Swift Value Types vs Reference Types • Value Types: • Numbers, strings, arrays, dictionaries, enums, tuples, and structs. • Reference Types: • Classes
  • 16. Value Types • Single owner • Copied on assign • Example var a = 10 var b = a // a = 10, b = 5 b = 5 // a = 10, b = 5
  • 17. Reference Types • Multiple owners • Shared on assignment var a = UIView() var b = a // a.alpha = 1, b.alpha = 1 b.alpha = 0 // a.alpha = 0; b.alpha = 0
  • 19. Pure Functions • Same input always results in same output (Referential Transparency) • Evaluation of result does not cause any side effect • eg. No change in database or to I/O devices • Result value need not depend on all arguments but it must depend on nothing other than the arguments
  • 20. Pure Function func factorial(number: Int) -> Int { if (number == 1) { return 1 } else { return number * factorial(number - 1) } } factorial(6)
  • 21. Not Pure!! func naturalSum(numbers:[Int]) -> Int{ var total = 0 for num in numbers { total = total + num } return total } func sayHelloTo(name: String) { println("Hello (name)") }
  • 22. Higher Order Functions • Function as parameter • Function as return value
  • 23. Function as Parameter func average(x: Int, y: Int, f:(Int -> Int)) -> Int{ return (f(x) + f(y))/2 } let square = {(x: Int) -> Int in return x * x} average(2, 3, f:square)
  • 24. Function as Parameter func average(x: Int, y: Int, f:(Int -> Int) = {x in return x}) -> Int{ return (f(x) + f(y))/2 } let square = {(x: Int) -> Int in return x * x} average(2, 3, f:square)
  • 25. Function as Return Value func add(a: Int) -> (Int -> Int) { return {b in a + b} } // partial application let addThree = add(3) // let x3 = xs2.map(addThree)
  • 26. Functional Tool Box • Map • Reduce • Filter
  • 27. Functional Tool Box • Laziness • Flatmap • Currying
  • 28. Functional Toolbox • Functors, Applicative Functors • Monads References: Functional Programming in Swift http://www.objc.io/books/ Why is a Monad like a writing desk? http:www.infoq.com/presentations/Why-is-a-Monad-Like-a-Writing-Desk
  • 29. Map
  • 30. Map • Transforming data • Normally number of output collections is equal to input
  • 31. Map Example for Array<T> • Produce a list of the squares of the first 10 positive numbers.
  • 32. Imperative Approach let firstTenPositiveIntegers = [Int](1…10) var firstTenSquares = [Int]() for number in firstTenPositiveIntegers { firstTenSquares.append(number*number) } println(firstTenSquares) // [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • 33. Problems with Imperative Approach • Mixes looping with transformation • Not declarative • What if .. firstTenSquares.append(23) // [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 23]
  • 34. Functional Approach let firstTenPositiveIntegers = [Int](1...10) let firstTenSquares = firstTenPositiveIntegers.map {$0 * $0} println(firstTenSquares) // [1, 4, … , 100]
  • 35. Benefits of Functional Approach • Declarative. States what we want to do. Not how we want to do it. • Separates the mechanism(step through and select each item) from the transformation logic
  • 37. Filter • Transforms the initial collection to some final collection applying some condition • Final size may not be equal to initial size
  • 38. Filter Example for Array<T> • Requirement: • Produce a list of even squares of the first 10 positive numbers.
  • 39. Imperative Approach: let firstTenPositiveIntegers = [Int](1...10) var evenSquaresInFirstTen = [Int]() for number in firstTenPositiveIntegers { let square = number * number if (square % 2) == 0 { evenSquaresInFirstTen.append(square) } } println(evenSquaresInFirstTen) // [4, 16, 36, 64, 100]
  • 40. Functional Approach let firstTenPositiveIntegers = [Int](1…10) let evenSquaresInFirstTen = firstTenPositiveIntegers.map({$0 * $0}).filter({($0%2) == 0}) println(evenSquaresInFirstTen) // [4, 16, 36, 64, 100]
  • 42. Reduce • Transform a collection to a single value
  • 43. Reduce Example for Array<T> • Sum of the even squares in the first 10 positive numbers?
  • 44. Imperative Approach let firstTenPositiveIntegers = [Int](1…10) var sumOfEvenSquaresInFirstTen = 0 for number in firstTenPositiveIntegers { let square = number * number if (square % 2) == 0 { sumOfEvenSquaresInFirstTen += square } } println(sumOfEvenSquaresInFirstTen) // 220
  • 45. Functional Approach let firstTenPositiveIntegers = [Int](1…10) let sumOfEvenSquaresInFirstTen = firstTenPositiveIntegers.map({$0 * .filter({$0 %2 == 0}) .reduce(0, combine: {$0 + $1}) println(sumOfEvenSquaresInFirstTen) // 220
  • 46. Functional Approach let firstTenPositiveIntegers = [Int](1...10) let sumOfEvenSquaresInFirstTen = firstTenPositiveIntegers.map({$0 * $0}).reduce println(sumOfEvenSquaresInFirstTen) // 220
  • 48. Currying • Translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions each with a single argument(partial application)
  • 49. Currying example • Say we have a method that takes two inputs and add them func add(a: Int, b: Int) -> Int { return a + b } let sum = add(2, 3) // sum = 5
  • 50. Currying Example • Now we want to add 2 to a list of numbers • Drawbacks • creating a closure just for passing add with default value • difficulty arises in case multiple default values are needed let xs = 1...100 let x = xs.map { add($0, 2) } // x = [3, 4, 5, 6, etc]
  • 51. Currying Example • Use currying • Drawbacks • need of wrapper functions for each integer func addTwo(a: Int) -> Int { return add(a, 2) } let xs = 1...100 let x = xs2.map(addTwo)
  • 52. Currying Example • Use another currying method func add(a: Int) -> (Int -> Int) { return {b in a + b} } // Using method as a whole; add(2)(3) // partial application let addThree = add(3) // let x3 = xs2.map(addThree)
  • 54. Application Architecture • Cocoa and Cocoa Touch are inherently imperative • Don’t fight the framework
  • 55. Infusing functional thinking into our Cocoa applications? • Functional Core / Imperative Shell approach • References • https://www.destroyallsoftware.com/screencasts/catalog/functional-core- imperative-shell • https://speakerdeck.com/jspahrsummers/enemy-of-the-state
  • 56. Functional Core / Imperative Shell • Place application’s core data and logic in immutable constructs (value types). • Push state out to the ‘edges’. Represent state as objects with mutable references to immutable core constructs.
  • 57. Benefits • Allows isolated testing • Leads to an imperative shell with few conditionals, making reasoning about the program’s state over time much easier
  • 58. UI - Can We Functionalize the Imperative Shell? • Not quite there. Some references worth mentioning. • Reactive Cocoa https://github.com/ReactiveCocoa/ReactiveCocoa • React Native http://www.reactnative.com • Functional View Controllers http://chris.eidhof.nl/posts/functinal-view-controllers.html