Slide 1

Slide 1 text

Make Our Swift Better Daiki Matsudate @d_date

Slide 2

Slide 2 text

Daiki Matsudate • Tokyo, Japan • iOS Developer from iOS 4 • FOLIO -> ??? • Google Developer Expert for Firebase • Open API Generator Technical Committee • @d_date • Sushi

Slide 3

Slide 3 text

March, 18 - 20th, 2020 https://www.tryswift.co/

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { var destination = [String: String]() for (key, nillableValue) in source { if let value: Any = nillableValue { destination[key] = "\(value)" } } return destination }

Slide 6

Slide 6 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { return source.flatMap { $0 } }

Slide 7

Slide 7 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { return source.flatMap { $0 } // [(key: String, value: String)] }

Slide 8

Slide 8 text

0. Get advice from community

Slide 9

Slide 9 text

Discord: Swift-developers-Japan

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { var destination = [String: String]() for (key, nillableValue) in source { if let value: Any = nillableValue { destination[key] = "\(value)" } } return destination }

Slide 13

Slide 13 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { return source.reduce(into: [String: String](), { (result, x) in if let value = x.value { result[x.key] = "\(value)" } }) }

Slide 14

Slide 14 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { return source.reduce(into: [String: String](), { (result, x) in if let value = x.value { result[x.key] = "\(value)" } }) } public func reduce(into initialResult: Result, _ updateAccumulatingResult: (inout Result, (key: Key, value: Value)) throws -> ()) rethrows -> Result

Slide 15

Slide 15 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { return source.reduce(into: [String: String](), { (result, x) in if let value = x.value { result[x.key] = "\(value)" } }) } rejectNilHeaders(["a": "1", "b": nil, "c": "3"]) // ["a" : 1, "c" : 3]

Slide 16

Slide 16 text

static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] { return source.reduce(into: [String: String](), { (result, x) in if let value = x.value { result[x.key] = "\(value)" } }) } ["a": "1", "b": nil, "c": “3”].xxx({ $0 }) // ["a" : 1, "c" : 3]

Slide 17

Slide 17 text

map and flatMap in Collection let r = [1, 2, 3].map { $0 * 2 } // [2, 4, 6] let r22 = [1, nil, 3].flatMap { $0 } // [1, 3]

Slide 18

Slide 18 text

map and compactMap in Collection let r = [1, 2, 3].map { $0 * 2 } // [2, 4, 6] let r22 = [1, nil, 3].compactMap { $0 } // [1, 3]

Slide 19

Slide 19 text

mapValues in Dictionary ["a": 1, "b": 2, "c": 3].mapValues({ $0 * 2 }) // ["a": 2, "b": 4, "c": 6]

Slide 20

Slide 20 text

map compactMap mapValues

Slide 21

Slide 21 text

map compactMap mapValues ?

Slide 22

Slide 22 text

Dictionary.compactMapValues map compactMap mapValues compactMapValues

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

["a": "1", "b": nil, "c": “3”].compactMapValues({ $0 }) // ["a" : 1, "c" : 3] 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 25

Slide 25 text

1.Post your idea to Forum

Slide 26

Slide 26 text

https://forums.swift.org

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 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 30

Slide 30 text

2. Make proposal to Swift-evolution

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

2. Make proposal to Swift-evolution

Slide 33

Slide 33 text

Proposal • Introduction • Motivation • Proposed Solution • Detailed Design • Source compatibility • Effect on ABI stability / API resilience • Alternative considered

Slide 34

Slide 34 text

2. Make proposal to Swift-evolution

Slide 35

Slide 35 text

3. Build your environment

Slide 36

Slide 36 text

3. Build your environment $ git clone https://github.com/apple/swift.git

Slide 37

Slide 37 text

3. Build your environment $ ./swift/utils/update-checkout --clone

Slide 38

Slide 38 text

3. Build your environment $ cd swift $ utils/build-script -Rt

Slide 39

Slide 39 text

3. Build your environment Waiting for 30 minutes

Slide 40

Slide 40 text

3. Build your environment

Slide 41

Slide 41 text

4. Implement your idea /// Returns a new dictionary containing the keys of this dictionary with the /// values transformed by the given closure. /// - Parameter transform: A closure that transforms a value. `transform` /// accepts each value of the dictionary as its parameter and returns a /// transformed value of the same or of a different type. /// - Returns: A dictionary containing the keys and transformed values of /// this dictionary. @inlineable // FIXME(sil-serialize-all) public func compactMapValues( _ transform: (Value) throws -> T? ) rethrows -> Dictionary { return try self.reduce(into: [Key: T](), { (result, x) in if let value = try transform(x.value) { result[x.key] = value } }) }

Slide 42

Slide 42 text

