SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
Currying in Swift 
A tale of state encapsulation 
Maxim Zaks @iceX33
Every presentation 
needs a theme
func amountWithVat(vat:Float, amount: Float) -> Float { 
return roundf((1 + vat) * amount) 
} 
let amount1 = amountWithVat(0.19, 100) 
let amount2 = amountWithVat(0.19, 200) 
let amount3 = amountWithVat(0.2, 300)
func amountWithVat(vat:Float)(amount: Float) -> Float { 
return roundf((1 + vat) * amount) 
} 
let addGermanVatTo = amountWithVat(0.19) 
let addUKVatTo = amountWithVat(0.2) 
let amount1 = addGermanVatTo(amount: 100) 
let amount2 = addGermanVatTo(amount: 200) 
let amount3 = addUKVatTo(amount: 300)
func amountWithVat( vat : Float, 
roundFunction : (Float)->Float = roundf) 
(amount: Float) -> Float { 
return roundFunction((1 + vat) * amount) 
} 
let addGermanVatTo = amountWithVat(0.19){ 
roundf($0 * 100) / 100 
} 
let addUKVatTo = amountWithVat(0.2) 
let amount1 = addGermanVatTo(amount: 101) 
let amount2 = addGermanVatTo(amount: 200) 
let amount3 = addUKVatTo(amount: 300)
Should we consider 
currying as a 
revelation?
func amountWithVat(vat:Float)(amount: Float) -> Float { 
return roundf((1 + vat) * amount) 
} 
let addGermanVatTo = amountWithVat(0.19) 
let addUKVatTo = amountWithVat(0.2) 
let amount1 = addGermanVatTo(amount: 100) 
let amount2 = addGermanVatTo(amount: 200) 
let amount3 = addUKVatTo(amount: 300)
func amountWithVat(vat:Float) -> Float -> Float { 
return { (amount:Float) -> Float in 
return roundf((1 + vat) * amount) 
} 
} 
let addGermanVatTo = amountWithVat(0.19) 
let addUKVatTo = amountWithVat(0.2) 
let amount1 = addGermanVatTo(100) 
let amount2 = addGermanVatTo(200) 
let amount3 = addUKVatTo(300)
typedef float (^FloatToFloat)(float); 
FloatToFloat (^amountWithVat)(float) = 
^FloatToFloat (float vat) { 
return ^(float amount){ 
return roundf((1 + vat) * amount); 
}; 
}; 
FloatToFloat addGermanVatTo = amountWithVat(.19); 
FloatToFloat addUKVatTo = amountWithVat(0.2); 
float amount1 = addGermanVatTo(100); 
float amount2 = addGermanVatTo(200); 
float amount3 = addUKVatTo(300);
Interesting application of function currying found out 
by Ole Begemann 
(http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/)
class BankAccount { 
var balance: Double = 0.0 
func deposit(amount: Double) { 
balance += amount 
} 
} 
let account = BankAccount() 
account.deposit(100) // balance is now 100 
let depositor = BankAccount.deposit 
depositor(account)(100) // balance is now 200
So every method is implemented internaly through 
function currying
One More Thing
Who knows the name 
of the show I used for 
the theme
Thank you 
Maxim Zaks @iceX33

Weitere ähnliche Inhalte

Was ist angesagt?

Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderSamsil Arefin
 
Tensorflowで五目並べの思考アルゴリズムを作る
Tensorflowで五目並べの思考アルゴリズムを作るTensorflowで五目並べの思考アルゴリズムを作る
Tensorflowで五目並べの思考アルゴリズムを作るjugemjugemjugem
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions Dr. Volkan OBAN
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using ScalaSiarhiej Siemianchuk
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods kinan keshkeh
 
The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018Lara Schenck
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentationMartin McBride
 
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...Lara Schenck
 
Program membalik kata
Program membalik kataProgram membalik kata
Program membalik katahaqiemisme
 

Was ist angesagt? (18)

Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
Rates of change
Rates of changeRates of change
Rates of change
 
