Ray Fix
August 30, 2018
2.1k

# アルゴリズムを通じて よりよいアプリを

One of the "hidden" themes of WWDC 2018 was algorithms. This talk looks at some of the sessions that talked about using algorithms to make better code and shows a toy example of using algorithms using UICollectionView animation.

August 30, 2018

## Transcript

2. None

6. ### iOS 12 ϚʔέοτΠϯ • ΑΓ଎͘ɺΑΓ൓Ԡྑ͘ • Ξχϝʔγϣϯ͕ҰஈͱεϜʔζʹ • ΞϓϦέʔγϣϯͷىಈ͕࠷େ40%଎͘ •

ΩʔϘʔυͷද͕ࣔ࠷େ50%଎͘ • εϫΠϓʹΑΔΧϝϥͷىಈ͕࠷େ70%଎͘ • ෛՙ࣌ͷڞ༗γʔτͷද͕ࣔ࠷େ2ഒ଎͘ iPhone 5s
7. ### O(1) ఆ਺࣌ؒ O(log n) ର਺ O(n) ઢܗؔ਺ O(n log n)

ઢܗର਺ O(n2) ೋ৐࣌ؒ O(nc) ଟ߲ࣜ࣌ؒ O(2n) ؒࢦ਺࣌ؒ O(n!) ֊৐ؔ਺ ϥϯμ΢ͷه߸ εέʔϧ͢Δ εέʔϧ͠ͳ͍
8. ### O(1) O(log n) O(n) O(n log n) O(n2) O(nc) O(2n)

O(n!) Auto Layout Session 202 iOS 12

10. ### Layout “Churn” class MyView: UIView { override func updateConstraints() {

} } ϨΠΞ΢τ੍໿࠶ॳظԽʹ஫ҙ ̍ඵʹ̍̎̌ճݺͼग़͞ΕΔՄೳੑ͕͋Δ Session 220
11. ### ίϨΫγϣϯΛޮՌతʹ ࢖༻͢Δํ๏ • ஗Ԇॲཧ΍εϥΠεͷίετΛཧղ͠ͳ͕Βɺ  ίϨΫγϣϯ࢖͍·͠ΐ͏ • Swiftܕ ͱ FoundationܕͷؒʹϒϦοδ͕ඞཁͰ͢ɻ  ίετ͸O(1)

͔ΒO(n) Ͱ͢ɻ let story = “σοΧ͍ string" let text = NSMutableAttributedString(string: story) let string = text.string Session 229
12. ### ԕ͗ͨ͢ڮ let story = “σοΧ͍ string" let text = NSMutableAttributedString(string:

story) let string = text.string as NSString for index in 1 ... { } let nsRange = string.range(of: “kitten") n InstrumentsͰܭΓ·͠ΐ͏
13. ### ΞϧΰϦζϜΛड͚ೖΕΔ Crusty Dave Abrahams @DaveAbrahams Swift library designer / Professor

of blowing-your-mind @apple @Mind your own business . Session 223
14. ### γΣʔϓͷ࡟আ extension Canvas { mutating func deleteSelection() { for i

in 0 ..< shapes.count { if shapes[i].isSelected { shapes.remove(at: i) } } } }
15. ### γΣʔϓͷ࡟আ extension Canvas { mutating func deleteSelection() { for i

in 0 ..< shapes.count { if shapes[i].isSelected { shapes.remove(at: i) } } } } TRAP
16. ### γΣʔϓͷ࡟আ (V2) extension Canvas { mutating func deleteSelection() { var

i = 0 while i < shapes.count { if shapes[i].isSelected { shapes.remove(at: i) } i += 1 } } }
17. ### γΣʔϓͷ࡟আ (V2) extension Canvas { mutating func deleteSelection() { var

i = 0 while i < shapes.count { if shapes[i].isSelected { shapes.remove(at: i) } i += 1 } } } ͋Εʁ
18. ### γΣʔϓͷ࡟আ (V3) extension Canvas { mutating func deleteSelection() { var

i = 0 while i < shapes.count { if shapes[i].isSelected { shapes.remove(at: i) } else { i += 1 } } } }
19. ### γΣʔϓͷ࡟আ (V4) extension Canvas { mutating func deleteSelection() { for

i in (0..<shapes.count).reversed() { if shapes[i].isSelected { shapes.remove(at: i) } } } } ͔͠͠…
20. ### γΣʔϓͷ࡟আ (V4) extension Canvas { mutating func deleteSelection() { for

i in (0..<shapes.count).reversed() { if shapes[i].isSelected { shapes.remove(at: i) } } } } O(n) O(n) O(n2) ೋ৐࣌ؒ

22. ### γΣʔϓͷ࡟আ (V5) extension Canvas { mutating func deleteSelection() { shapes.removeAll

{ \$0.isSelected } } } O(n)

26. ### ׽ࣈ͕೉͍͠ cards.shuffle() let shuffledIndices = cards.indices.shuffled() ฤूεςοϓ for (new, orig)

in shuffledIndices.enumerated() { newCards[new] = originalCards[orig] } collectionView.performBatchUpdates({ for ( ) in shuffledIndices.enumerated() { let src = IndexPath(item: orig, section: 0) let dst = IndexPath(item: new, section: 0) collectionView.moveItem(at: src, to: dst) } }, completion: nil) new orig, Session 225 όά ʢnew, orig) ͕ਖ਼͍͠

29. ### zip 2 ( ) , 0 ( ) , 1

( ) , 4 ( ) , 3 ( ) ,
30. ### sort 2 ( ) , 0 ( ) , 1

( ) , 4 ( ) , 3 ( ) ,
31. ### δΣωϦοΫʹ extension Collection { func sortedWithIndices(by order: (Element, Element) ->

Bool) -> ([Element], moves: [(from: Index, to: Index)]) { let elementsAndIndices = zip(self, self.indices) let sorting = elementsAndIndices.sorted { order(\$0.0, \$1.0) } let elements = sorting.map { \$0.0 } let moves = sorting.enumerated().map { (offset, element) -> (from: Index, to: Index) in var toIndex: Index = startIndex formIndex(&toIndex, offsetBy: offset) let fromIndex: Index = element.1 return (from: fromIndex, to: toIndex) }.filter { \$0.from != \$0.to } return (elements, moves: moves) } }
32. ### ࠩ෼ϥΠϒϥϦʔ • DeepDiﬀ • Diﬀer • DiﬀerenceKit • IGListKit •

RxDataSources DiﬀerenceKit Heckel’s Algorithm O(n)
33. None