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.3k
waiwai-swiftpm-part2
d_date
3
480
わいわいSwift PM part 1
d_date
2
380
What's new in Firebase 2021
d_date
2
1.4k
CI/CDをミニマルに構築する
d_date
1
550
Swift Package centered project - Build and Practice
d_date
20
13k
How to write Great Proposal
d_date
4
1.2k
Thinking about Architecture for SwiftUI
d_date
8
2.3k
Integrate your app to modern world in Niigata
d_date
0
640
Other Decks in Programming
See All in Programming
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
240
令和7年版 あなたが使ってよいフロントエンド機能とは
mugi_uno
12
6k
Spring gRPC について / About Spring gRPC
mackey0225
0
180
Flatt Security XSS Challenge 解答・解説
flatt_security
0
1.1k
Rubyでつくるパケットキャプチャツール
ydah
0
530
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
270
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
28
6.1k
AHC041解説
terryu16
0
550
2025.01.17_Sansan × DMM.swift
riofujimon
2
670
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
200
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
360
CNCF Project の作者が考えている OSS の運営
utam0k
5
620
Featured
See All Featured
Visualization
eitanlees
146
15k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Building an army of robots
kneath
302
45k
Optimising Largest Contentful Paint
csswizardry
33
3k
The Cost Of JavaScript in 2023
addyosmani
47
7.3k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Designing for humans not robots
tammielis
250
25k
A designer walks into a library…
pauljervisheath
205
24k
Designing Experiences People Love
moore
139
23k
Become a Pro
speakerdeck
PRO
26
5.1k
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