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.8k
waiwai-swiftpm-part2
d_date
3
560
わいわいSwift PM part 1
d_date
2
450
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
1.9k
Thinking about Architecture for SwiftUI
d_date
8
2.5k
Integrate your app to modern world in Niigata
d_date
0
720
Other Decks in Programming
See All in Programming
ロボットのための工場に灯りは要らない
watany
10
2.5k
AIコードレビューの導入・運用と AI駆動開発における「AI4QA」の取り組みについて
hagevvashi
0
420
The Ralph Wiggum Loop: First Principles of Autonomous Development
sembayui
0
3.7k
猫の手も借りたい!ので AIエージェント猫を作って社内に放した話 Claude Code × Container Lambda の Slack Bot "DevNeko"
naramomi7
0
260
2026年は Rust 置き換えが流行る! / 20260220-niigata-5min-tech
girigiribauer
0
230
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
4
500
TipKitTips
ktcryomm
0
160
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
110
AWS Infrastructure as Code の新機能 2025 総まとめ 〜SA 4人による怒涛のデモ祭り〜
konokenj
10
3.3k
Agentic AI: Evolution oder Revolution
mobilelarson
PRO
0
140
LangChain4jとは一味違うLangChain4j-CDI
kazumura
1
170
Swift ConcurrencyでよりSwiftyに
yuukiw00w
0
250
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
55
8k
Paper Plane (Part 1)
katiecoart
PRO
0
5.4k
Test your architecture with Archunit
thirion
1
2.2k
Statistics for Hackers
jakevdp
799
230k
Tell your own story through comics
letsgokoyo
1
840
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
Darren the Foodie - Storyboard
khoart
PRO
3
2.8k
How to make the Groovebox
asonas
2
2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
Evolving SEO for Evolving Search Engines
ryanjones
0
150
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