Rates of change (2)
Rates of change (2)Rates of change (2)
Rates of change (2)
 
Tensorflowで五目並べの思考アルゴリズムを作る
Tensorflowで五目並べの思考アルゴリズムを作るTensorflowで五目並べの思考アルゴリズムを作る
Tensorflowで五目並べの思考アルゴリズムを作る
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
 
Sol10
Sol10Sol10
Sol10
 
Concurrent Application Development using Scala
Concurrent Application Development using ScalaConcurrent Application Development using Scala
Concurrent Application Development using Scala
 
Wcbpijwbpij new
Wcbpijwbpij newWcbpijwbpij new
Wcbpijwbpij new
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018The Algorithms of CSS @ CSSConf EU 2018
The Algorithms of CSS @ CSSConf EU 2018
 
Print input-presentation
Print input-presentationPrint input-presentation
Print input-presentation
 
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
Bridging the Design to Development Gap with CSS Algorithms (Algorithms of CSS...
 
Statistics.cpp
Statistics.cppStatistics.cpp
Statistics.cpp
 
dplyr
dplyrdplyr
dplyr
 
ProgrammingwithGOLang
ProgrammingwithGOLangProgrammingwithGOLang
ProgrammingwithGOLang
 
Class & sub class
Class & sub classClass & sub class
Class & sub class
 
Program membalik kata
Program membalik kataProgram membalik kata
Program membalik kata
 

Andere mochten auch

Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Maxim Zaks
 
Latest enzyme (2)
Latest enzyme (2)Latest enzyme (2)
Latest enzyme (2)Umi Biee
 
Chemistry of Essential Fatty Acids
Chemistry of Essential Fatty AcidsChemistry of Essential Fatty Acids
Chemistry of Essential Fatty Acidsrohini sane
 
Glucose tolerance test
Glucose tolerance testGlucose tolerance test
Glucose tolerance testAshok Katta
 
Dr ravi lipid profile
Dr ravi lipid profileDr ravi lipid profile
Dr ravi lipid profileRavi Jain
 
Effect of Temperature and pH on enzyme activity
Effect of Temperature and pH on enzyme activityEffect of Temperature and pH on enzyme activity
Effect of Temperature and pH on enzyme activityclairebloom
 

Andere mochten auch (8)

Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012
 
Latest enzyme (2)
Latest enzyme (2)Latest enzyme (2)
Latest enzyme (2)
 
Chemistry of Essential Fatty Acids
Chemistry of Essential Fatty AcidsChemistry of Essential Fatty Acids
Chemistry of Essential Fatty Acids
 
Lipids methodology
Lipids methodologyLipids methodology
Lipids methodology
 
Glucose tolerance test
Glucose tolerance testGlucose tolerance test
Glucose tolerance test
 
Dr ravi lipid profile
Dr ravi lipid profileDr ravi lipid profile
Dr ravi lipid profile
 
Essential fatty acids
Essential fatty acidsEssential fatty acids
Essential fatty acids
 
Effect of Temperature and pH on enzyme activity
Effect of Temperature and pH on enzyme activityEffect of Temperature and pH on enzyme activity
Effect of Temperature and pH on enzyme activity
 

Mehr von Maxim Zaks

Entity Component System - a different approach to game and app development
Entity Component System - a different approach to game and app developmentEntity Component System - a different approach to game and app development
Entity Component System - a different approach to game and app developmentMaxim Zaks
 
Nitty Gritty of Data Serialisation
Nitty Gritty of Data SerialisationNitty Gritty of Data Serialisation
Nitty Gritty of Data SerialisationMaxim Zaks
 
Wind of change
Wind of changeWind of change
Wind of changeMaxim Zaks
 
Data model mal anders
Data model mal andersData model mal anders
Data model mal andersMaxim Zaks
 
Talk Binary to Me
Talk Binary to MeTalk Binary to Me
Talk Binary to MeMaxim Zaks
 
Entity Component System - for App developers
Entity Component System - for App developersEntity Component System - for App developers
Entity Component System - for App developersMaxim Zaks
 
Beyond JSON - An Introduction to FlatBuffers
Beyond JSON - An Introduction to FlatBuffersBeyond JSON - An Introduction to FlatBuffers
Beyond JSON - An Introduction to FlatBuffersMaxim Zaks
 
Beyond JSON @ Mobile.Warsaw
Beyond JSON @ Mobile.WarsawBeyond JSON @ Mobile.Warsaw
Beyond JSON @ Mobile.WarsawMaxim Zaks
 
Beyond JSON @ dot swift 2016
Beyond JSON @ dot swift 2016Beyond JSON @ dot swift 2016
Beyond JSON @ dot swift 2016Maxim Zaks
 
Beyond JSON with FlatBuffers
Beyond JSON with FlatBuffersBeyond JSON with FlatBuffers
Beyond JSON with FlatBuffersMaxim Zaks
 
Basics of Computer Science
Basics of Computer ScienceBasics of Computer Science
Basics of Computer ScienceMaxim Zaks
 
Entity system architecture with Unity @Unite Europe 2015
Entity system architecture with Unity @Unite Europe 2015 Entity system architecture with Unity @Unite Europe 2015
Entity system architecture with Unity @Unite Europe 2015 Maxim Zaks
 
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlinUIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlinMaxim Zaks
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit partsMaxim Zaks
 
Promise of an API
Promise of an APIPromise of an API
Promise of an APIMaxim Zaks
 
96% macoun 2013
96% macoun 201396% macoun 2013
96% macoun 2013Maxim Zaks
 
Diagnose of Agile @ Wooga 04.2013
Diagnose of Agile @ Wooga 04.2013Diagnose of Agile @ Wooga 04.2013
Diagnose of Agile @ Wooga 04.2013Maxim Zaks
 
Start playing @ mobile.cologne 2013
Start playing @ mobile.cologne 2013Start playing @ mobile.cologne 2013
Start playing @ mobile.cologne 2013Maxim Zaks
 
Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013Maxim Zaks
 
Don’t do Agile, be Agile @NSConf 2013
Don’t do Agile, be Agile @NSConf 2013Don’t do Agile, be Agile @NSConf 2013
Don’t do Agile, be Agile @NSConf 2013Maxim Zaks
 

Mehr von Maxim Zaks (20)

Entity Component System - a different approach to game and app development
Entity Component System - a different approach to game and app developmentEntity Component System - a different approach to game and app development
Entity Component System - a different approach to game and app development
 
Nitty Gritty of Data Serialisation
Nitty Gritty of Data SerialisationNitty Gritty of Data Serialisation
Nitty Gritty of Data Serialisation
 
Wind of change
Wind of changeWind of change
Wind of change
 
Data model mal anders
Data model mal andersData model mal anders
Data model mal anders
 
Talk Binary to Me
Talk Binary to MeTalk Binary to Me
Talk Binary to Me
 
Entity Component System - for App developers
Entity Component System - for App developersEntity Component System - for App developers
Entity Component System - for App developers
 
Beyond JSON - An Introduction to FlatBuffers
Beyond JSON - An Introduction to FlatBuffersBeyond JSON - An Introduction to FlatBuffers
Beyond JSON - An Introduction to FlatBuffers
 
Beyond JSON @ Mobile.Warsaw
Beyond JSON @ Mobile.WarsawBeyond JSON @ Mobile.Warsaw
Beyond JSON @ Mobile.Warsaw
 
Beyond JSON @ dot swift 2016
Beyond JSON @ dot swift 2016Beyond JSON @ dot swift 2016
Beyond JSON @ dot swift 2016
 
Beyond JSON with FlatBuffers
Beyond JSON with FlatBuffersBeyond JSON with FlatBuffers
Beyond JSON with FlatBuffers
 
Basics of Computer Science
Basics of Computer ScienceBasics of Computer Science
Basics of Computer Science
 
Entity system architecture with Unity @Unite Europe 2015
Entity system architecture with Unity @Unite Europe 2015 Entity system architecture with Unity @Unite Europe 2015
Entity system architecture with Unity @Unite Europe 2015
 
UIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlinUIKonf App & Data Driven Design @swift.berlin
UIKonf App & Data Driven Design @swift.berlin
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit parts
 
Promise of an API
Promise of an APIPromise of an API
Promise of an API
 
96% macoun 2013
96% macoun 201396% macoun 2013
96% macoun 2013
 
Diagnose of Agile @ Wooga 04.2013
Diagnose of Agile @ Wooga 04.2013Diagnose of Agile @ Wooga 04.2013
Diagnose of Agile @ Wooga 04.2013
 
Start playing @ mobile.cologne 2013
Start playing @ mobile.cologne 2013Start playing @ mobile.cologne 2013
Start playing @ mobile.cologne 2013
 
Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013Under Cocos2D Tree @mdvecon 2013
Under Cocos2D Tree @mdvecon 2013
 
Don’t do Agile, be Agile @NSConf 2013
Don’t do Agile, be Agile @NSConf 2013Don’t do Agile, be Agile @NSConf 2013
Don’t do Agile, be Agile @NSConf 2013
 

Kürzlich hochgeladen

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Kürzlich hochgeladen (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

Currying in Swift

  • 1. Currying in Swift A tale of state encapsulation Maxim Zaks @iceX33
  • 3. func amountWithVat(vat:Float, amount: Float) -> Float { return roundf((1 + vat) * amount) } let amount1 = amountWithVat(0.19, 100) let amount2 = amountWithVat(0.19, 200) let amount3 = amountWithVat(0.2, 300)
  • 4. func amountWithVat(vat:Float)(amount: Float) -> Float { return roundf((1 + vat) * amount) } let addGermanVatTo = amountWithVat(0.19) let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(amount: 100) let amount2 = addGermanVatTo(amount: 200) let amount3 = addUKVatTo(amount: 300)
  • 5. func amountWithVat( vat : Float, roundFunction : (Float)->Float = roundf) (amount: Float) -> Float { return roundFunction((1 + vat) * amount) } let addGermanVatTo = amountWithVat(0.19){ roundf($0 * 100) / 100 } let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(amount: 101) let amount2 = addGermanVatTo(amount: 200) let amount3 = addUKVatTo(amount: 300)
  • 6. Should we consider currying as a revelation?
  • 7. func amountWithVat(vat:Float)(amount: Float) -> Float { return roundf((1 + vat) * amount) } let addGermanVatTo = amountWithVat(0.19) let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(amount: 100) let amount2 = addGermanVatTo(amount: 200) let amount3 = addUKVatTo(amount: 300)
  • 8. func amountWithVat(vat:Float) -> Float -> Float { return { (amount:Float) -> Float in return roundf((1 + vat) * amount) } } let addGermanVatTo = amountWithVat(0.19) let addUKVatTo = amountWithVat(0.2) let amount1 = addGermanVatTo(100) let amount2 = addGermanVatTo(200) let amount3 = addUKVatTo(300)
  • 9. typedef float (^FloatToFloat)(float); FloatToFloat (^amountWithVat)(float) = ^FloatToFloat (float vat) { return ^(float amount){ return roundf((1 + vat) * amount); }; }; FloatToFloat addGermanVatTo = amountWithVat(.19); FloatToFloat addUKVatTo = amountWithVat(0.2); float amount1 = addGermanVatTo(100); float amount2 = addGermanVatTo(200); float amount3 = addUKVatTo(300);
  • 10. Interesting application of function currying found out by Ole Begemann (http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/)
  • 11. class BankAccount { var balance: Double = 0.0 func deposit(amount: Double) { balance += amount } } let account = BankAccount() account.deposit(100) // balance is now 100 let depositor = BankAccount.deposit depositor(account)(100) // balance is now 200
  • 12. So every method is implemented internaly through function currying
  • 14. Who knows the name of the show I used for the theme
  • 15.
  • 16. Thank you Maxim Zaks @iceX33