Guest User

Untitled

a guest
Oct 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. import UIKit
  2. // *********************************
  3. // ************** MAP **************
  4. // *********************************
  5. // Классический вариант
  6. func lengthOf(strings: [String]) -> [Int] {
  7.  
  8. var result = [Int]()
  9.  
  10. for string in strings {
  11. result.append(string.count)
  12. }
  13.  
  14. return result
  15. }
  16.  
  17. // Функциональный вариант
  18. func flengthOf(strings: [String]) -> [Int] {
  19. return strings.map { $0.count }
  20. }
  21.  
  22. let fruits = ["Apple", "Cherry", "Orange", "Pineapple"]
  23. let upperFruits = fruits.map { $0.uppercased() } // ["APPLE", "CHERRY", "ORANGE", "PINEAPPLE"]
  24.  
  25. let scores = [100, 80, 85]
  26. let formatted = scores.map { "Your score was \($0)" } // ["Your score was 100", "Your score was 80", "Your score was 85"]
  27. let passOfFail = scores.map { $0 > 85 ? "Pass" : "Fail" } // ["Pass", "Fail", "Fail"]
  28.  
  29. let position = [50, 60, 40]
  30. let averageResults = position.map { 45...55 ~= $0 ? "Within average" : "Outside average"} // ["Within average", "Outside average", "Outside average"]
  31.  
  32. let numbers: [Double] = [4, 9, 25, 36, 49]
  33. let result = numbers.map(sqrt) // [2, 3, 5, 6, 7]
  34.  
  35. // *********************************
  36. // *********** FLATMAP *************
  37. // *********************************
  38.  
  39. let numbers = [[1, 2], [3, 4], [5, 6]]
  40. let joined = Array(numbers.joined()) // [1, 2, 3, 4, 5, 6]
  41. print(joined)
  42.  
  43. let albums: [String?] = ["Fearless", nil, "Speak Now", nil, "Red"]
  44. let result = albums.map { $0 } // [Optional("Fearless"), nil, Optional("Speak Now"), nil, Optional("Red")]
  45. let flat_result = albums.flatMap { $0 } // ["Fearless", "Speak Now", "Red"]
  46. let compact_result = albums.compactMap { $0 } // ["Fearless", "Speak Now", "Red"]
  47. print(compact_result)
  48.  
  49. let scores = ["100", "90", "Fish", "85"]
  50. let flatMapScores = scores.flatMap { Int($0) } // [100, 90, 85]
  51. let compactMapScores = scores.flatMap { Int($0) } // [100, 90, 85]
  52. print(compactMapScores)
  53.  
  54. let flatMapFiles = (1...10).flatMap({ try? String(contentsOfFile: "someFile-\($0).txt") }) // []
  55. let compactMapFiles = (1...10).compactMap({ try? String(contentsOfFile: "someFile-\($0).txt") }) // []
  56. print(compactMapFiles)
  57.  
  58. let view = UIView()
  59. let flatMaplabels = view.subviews.flatMap({ $0 as? UILabel }) // []
  60. let compactMaplabels = view.subviews.compactMap({ $0 as? UILabel }) // []
  61.  
  62. // *********************************
  63. // ************ FILTER *************
  64. // *********************************
  65.  
  66. let fibonacciNumbers = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
  67. let evenFibonacci = fibonacciNumbers.filter { $0 % 2 == 0 } // [2, 8, 34]
  68.  
  69. let names = ["Michael Jackson", "Taylor Swift", "Michael Caine", "Adele Adkins", "Michael Jordan"]
  70. let result = names.filter({ $0.hasPrefix("Michael") }) // ["Michael Jackson", "Michael Caine", "Michael Jordan"]
  71.  
  72. let words = ["1989", "Fearless", "Red"]
  73. let input = "My favorite album is Fearless"
  74. let wordsResult = words.filter({ input.contains($0) }) // ["Fearless"]
  75.  
  76. let optionalWords: [String?] = ["1989", nil, "Fearless", nil, "Red"]
  77. let optionalWordsResult = optionalWords.filter({ $0 != nil }) // [Optional("1989"), Optional("Fearless"), Optional("Red")]
  78. print(optionalWordsResult)
  79.  
  80. // *********************************
  81. // ************ REDUCE *************
  82. // *********************************
  83.  
  84. let scores = [100, 90, 95]
  85. let sum = scores.reduce(0, +) // 285
  86.  
  87. let result = scores.reduce("") { $0 + String($1) } // 1009095
  88.  
  89. let names = ["Taylor", "Paul", "Adele"]
  90. let count = names.reduce(0) { $0 + $1.count } // 15
  91. let longest = names.reduce("") { $1.count > $0.count ? $1 : $0 } // Taylor
  92. let anotherLongest = names.max { $1.count > $0.count } // Optional("Taylor")
  93.  
  94. let numbers = [
  95. [1, 1, 2],
  96. [3, 5, 8],
  97. [13, 21, 34]
  98. ]
  99.  
  100. let flattened: [Int] = numbers.reduce([]) { $0 + $1 } // [1, 1, 2, 3, 5, 8, 13, 21, 34]
  101. let flattened2 = numbers.flatMap({ $0 }) // [1, 1, 2, 3, 5, 8, 13, 21, 34]
  102. let flattened3 = Array(numbers.joined()) // [1, 1, 2, 3, 5, 8, 13, 21, 34]
  103.  
  104. // *********************************
  105. // ********* COMPOSITION ***********
  106. // *********************************
  107.  
  108. let london = (name: "London", continent: "Europe", population: 8_539_000)
  109. let paris = (name: "Paris", continent: "Europe", population: 2_244_000)
  110. let lisbon = (name: "Lisbon", continent: "Europe", population: 530_000)
  111. let rome = (name: "Rome", continent: "Europe", population: 2_627_000)
  112. let tokyo = (name: "Tokyo", continent: "Asia", population: 13_350_000)
  113.  
  114. let cities = [london, paris, lisbon, rome, tokyo]
  115.  
  116. let totalPopulation = cities.reduce(0) { $0 + $1.population } // 27290000
  117. let europePopulation = cities.filter({ $0.continent == "Europe" }).reduce(0) { $0 + $1.population } // 13940000
  118. let biggestCities = cities.filter({ $0.population > 2_000_000 }).sorted(by: { $0.population > $1.population }).prefix(upTo: 3).map { "\($0.name) is a big city. with a population of \($0.population)" }.joined(separator: "\n")
  119. /*
  120. Tokyo is a big city. with a population of 13350000
  121. London is a big city. with a population of 8539000
  122. Rome is a big city. with a population of 2627000
  123.  
  124. */
  125.  
  126. precedencegroup CompositionPrecedence {
  127. associativity: left
  128. }
  129.  
  130. infix operator >>>: CompositionPrecedence
  131.  
  132. func >>> <T, U, V>(lhs: @escaping (T) -> U, rhs: @escaping (U) -> V) -> (T) -> (V) {
  133. return { rhs(lhs($0)) }
  134. }
  135.  
  136. //func >>> (lhs: @escaping (Int) -> String, rhs: @escaping (String) -> [String]) -> (Int) -> [String] {
  137. // return { rhs(lhs($0)) }
  138. //}
  139.  
  140.  
  141. func generateRandomNumber(max: Int) -> Int {
  142.  
  143. let number = Int(arc4random_uniform(UInt32(max)))
  144. print("Using number: \(number)")
  145.  
  146. return number
  147. }
  148.  
  149. func calculateFactors(number: Int) -> [Int] {
  150. return (1...number).filter { number % $0 == 0 }
  151. }
  152.  
  153. func reduceToString(numbers: [Int]) -> String {
  154. return numbers.reduce("Factors: ") { $0 + String($1) + " " }
  155. }
  156.  
  157. let result = reduceToString(numbers: calculateFactors(number: generateRandomNumber(max: 100)))
  158. print(result)
  159.  
  160. let combined = generateRandomNumber >>> calculateFactors >>> reduceToString
  161. print(combined(100))
Add Comment
Please, Sign In to add comment