Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Functional Swift
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Ulrik Flænø Damm
July 23, 2014
Programming
100
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Functional Swift
Presented at NSCoder Night Copenhagen
Ulrik Flænø Damm
July 23, 2014
Other Decks in Programming
See All in Programming
golang-bob.pdf
iwatsukayura
0
130
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
0
480
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
The Rails Doctrine Decade
koic
2
590
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
5.1k
Domain-Driven Transformation
hschwentner
2
2.3k
Next.js: The final boss of Web Performance
guaca
0
260
パスワード再設定メールのURLについて知っておこう
uyuki234
0
160
気持ちぃ〜角丸 Squircle
ryu1013
0
310
Vue Fes Japan の “作る側”に関わってみて
naokihaba
0
160
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
28
32k
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
410
Faster Mobile Websites
deanohume
310
32k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.4k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
The Curious Case for Waylosing
cassininazir
1
570
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
570
Navigating Weather and Climate Data
rabernat
0
560
Transcript
Hi! I’m ULRIK @ULRIKDAMM
FUNCTIONAL Swift
FUNCTIONS AS FIRST Class citizens
Int -> Int func sqr(i : Int) -> Int {
return i * i } { i in i * i } ~
None
None
PROCEDURAL
1 2 ff 2e ad load value to a load
value to b > add a and b and store in c print value c
FUNCTIONAL
None
MAP & REDUCE
MAP
REDUCE
MAP & REDUCE 1 2 3 1 4 9 0
14 (((0 + 1) + 4) + 9) = 14 sqr sqr sqr + + + =
IN SWIFT
IN SWIFT func map<U>(transform: (T) -> U) -> [U] func
reduce<U>(initial: U, combine: (U, T) -> U) -> U
IN SWIFT values.map(transform) values.reduce(initial, transform)
IN SWIFT [1, 2, 3].map { value in value +
1 } [1, 2, 3].reduce(0) { result, value in result + value }
CLOSURE SYNTAX
CLOSURE SYNTAX let closure : (Int, Int) -> Int =
{ value1, value2 -> Int in return value1 + value2 } [1, 2, 3].reduce(0, closure)
CLOSURE SYNTAX [1, 2, 3].reduce(0, { value1, value2 -> Int
in return value1 + value2 })
CLOSURE SYNTAX [1, 2, 3].reduce(0, { value1, value2 -> Int
in return value1 + value2 })
CLOSURE SYNTAX [1, 2, 3].reduce(0) { value1, value2 -> Int
in return value1 + value2 }
CLOSURE SYNTAX [1, 2, 3].reduce(0) { value1, value2 -> Int
in return value1 + value2 }
CLOSURE SYNTAX [1, 2, 3].reduce(0) { value1, value2 in return
value1 + value2 }
CLOSURE SYNTAX [1, 2, 3].reduce(0) { value1, value2 in return
value1 + value2 }
CLOSURE SYNTAX [1, 2, 3].reduce(0) { value1, value2 in value1
+ value2 }
CLOSURE SYNTAX [1, 2, 3].reduce(0) { value1, value2 in value1
+ value2 } // most common
CLOSURE SYNTAX [1, 2, 3].reduce(0) { value1, value2 in value1
+ value2 }
CLOSURE SYNTAX [1, 2, 3].reduce(0) { $0 + $1 }
CLOSURE SYNTAX [1, 2, 3].reduce(0) { $0 + $1 }
// short form
CLOSURE SYNTAX [1, 2, 3].reduce(0, +)
EXAMPLES
AVERAGE let values = [5, 8, 17] // procedural var
sum = 0 for value in values { sum += value } let average = sum / values.count // functional values.reduce(0, +) / values.count
COUNT OCCURENCES func count_occurences(vals : [Int], of val : Int)
-> Int { return vals.reduce(0) { count, this in return count + (val == this ? 1 : 0) } }
CONTAINS func contains(vals : [Int], find val : Int) ->
Bool { return vals.reduce(false) { found, this in if found { return true } else { return val == this } } }
WORDBASE func createPositions(positions : (Int, Int)...) -> [Position] { return
positions.map { position in return Position(x: position.0, y: position.1) } } func wordFromTiles(tiles : [Position]) -> String { return tiles.reduce("") { string, position in return string + self.characterAtPosition(position) } }
TYPE INFERENCE let ns = [1, 2, 3] ns.reduce(0, +)
// 6 let ss = ["1", "2", "3"] ss.reduce("", +) // “123" let moves = [(0,1) => (0,2), (5, 7) => (7, 7)] moves.reduce(ChessBoard(), +)
FIN