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
How to use Dictionary.compactMapValues
Search
d_date
October 09, 2018
Programming
3
5k
How to use Dictionary.compactMapValues
potatotips #55
d_date
October 09, 2018
Tweet
Share
More Decks by d_date
See All by d_date
TCA Practice in 5 min
d_date
2
1.5k
waiwai-swiftpm-part2
d_date
3
510
わいわいSwift PM part 1
d_date
2
410
What's new in Firebase 2021
d_date
2
1.5k
CI/CDをミニマルに構築する
d_date
1
580
Swift Package centered project - Build and Practice
d_date
20
14k
How to write Great Proposal
d_date
4
1.7k
Thinking about Architecture for SwiftUI
d_date
8
2.4k
Integrate your app to modern world in Niigata
d_date
0
680
Other Decks in Programming
See All in Programming
Python型ヒント完全ガイド 初心者でも分かる、現代的で実践的な使い方
mickey_kubo
1
120
Railsアプリケーションと パフォーマンスチューニング ー 秒間5万リクエストの モバイルオーダーシステムを支える事例 ー Rubyセミナー 大阪
falcon8823
5
1.1k
AI時代のソフトウェア開発を考える(2025/07版) / Agentic Software Engineering Findy 2025-07 Edition
twada
PRO
87
29k
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
170
A full stack side project webapp all in Kotlin (KotlinConf 2025)
dankim
0
120
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
250
「Cursor/Devin全社導入の理想と現実」のその後
saitoryc
0
820
初学者でも今すぐできる、Claude Codeの生産性を10倍上げるTips
s4yuba
16
11k
NPOでのDevinの活用
codeforeveryone
0
840
RailsGirls IZUMO スポンサーLT
16bitidol
0
190
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
360
Startups on Rails in Past, Present and Future–Irina Nazarova, RailsConf 2025
irinanazarova
0
110
Featured
See All Featured
Site-Speed That Sticks
csswizardry
10
690
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Music & Morning Musume
bryan
46
6.6k
Practical Orchestrator
shlominoach
189
11k
4 Signs Your Business is Dying
shpigford
184
22k
RailsConf 2023
tenderlove
30
1.1k
Agile that works and the tools we love
rasmusluckow
329
21k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
Why Our Code Smells
bkeepers
PRO
336
57k
How GitHub (no longer) Works
holman
314
140k
Producing Creativity
orderedlist
PRO
346
40k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.5k
Transcript
How to use Dictionary.compactMapValues potatotips #55 Daiki Matsudate / @d_date
Daiki Matsudate @d_date
https://peaks.cc/iOS_architecture
IUUQTZBLJSJOHPCPPUIQNJUFNT
Dictionary .compactMapValues
extension Dictionary { public func compactMapValues<T>(_ transform: (Value) throws ->
T?) rethrows -> [Key: T] { return try self.reduce(into: [Key: T](), { (result, x) in if let value = try transform(x.value) { result[x.key] = value } }) } } Available in Swift 5
Use Cases
let d1 = ["a": "1", "b": "2", "c": nil] d1.filter({
$0.value != nil}).mapValues({ $0! }) // ["b": "2", "a": "1"]
["a": "1", "b": nil, "c": “3”].compactMapValues({ $0 }) // ["a"
: 1, "c" : 3]
var object = { 'a': 1, 'b': null, 'c': 3
}; _.omitBy(object, _.isNil); // => { 'a': 1, 'c': 3 } DG@PNJU#Z
let d = ["a": "1", "b": "2", "c": "three"] d.mapValues(Int.init).filter({
$0.value != nil}).mapValues({ $0! })
extension Dictionary { public func compactMapValues<T>(_ transform: (Value) throws ->
T?) rethrows -> [Key: T] { return try self.reduce(into: [Key: T](), { (result, x) in if let value = try transform(x.value) { result[x.key] = value } }) } }
let d = ["a": "1", "b": "2", "c": "three"] d.compactMapValues(Int.init)
// ["b": 2, "a": 1]
None
• Daiki Matsudate / @d_date Available in Swift 5
Make our Swift better 09/05/2018 try! Swift NYC Daiki Matsudate
/ @d_date
None
• compactMapValues is available in Swift 5 • compactMapValues can
transform and unwrap dictionary • Let’s try to contribute to Swift lang Recap
None