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
4.9k
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
1
720
waiwai-swiftpm-part2
d_date
2
390
わいわいSwift PM part 1
d_date
2
340
What's new in Firebase 2021
d_date
2
1.3k
CI/CDをミニマルに構築する
d_date
1
490
Swift Package centered project - Build and Practice
d_date
20
12k
How to write Great Proposal
d_date
4
780
Thinking about Architecture for SwiftUI
d_date
8
2.2k
Integrate your app to modern world in Niigata
d_date
0
590
Other Decks in Programming
See All in Programming
ライブラリをパブリッシュせずにすばやく試す
exoego
2
110
プログラミング&実行できるWEBアプリを手作り
ohmori_yusuke
3
190
The test code generator using static analysis and LLM
mikik0
1
550
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
2
76k
Google Analytics でサイト速度を計測する / Measure site speed with Google Analytics
forrep
2
100
LTのやり方
yosuke_furukawa
PRO
7
930
Getting along with YAML comments with Psych
qnighy
2
1.4k
Svelte採用記 - 位置情報と可視化の会社で、全社の標準技術スタックに選ぶまで / Svelte Japan Online Meetup #3
sorami
2
200
Datadogのmonitorを Terraform管理に爆速で 移行する
kuro_kurorrr
2
280
의존성 주입과 Dagger Hilt
fornewid
0
110
「ソフトウェア設計」のドメイン - 「データモデリングでドメインを駆動する」を読んで
hidenorigoto
6
1.9k
Using Ruby in the browser is wonderful
ledsun
1
1.6k
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
221
21k
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
Infographics Made Easy
chrislema
238
18k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
22
1.5k
Writing Fast Ruby
sferik
623
60k
Building Flexible Design Systems
yeseniaperezcruz
321
37k
The World Runs on Bad Software
bkeepers
PRO
61
6.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
244
12k
Building an army of robots
kneath
300
42k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
246
20k
The Cost Of JavaScript in 2023
addyosmani
22
4k
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