Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Sequence の便利 API 再発見

Swift Sequence の便利 API 再発見

Swift Sequence の便利 API 再発見 / Rediscovering useful Swift Sequence APIs

potatotips #95 iOS/Android開発Tips共有会
2026/05/15 19:00〜
https://potatotips.connpass.com/event/389026/

More Decks by treastrain / Tanaka Ryoga

Other Decks in Technology

Transcript

  1. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. 1

    Swift Sequence ͷศར API ࠶ൃݟ potatotips #95 treastrain / Tanaka Ryoga #potatotips
  2. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. 2

    treastrain / Tanaka Ryoga @treastrainɹhttps://tret.jp Swift 🦅 / Core NFC 📶 - Japan NFC Reader 💳 - PadDisplay 📺 DeNA Co., Ltd. - iOS App Developer ( April 2021 - Current)
  3. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. Sequence

    4 Array Dictionary Range / ClosedRange Set String …etc. ద߹͍ͯ͠Δܕ InlineArray
  4. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. for-in

    จ 5 for time in 1...95 { print("potatotips #\(time)") } // potatotips #1 // potatotips #2 // ... // potatotips #94 // potatotips #95
  5. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. Swift

    ͷ഑ྻ͸஋ܕ 6 struct Object { var isReady: Bool var value: Int } let array: [Object] = ... let result = array .filter { $0.isReady } .map { $0.value } .prefix(3) for element in result { // ... }
  6. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. Swift

    ͷ഑ྻ͸஋ܕ 7 let result = array .filter { $0.isReady } .map { $0.value } .prefix(3) for element in result { // ... } ཁૉ਺ɹɹ 10,000 ཁૉ਺࠷େ 10,000 ཁૉ਺࠷େ 10,000 ϧʔϓճ਺ 3
  7. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. let

    result = array .filter { $0.isReady } .map { $0.value } .prefix(3) for element in result { // ... } Swift ͷ഑ྻ͸஋ܕ 8 var result: [Int] = [] for item in array { if item.isReady { result.append(item.value) } if result.count == 3 { break } } for element in result { // ... } ໋ྩతʹॻ͘৔߹
  8. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. LazySequence

    9 ཁૉʹର͢Δૢ࡞Λ஗Ԇͤ͞Δ let result = array .lazy .filter { $0.isReady } .map { $0.value } .prefix(3) for element in result { // ... } ཁૉ਺ɹɹ 10,000 ཁૉ਺࠷େ 10,000 ཁૉ਺࠷େ 10,000 ϧʔϓճ਺ 3 var result: [Int] = [] for item in array { if item.isReady { result.append(item.value) } if result.count == 3 { break } } for element in result { // ... }
  9. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. stride(from:to:by:)

    10 ຤ඌΛؚ·ͳ͍ let hours = [0, 3, 6, 9, 12, 15, 18, 21] // 0, 3, 6, 9, 12, 15, 18, 21 let hours = stride(from: 0, to: 24, by: 3)
  10. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. stride(from:through:by:)

    11 ຤ඌΛؚΉ let hours = [0, 3, 6, 9, 12, 15, 18, 21] // 0, 3, 6, 9, 12, 15, 18, 21 let hours = stride(from: 0, to: 24, by: 3) // 0, 3, 6, 9, 12, 15, 18, 21, 24 let hours = stride(from: 0, through: 24, by: 3)
  11. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. sequence(first:next:)

    12 લͷ஋Λ࢖͍ͳ͕ΒཁૉΛ࡞Δ let powersOfTwo = [1, 2, 4, 8, 16, 32, 64, 128] // 1, 2, 4, 8, 16, 32, 64, 128 let powersOfTwo = sequence(first: 1, next: { $0 * 2 }).prefix(8)
  12. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. sequence(state:next:)

    13 ঢ়ଶΛอ࣋͠ͳ͕ΒཁૉΛ࡞Δ // 0, 1, 1, 2, 3, 5, 8, 13, 21 let fibonacci = sequence(state: (0, 1)) { state in let current = state.0 state = (state.1, state.0 + state.1) return current } .prefix(9)
  13. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. repeatElement(_:count:)

    14 ಉ͡ཁૉ͕࿈ଓ͢Δ Sequence Λ࡞Δ let zeros = Array(repeating: 0, count: 10000) let zeros = repeatElement(0, count: 10000)
  14. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. partitioned(by:)

    17 ৚݅ʹΑͬͯ2ͭͷ഑ྻʹ෼͚Δ import Algorithms let numbers = [1, 2, 3, 4, 5] let (evens, odds) = numbers.partitioned(by: { $0.isMultiple(of: 2) }) print(evens) // [1, 3, 5] print(odds) // [2, 4]
  15. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. chunks(ofCount:)

    18 ࢦఆͨ͠਺͝ͱͷ·ͱ·Γʹ͢Δ import Algorithms let characters = ["ϒϥ΢ϯ", "ίχʔ", "αϦʔ", "͚Μ͘͞", "͑Μ͡Μ"] for chunk in characters.chunks(ofCount: 3) { print(chunk) // ["ϒϥ΢ϯ", "ίχʔ", "αϦʔ"] // ["͚Μ͘͞", "͑Μ͡Μ"] }
  16. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. adjacentPairs()

    19 ྡΓ߹͏ཁૉͷϖΞΛऔಘ͢Δ import Algorithms let places = ["DeNA", "GMOϖύϘ", "YOUTRUST", "LINEϠϑʔ"] for pair in places.adjacentPairs() { print(pair.0, "→", pair.1) // DeNA → GMOϖύϘ // GMOϖύϘ → YOUTRUST // YOUTRUST → LINEϠϑʔ }
  17. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. firstNonNil(_:)

    20 ࠷ॳʹ nil Ͱ͸ͳ͍ཁૉΛऔಘ͢Δ import Algorithms; import SwiftUI struct Profile { var displayName, username, email: String? var candidates: [String?] { [displayName, username, email] } } struct ContentView: View { var profile: Profile var body: some View { Text(profile.candidates.firstNonNil(\.self) ?? "໊ແ͠") } }
  18. Copyright © 2026 treastrain / Tanaka RyogaɹAll rights reserved. CollectionOfOne

    EmptyCollection ීஈͷ iOS ΞϓϦ։ൃͰ͸࢖Θͳͯ͘ྑ͍ 21 // 1ͭͷཁૉ͚ͩΛ࣋ͭίϨΫγϣϯ let collectionOfOne = CollectionOfOne("treastrain") // ීஈͷ iOS ΞϓϦ։ൃͰ͋Ε͹഑ྻͰΑ͍ let singleArray = ["treastrain"] // ཁૉ͕·ͬͨ͘ͳ͍ίϨΫγϣϯ let emptyCollection = EmptyCollection<Int>() // ීஈͷ iOS ΞϓϦ։ൃͰ͋Ε͹഑ྻͰΑ͍ let emptyArray: [Int] = []