Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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.7k
waiwai-swiftpm-part2
d_date
3
550
わいわいSwift PM part 1
d_date
2
430
What's new in Firebase 2021
d_date
2
1.6k
CI/CDをミニマルに構築する
d_date
1
600
Swift Package centered project - Build and Practice
d_date
20
15k
How to write Great Proposal
d_date
4
1.8k
Thinking about Architecture for SwiftUI
d_date
8
2.5k
Integrate your app to modern world in Niigata
d_date
0
700
Other Decks in Programming
See All in Programming
チームをチームにするEM
hitode909
0
300
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
600
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
120
FluorTracer / RayTracingCamp11
kugimasa
0
220
Cap'n Webについて
yusukebe
0
130
20251127_ぼっちのための懇親会対策会議
kokamoto01_metaps
2
420
React Native New Architecture 移行実践報告
taminif
1
150
Integrating WordPress and Symfony
alexandresalome
0
150
안드로이드 9년차 개발자, 프론트엔드 주니어로 커리어 리셋하기
maryang
1
110
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
350
JETLS.jl ─ A New Language Server for Julia
abap34
1
340
Building AI Agents with TypeScript #TSKaigiHokuriku
izumin5210
6
1.3k
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
340
57k
GitHub's CSS Performance
jonrohan
1032
470k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Done Done
chrislema
186
16k
Speed Design
sergeychernyshev
33
1.4k
How to train your dragon (web standard)
notwaldorf
97
6.4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
700
A Tale of Four Properties
chriscoyier
162
23k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Designing for humans not robots
tammielis
254
26k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
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