美文网首页
Swift高阶函数-min、max、starts、element

Swift高阶函数-min、max、starts、element

作者: loongod | 来源:发表于2022-07-01 09:22 被阅读0次

    一、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
          }
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:Swift高阶函数-min、max、starts、element

          本文链接:https://www.haomeiwen.com/subject/qmzfbrtx.html