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

2af8daf170a3ed693e577a9471ea1f10?s=47 Ray Fix
August 30, 2018

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

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.

2af8daf170a3ed693e577a9471ea1f10?s=128

Ray Fix

August 30, 2018
Tweet

Transcript

  1. ΞϧΰϦζϜΛ௨ͯ͡ ΑΓΑ͍ΞϓϦΛ iOSDC 2018 ϨΠɾϑΟοΫε @rayfix

  2. None
  3. ΞϧΰϦζϜ͸ ۀքͷֵ໋Λىͤ͜·͢! ϑΝΠϧ 0 15 30 45 60 ΦʔσΟΦσʔλ ѹॖ͞ΕͨΦʔσΟΦσʔλ

  4. ؾ͖ͮ·͔ͨ͠ʁ

  5. ALGORITHMS

  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
  9. Auto Layout ϨΞ΢τ੍໿ͷ਺ ॲཧ࣌ؒ O(n) 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) ೋ৐࣌ؒ
  21. Ϗοάɾσʔλ

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

    { $0.isSelected } } } O(n)
  23. ΩʔϙΠϯτ •ඪ४ϥΠϒϥϦͷΞϧΰϦζϜΛ
 ࢖ͬͨ΄͏͕෼͔Γ΍͍͢ •ඪ४ϥΠϒϥϦͷΞϧΰϦζϜ͸ੑೳͷ
 ༧ଌੑ͕͋Δ •O(1) Ҏ֎͸ඞͣυΩϡϝϯτ͞Ε͍ͯΔ

  24. Raw LoopΛ࢖Θͳ͍ http://sean-parent.stlab.cc/presentations/ 2013-09-11-cpp-seasoning/cpp-seasoning.pdf Sean Parent @SeanParent Software Engineer at

    Adobe C++ Seasoning
  25. Raw LoopΛ࢖Θͳ͍ •ඪ४ͷΞϧΰϦζϜΛ࢖͏ •ඪ४ͷΞϧΰϦζϜΛ࣮૷͢Δ •શͯͷίʔυ͸ϥΠϒϥϦʔԽ͢Δ •৽͍͠ΞϧΰϦζϜΛ։ൃ͢Δ •༗໊ʹͳΔ


  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) ͕ਖ਼͍͠
  27. ࠓ೔ͷӳޠڭࣨ

  28. ΤϏϯάϋ΢ε๨٫ۂઢ R = e− t S

  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. ࠩ෼ϥΠϒϥϦʔ • DeepDiff • Differ • DifferenceKit • IGListKit •

    RxDataSources DifferenceKit Heckel’s Algorithm O(n)
  33. None
  34. Summary •ΞϧΰϦζϜΛษڧ͠·͠ΐ͏ •ඪ४ϥΠϒϥϦʔΛಡΈ·͠ΐ͏ •Raw Loop Λආ͚·͠ΐ͏

  35. ͝੩ௌ ͋Γ͕ͱ͏͍͟͝·ͨ͠!