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

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

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.

Ray Fix

August 30, 2018
Tweet

More Decks by Ray Fix

Other Decks in Technology

Transcript

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

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

    ઢܗର਺ O(n2) ೋ৐࣌ؒ O(nc) ଟ߲ࣜ࣌ؒ O(2n) ؒࢦ਺࣌ؒ O(n!) ֊৐ؔ਺ ϥϯμ΢ͷه߸ εέʔϧ͢Δ εέʔϧ͠ͳ͍
  3. 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
  4. Layout “Churn” class MyView: UIView { override func updateConstraints() {

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

    ͔ΒO(n) Ͱ͢ɻ let story = “σοΧ͍ string" let text = NSMutableAttributedString(string: story) let string = text.string Session 229
  6. ԕ͗ͨ͢ڮ 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ͰܭΓ·͠ΐ͏
  7. ΞϧΰϦζϜΛड͚ೖΕΔ Crusty Dave Abrahams @DaveAbrahams Swift library designer / Professor

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

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

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

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

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

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

    i in (0..<shapes.count).reversed() { if shapes[i].isSelected { shapes.remove(at: i) } } } } ͔͠͠…
  14. γΣʔϓͷ࡟আ (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) ೋ৐࣌ؒ
  15. ׽ࣈ͕೉͍͠ 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) ͕ਖ਼͍͠
  16. zip 2 ( ) , 0 ( ) , 1

    ( ) , 4 ( ) , 3 ( ) ,
  17. sort 2 ( ) , 0 ( ) , 1

    ( ) , 4 ( ) , 3 ( ) ,
  18. δΣωϦοΫʹ 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) } }
  19. ࠩ෼ϥΠϒϥϦʔ • DeepDiff • Differ • DifferenceKit • IGListKit •

    RxDataSources DifferenceKit Heckel’s Algorithm O(n)