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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
d_date
October 09, 2018
Programming
5k
3
Share
How to use Dictionary.compactMapValues
potatotips #55
d_date
October 09, 2018
More Decks by d_date
See All by d_date
TCA Practice in 5 min
d_date
2
1.8k
waiwai-swiftpm-part2
d_date
3
580
わいわいSwift PM part 1
d_date
2
460
What's new in Firebase 2021
d_date
2
1.6k
CI/CDをミニマルに構築する
d_date
1
620
Swift Package centered project - Build and Practice
d_date
20
16k
How to write Great Proposal
d_date
4
2k
Thinking about Architecture for SwiftUI
d_date
8
2.5k
Integrate your app to modern world in Niigata
d_date
0
730
Other Decks in Programming
See All in Programming
The Less-Told Story of Socket Timeouts
coe401_
3
900
t *testing.T は どこからやってくるの?
otakakot
1
870
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
290
決定論 vs 確率論:Gemini 3 FlashとTF-IDFを組み合わせた「法規判定エンジン」の構築
shukob
0
140
From Formal Specification to Property Based Test
ohbarye
0
640
クラウドネイティブなエンジニアに向ける Raycastの魅力と実際の活用事例
nealle
2
230
AWSコミュニティ活動は顧客のクラウド推進に効くのか / Do AWS community activities help customers adopt the cloud?
seike460
PRO
0
160
なぜあなたのコードには「コシ」がないのか?〜AI時代に問う、最後まで美味しい設計と戦略〜 #phpconkagawa / phpconkagawa2026
shogogg
0
100
PHPer、Cloudflare に引っ越す
suguruooki
1
130
AIと共に生きる技術選定 2026
sgash708
0
120
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
360
JOAI2026 1st solution - heron0519 -
heron0519
0
170
Featured
See All Featured
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
180
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2k
Crafting Experiences
bethany
1
130
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
220
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
210
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
Chasing Engaging Ingredients in Design
codingconduct
0
180
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
550
SEO for Brand Visibility & Recognition
aleyda
0
4.5k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
500
New Earth Scene 8
popppiees
3
2.2k
Google's AI Overviews - The New Search
badams
0
990
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