Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.76 KB | None | 0 0
  1. let someArray = [1, 12, 123, 110, 1245, 20, 1, 6]
  2. func filterArrray(array: [Int], condition: (Int) -> (Bool)) -> [Int] {
  3.     var filteredArray: [Int] = []
  4.     for arrayElement in array {
  5.         if condition(arrayElement) {
  6.             filteredArray.append(arrayElement)
  7.         }
  8.     }
  9.  
  10.     return filteredArray
  11. }
  12.  
  13. func firstFunc(number: Int) -> Bool {
  14.     return (number > 100) && (number < 200)
  15. }
  16.  
  17. let firstResult = filterArrray(array: someArray, condition: firstFunc)
  18. let secondResult = filterArrray(array: someArray, condition: { (element) -> (Bool) in
  19.     return element % 10 == 0
  20. })
  21. let thirdResult = filterArrray(array: someArray) { (element) -> (Bool) in
  22.     return element != 1
  23. }
  24.  
  25. print(firstResult)
  26. print(secondResult)
  27. print(thirdResult)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement