Swift高階函數(shù)contains?allSatisfy?reversed?lexicographicallyPrecedes用法示例
一、contains
返回一個(gè)布爾值,指示序列的每個(gè)元素是否滿足給定的條件。如果有一個(gè)滿足即返回。
let expenses = [21.37, 55.21, 9.32, 10.18, 388.77, 11.41] let hasBigPurchase = expenses.contains { $0 > 100 } // 'hasBigPurchase' == true
Sequence
協(xié)議源碼
@inlinable public func contains(_ element: Element) -> Bool { if let result = _customContainsEquatableElement(element) { return result } else { return self.contains { $0 == element } } }
@inlinable public func contains( where predicate: (Element) throws -> Bool ) rethrows -> Bool { for e in self { if try predicate(e) { return true } } return false }
二、allSatisfy
返回一個(gè)布爾值,指示序列的每個(gè)元素是否滿足給定的條件。需要所有元素都滿足。
let names = ["Sofia", "Camilla", "Martina", "Mateo", "Nicolás"] let allHaveAtLeastFive = names.allSatisfy({ $0.count >= 5 }) // allHaveAtLeastFive == true
Sequence
協(xié)議源碼
allSatisfy
里面調(diào)用了contains
方法
@inlinable public func allSatisfy( _ predicate: (Element) throws -> Bool ) rethrows -> Bool { return try !contains { try !predicate($0) } }
三、reversed
返回一個(gè)數(shù)組,該數(shù)組以相反的順序包含此序列的元素。
let list = [1, 2, 3, 4, 5] let ret = list.reversed() print(Array(ret)) ---console [5, 4, 3, 2, 1]
Sequence
協(xié)議源碼
@inlinable public __consuming func reversed() -> [Element] { // FIXME(performance): optimize to 1 pass? But Array(self) can be // optimized to a memcpy() sometimes. Those cases are usually collections, // though. var result = Array(self) let count = result.count for i in 0..<count/2 { result.swapAt(i, count - ((i + 1) as Int)) } return result }
四、lexicographicallyPrecedes
返回一個(gè)布爾值,該值指示在字典順序(字典)中該序列是否在另一個(gè)序列之前,使用給定條件語句比較元素。
let list = [1, 2, 3, 4, 5] let list2 = [2, 2, 3, 5] let ret1 = list.lexicographicallyPrecedes(list2) let ret2 = list2.lexicographicallyPrecedes(list) print(ret1, ret2) ---console true false
Sequence
協(xié)議源碼
@inlinable public func lexicographicallyPrecedes<OtherSequence: Sequence>( _ other: OtherSequence ) -> Bool where OtherSequence.Element == Element { return self.lexicographicallyPrecedes(other, by: <) }
@inlinable public func lexicographicallyPrecedes<OtherSequence: Sequence>( _ other: OtherSequence, by areInIncreasingOrder: (Element, Element) throws -> Bool ) rethrows -> Bool where OtherSequence.Element == Element { var iter1 = self.makeIterator() var iter2 = other.makeIterator() while true { if let e1 = iter1.next() { if let e2 = iter2.next() { if try areInIncreasingOrder(e1, e2) { return true } if try areInIncreasingOrder(e2, e1) { return false } continue // Equivalent } return false } return iter2.next() != nil } }
以上就是Swift高階函數(shù)contains allSatisfy reversed lexicographicallyPrecedes用法示例的詳細(xì)內(nèi)容,更多關(guān)于Swift高階函數(shù)用法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Swift編程中實(shí)現(xiàn)希爾排序算法的代碼實(shí)例
希爾排序是對(duì)插入排序的一種改進(jìn)版本,算法本身并不穩(wěn)定,存在優(yōu)化空間,這里我們來講一下希爾排序的大體思路及Swift編程中實(shí)現(xiàn)希爾排序算法的代碼實(shí)例2016-07-07Swift開發(fā)應(yīng)用中如何更方便地使用顏色詳解
這篇文章主要給大家介紹了關(guān)于Swift開發(fā)應(yīng)用中如何更方便地使用顏色的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03Swift實(shí)現(xiàn)JSON轉(zhuǎn)Model的方法及HandyJSON使用講解
這篇文章給大家介紹了Swift實(shí)現(xiàn)JSON轉(zhuǎn)Model的方法及HandyJSON使用講解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-07-07Swift實(shí)現(xiàn)表格視圖單元格單選(1)
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)表格視圖單元格單選,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01