Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
Google Antigravity and Vibe Coding: Agentic Development Guide
mickey_kubo
2
120
Microservices rules: What good looks like
cer
PRO
0
320
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
130
Stay Hacker 〜九州で生まれ、Perlに出会い、コミュニティで育つ〜
pyama86
2
3.4k
Atomics APIを知る / Understanding Atomics API
ssssota
1
240
チーム開発の “地ならし"
konifar
8
6.8k
配送計画の均等化機能を提供する取り組みについて(⽩⾦鉱業 Meetup Vol.21@六本⽊(数理最適化編))
izu_nori
0
120
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
200
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
230
Herb to ReActionView: A New Foundation for the View Layer @ San Francisco Ruby Conference 2025
marcoroth
0
230
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 1
philipschwarz
PRO
0
120
2025 컴포즈 마법사
jisungbin
1
170
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
Bash Introduction
62gerente
615
210k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Done Done
chrislema
186
16k
Code Reviewing Like a Champion
maltzj
527
40k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
RailsConf 2023
tenderlove
30
1.3k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Building Adaptive Systems
keathley
44
2.8k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
11
960
4 Signs Your Business is Dying
shpigford
186
22k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.2k
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