5. Test your implementation // RUN: %target-run-simple-swift // REQUIRES: executable_test // REQUIRES: objc_interop import Foundation import StdlibUnittest var tests = TestSuite("CompactMapValues") tests.test("DefaultReturnType") { var result = ["a": "1", "c": "3"].compactMapValues { $0 } expectType([String: String].self, &result) } tests.test("ExplicitTypeContext") { expectEqual(["a":"1","c":"3"], ["a":"1","b":nil,"c":"3"].compactMapValues({$0}) ) expectEqual(["a": 1, "b": 2], ["a":"1","b":"2", "c":"three"].compactMapValues(Int.init) ) } runAllTests()

Slide 43

Slide 43 text

// RUN: %target-run-simple-swift // REQUIRES: executable_test // REQUIRES: objc_interop import Foundation import StdlibUnittest var tests = TestSuite("CompactMapValues") tests.test("DefaultReturnType") { var result = ["a": "1", "c": "3"].compactMapValues { $0 } expectType([String: String].self, &result) } tests.test("ExplicitTypeContext") { expectEqual(["a":"1","c":"3"], ["a":"1","b":nil,"c":"3"].compactMapValues({$0}) ) expectEqual(["a": 1, "b": 2], ["a":"1","b":"2", "c":"three"].compactMapValues(Int.init) ) } runAllTests() Import Write your test case 5. Test your implementation

Slide 44

Slide 44 text

6. Benchmark your changes (Optional) public let DictionaryCompactMapValues = [ BenchmarkInfo(name: "DictionaryCompactMapValuesOfNilValue", runFunction: run_DictionaryCompactMapValuesOfNilValue, tags: [.validation, .api, .Dictionary]),] @inline(never) public func run_DictionaryCompactMapValuesOfNilValue(_ N: Int) { let size = 100 var dict = [Int: Int?](minimumCapacity: size) // Fill Dictionary for i in 1...size { if i % 2 == 0 { dict[i] = nil } else { dict[i] = i } } CheckResults(dict.count == size / 2) var refDict = [Int: Int]() for i in stride(from: 1, to: 100, by: 2) { refDict[i] = i } var newDict = [Int: Int]() for _ in 1...1000*N { newDict = dict.compactMapValues({$0}) if newDict != refDict { break } } CheckResults(newDict == refDict) }

Slide 45

Slide 45 text

6. Benchmark your changes (Optional) public let DictionaryCompactMapValues = [ BenchmarkInfo(name: "DictionaryCompactMapValuesOfNilValue", runFunction: run_DictionaryCompactMapValuesOfNilValue, tags: [.validation, .api, .Dictionary]),] @inline(never) public func run_DictionaryCompactMapValuesOfNilValue(_ N: Int) { let size = 100 var dict = [Int: Int?](minimumCapacity: size) // Fill Dictionary for i in 1...size { if i % 2 == 0 { dict[i] = nil } else { dict[i] = i } } CheckResults(dict.count == size / 2) var refDict = [Int: Int]() for i in stride(from: 1, to: 100, by: 2) { refDict[i] = i } var newDict = [Int: Int]() for _ in 1...1000*N { newDict = dict.compactMapValues({$0}) if newDict != refDict { break } } CheckResults(newDict == refDict) } Specify your benchmark Prepare the data Check compactMapValues work

Slide 46

Slide 46 text

7. Waiting to starting evolution process

Slide 47

Slide 47 text

7. Waiting to starting evolution process

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Available Now!

Slide 51

Slide 51 text

import Foundation import UIKit let label = UILabel() let attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: "Menlo", size: 14)] let attributedText = NSAttributedString(string: “Great conference I love”, attributes: attributes) label.attributedText = attributedText

Slide 52

Slide 52 text

import Foundation import UIKit let label = UILabel() let attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: "Menlo", size: 14)] let attributedText = NSAttributedString(string: “Great conference I love”, attributes: attributes) label.attributedText = attributedText

Slide 53

Slide 53 text

import Foundation import UIKit let label = UILabel() let attributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: "Menlo", size: 14)].compactMapValues {$0} let attributedText = NSAttributedString(string: "Great conference I love", attributes: attributes) label.attributedText = attributedText

Slide 54

Slide 54 text

To be a contributor of Swift • https://swift.org/contributing • https://swift.org/quickstart- contribution/

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

March, 18 - 20th, 2020 https://www.tryswift.co/

Slide 57

Slide 57 text

Recap 0. Get advice from local community 1. Post your idea to forum.swift.org 2. Make proposal to Swift-evolution 3. Build your environment 4. Implement your idea 5. Test your implementation 6. Benchmark your changes (Optional) 7. Waiting to starting evolution process

Slide 58

Slide 58 text

Thank you! Daiki Matsudate @d_date