Slide 1

Slide 1 text

How to use Dictionary.compactMapValues potatotips #55 Daiki Matsudate / @d_date

Slide 2

Slide 2 text

Daiki Matsudate @d_date

Slide 3

Slide 3 text

https://peaks.cc/iOS_architecture

Slide 4

Slide 4 text

IUUQTZBLJSJOHPCPPUIQNJUFNT

Slide 5

Slide 5 text

Dictionary .compactMapValues

Slide 6

Slide 6 text

extension Dictionary { public func compactMapValues(_ 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

Slide 7

Slide 7 text

Use Cases

Slide 8

Slide 8 text

let d1 = ["a": "1", "b": "2", "c": nil] d1.filter({ $0.value != nil}).mapValues({ $0! }) // ["b": "2", "a": "1"]

Slide 9

Slide 9 text

["a": "1", "b": nil, "c": “3”].compactMapValues({ $0 }) // ["a" : 1, "c" : 3]

Slide 10

Slide 10 text

var object = { 'a': 1, 'b': null, 'c': 3 }; _.omitBy(object, _.isNil); // => { 'a': 1, 'c': 3 } DG@PNJU#Z

Slide 11

Slide 11 text

let d = ["a": "1", "b": "2", "c": "three"] d.mapValues(Int.init).filter({ $0.value != nil}).mapValues({ $0! })

Slide 12

Slide 12 text

extension Dictionary { public func compactMapValues(_ 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 } }) } }

Slide 13

Slide 13 text

let d = ["a": "1", "b": "2", "c": "three"] d.compactMapValues(Int.init) // ["b": 2, "a": 1]

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

• Daiki Matsudate / @d_date Available in Swift 5

Slide 16

Slide 16 text

Make our Swift better 09/05/2018 try! Swift NYC Daiki Matsudate / @d_date

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

• compactMapValues is available in Swift 5 • compactMapValues can transform and unwrap dictionary • Let’s try to contribute to Swift lang Recap

Slide 19

Slide 19 text

No content