Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. guard let idx = allAnnotations.index(where: {$0 is MKUserLocation}) else {return}
  2. let userLocation = allAnnotations.remove(at: idx) as! MKUserLocation
  3.  
  4. [Type1, Type1, Type1, Type1, Type1, Type1, Type2]
  5.  
  6. [Type1, Type1, Type1, Type1, Type1, Type1]
  7.  
  8. Type2?
  9.  
  10. extension Array {
  11. mutating func popFirstElementWith(_ predicate:(Element)->(Bool)) -> Element? {
  12. var firstElement:Element?
  13. self = self.compactMap({ (element:Element) -> Element? in
  14. guard predicate(element) == true else {
  15. return element
  16. }
  17. if firstElement == nil {
  18. firstElement = element
  19. }
  20. return nil
  21. })
  22. return firstElement
  23. }
  24. }
  25.  
  26. var array:[Any] = ["a", "b", 1, "c"]
  27. let element = array.popFirstElementWith { (element) -> (Bool) in
  28. return element is Int
  29. }
  30. print(element!)
  31. print(array)
  32.  
  33. 1
  34. ["a", "b", "c"]
  35.  
  36. extension Array {
  37. mutating func dropWhere(_ isIncluded: (Element) throws -> Bool) -> [Element] {
  38. do {
  39. let reverseArray = try filter { try isIncluded($0) }
  40. self = try filter { try !isIncluded($0) }
  41.  
  42. return reverseArray
  43. } catch {
  44. return []
  45. }
  46. }
  47. }
  48.  
  49. var array = [1, 2, 3]
  50. let array2 = array.dropWhere { $0 > 2 }
  51. print(array) //[1, 2]
  52. print(array2) //[3]
  53.  
  54. var typeList = [Type1, Type1, Type1, Type1, Type1, Type1, Type2]
  55.  
  56. var slicedArray:[ArraySlice] = typeList.split { (value) -> Bool in
  57. return value == YourCondition
  58. }
  59. print(slicedArray)
  60.  
  61. extension Array {
  62. mutating func popFirstElementWith(_ predicate:(Element)->(Bool)) -> Element? {
  63. for (idx, element) in enumerated() {
  64. if predicate(element) {
  65. remove(at: idx)
  66. return element
  67. }
  68. }
  69. return nil
  70. }
  71. }
  72.  
  73. @inlinable public mutating func removeAll(where shouldBeRemoved: (Element) throws -> Bool) rethrows
  74.  
  75. [1,2,3].removeAll(where: { $0 == 2})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement