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
610
waiwai-swiftpm-part2
d_date
2
380
わいわい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
1
500
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
禅の心を手に入れよ
eltociear
1
410
Open standards for building event-driven applications in the cloud
meteatamel
0
190
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
1.1k
Balkan Ruby 2024 — How and why to run SQLite on Rails in production
fractaledmind
0
100
Going beyond Apache Parquet's default settings
xhochy
0
140
冗長なエラーログを削減し、スタックトレースを手に入れる / Reducing Verbose Error Logs and Obtaining Stack Traces
upamune
0
1.1k
AppRouter Panel Talk
yosuke_furukawa
PRO
1
490
Domain-Driven Transformation
hschwentner
2
1.5k
Code Reviews
bkuhlmann
4
900
業務ツールとして使うPostman
msys75
0
110
パフォーマンスを求めてDBに機能を寄せる戦略
aoyagikouhei
0
110
Elm 0.19.0 Changes
bkuhlmann
0
510
Featured
See All Featured
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
GraphQLの誤解/rethinking-graphql
sonatard
56
9.3k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
[RailsConf 2023] Rails as a piece of cake
palkan
28
4k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
0
30
What's new in Ruby 2.0
geeforr
337
31k
Teambox: Starting and Learning
jrom
128
8.4k
The Power of CSS Pseudo Elements
geoffreycrofte
62
5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Bash Introduction
62gerente
605
210k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
What’s in a name? Adding method to the madness
productmarketing
PRO
17
2.7k
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