Swift高階函數-min、max、starts、elementsEqual

一、min、max

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let ret1 = list.min()

let strlist = ["10", "9", "8", "7", "6", "5"]
let ret2 = strlist.min {
    let t0 = Int($0) ?? 0
    let t1 = Int($1) ?? 0
    return t0 < t1
}
print(ret1, ret2)
---console
Optional(10) Optional("5")

Sequence協議中源碼

min函數

  @inlinable
  @warn_unqualified_access
  public func min() -> Element? {
    return self.min(by: <)
  }
  @inlinable // protocol-only
  @warn_unqualified_access
  public func min(
    by areInIncreasingOrder: (Element, Element) throws -> Bool
  ) rethrows -> Element? {
    var it = makeIterator()
    guard var result = it.next() else { return nil }
    while let e = it.next() {
      if try areInIncreasingOrder(e, result) { result = e }
    }
    return result
  }

max函數

  @inlinable
  @warn_unqualified_access
  public func max() -> Element? {
    return self.max(by: <)
  }
  @inlinable // protocol-only
  @warn_unqualified_access
  public func max(
    by areInIncreasingOrder: (Element, Element) throws -> Bool
  ) rethrows -> Element? {
    var it = makeIterator()
    guard var result = it.next() else { return nil }
    while let e = it.next() {
      if try areInIncreasingOrder(result, e) { result = e }
    }
    return result
  }

二、starts

這裏只展示數據的用法,字典用法類似。

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let prefixList = [2, 4]
let ret1 = list.starts(with:prefixList)
let ret2 = list.starts(with: prefixList) { item, prefixelItem in
    return item * 2 == prefixelItem
}
print(ret1, ret2)
---console
false true

Sequence協議中源碼

  @inlinable
  public func starts<PossiblePrefix: Sequence>(
    with possiblePrefix: PossiblePrefix
  ) -> Bool where PossiblePrefix.Element == Element {
    return self.starts(with: possiblePrefix, by: ==)
  }
  @inlinable
  public func starts<PossiblePrefix: Sequence>(
    with possiblePrefix: PossiblePrefix,
    by areEquivalent: (Element, PossiblePrefix.Element) throws -> Bool
  ) rethrows -> Bool {
    var possiblePrefixIterator = possiblePrefix.makeIterator()
    for e0 in self {
      if let e1 = possiblePrefixIterator.next() {
        if try !areEquivalent(e0, e1) {
          return false
        }
      }
      else {
        return true
      }
    }
    return possiblePrefixIterator.next() == nil
  }
}

三、elementsEqual

let list = [1, 2, 3, 4, 5]
let list2 = [1, 4, 9, 16, 25]

let ret1 = list.elementsEqual(list2)
let ret2 = list.elementsEqual(list2) { el, seEl in
    return el * el == seEl
}
print(ret1, ret2)
---console
false true

Sequence協議中源碼

  @inlinable
  public func elementsEqual<OtherSequence: Sequence>(
    _ other: OtherSequence
  ) -> Bool where OtherSequence.Element == Element {
    return self.elementsEqual(other, by: ==)
  }
  @inlinable
  public func elementsEqual<OtherSequence: Sequence>(
    _ other: OtherSequence,
    by areEquivalent: (Element, OtherSequence.Element) throws -> Bool
  ) rethrows -> Bool {
    var iter1 = self.makeIterator()
    var iter2 = other.makeIterator()
    while true {
      switch (iter1.next(), iter2.next()) {
      case let (e1?, e2?):
        if try !areEquivalent(e1, e2) {
          return false
        }
      case (_?, nil), (nil, _?): return false
      case (nil, nil):           return true
      }
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章