Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Functional Swift
Search
Ulrik Flænø Damm
July 23, 2014
Programming
0
97
Functional Swift
Presented at NSCoder Night Copenhagen
Ulrik Flænø Damm
July 23, 2014
Tweet
Share
Other Decks in Programming
See All in Programming
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
610
Patterns of Patterns
denyspoltorak
0
1.4k
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
130
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
130
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
450
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
270
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
3.9k
登壇資料を作る時に意識していること #登壇資料_findy
konifar
4
1.1k
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
230
AgentCoreとHuman in the Loop
har1101
5
230
MUSUBIXとは
nahisaho
0
130
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
120
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
910
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
54
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
330
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
60
42k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
180
Statistics for Hackers
jakevdp
799
230k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
98
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