Swift高階函數(shù)-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協(xié)議中源碼

min函數(shù)

  @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函數(shù)

  @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

這里只展示數(shù)據(jù)的用法,字典用法類似。

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協(xié)議中源碼

  @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協(xié)議中源碼

  @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
      }
    }
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容