├── HigherOrderFunctionsSwift.playground ├── playground.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcuserdata │ │ └── roxanajula.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist ├── contents.xcplayground └── Contents.swift └── LICENSE /HigherOrderFunctionsSwift.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /HigherOrderFunctionsSwift.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /HigherOrderFunctionsSwift.playground/playground.xcworkspace/xcuserdata/roxanajula.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ml-archive/higher-order-functions-swift/master/HigherOrderFunctionsSwift.playground/playground.xcworkspace/xcuserdata/roxanajula.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /HigherOrderFunctionsSwift.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Nodes - iOS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /HigherOrderFunctionsSwift.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Higher Order Functions in Swift 3 | // https://engineering.nodesagency.com/ 4 | // By Roxana Jula and Nicolai Harbo on 03/07/2019. 5 | // 6 | 7 | import UIKit 8 | 9 | // TEST DATA 10 | let meters = [10.0, 22.0, 55.0, 74.0] 11 | let numbers = [5, 3, 2, 6, 10, 23, 01, 43, 5, 7, 8, 9] 12 | let planetNames = ["mars", "jupiter", "mercury", "saturn", "earth", "neptune", "uranus", "venus"] 13 | let names = [["roxana", "peter", "jacob", "morten"],["iben", "nour", "nicolai"]] 14 | let scores = ["1", "2", "three", "four", "5"] 15 | 16 | class Address { 17 | var street: String 18 | var zipcode: Int 19 | 20 | init(street: String, zipcode: Int) { 21 | self.street = street 22 | self.zipcode = zipcode 23 | } 24 | } 25 | 26 | var addresses = [Address]() 27 | addresses.append(Address(street: "Nice Boulevard", zipcode: 1200)) 28 | addresses.append(Address(street: "Green Street", zipcode: 4560)) 29 | 30 | 31 | // MAP 32 | 33 | // Example 1: Convert Meters to Feet 34 | let feet = meters.map { $0 * 3.281} 35 | print("Meters converted to feet: \(feet)") 36 | 37 | // Example 2: Make the planet names capitalized 38 | let capitalizedPlanetNames = planetNames.map { $0.capitalized } 39 | print("Planet names capitalized: \(capitalizedPlanetNames)") 40 | 41 | //Example 3: Map the array of addresses to an array of zipcodes 42 | let zipcodes = addresses.map { $0.zipcode } 43 | print("Zip codes: \(zipcodes)") 44 | 45 | 46 | // FILTER 47 | 48 | // Example 1: Filter only the planets that start with the letter "M" 49 | let filteredPlanetNames = planetNames.filter {$0.prefix(1).uppercased() == "M"} 50 | print("Count of filtered planet names: \(filteredPlanetNames.count)") 51 | 52 | // Example 2: Filter the address array to only addresses from zipcode 1200 53 | let filteredAddresses = addresses.filter {$0.zipcode == 1200} 54 | print("Count of filtered addresses: \(filteredAddresses.count)") 55 | 56 | 57 | // REDUCE 58 | 59 | // Example 1: Sum of numbers - version 1 60 | let sumOfNumbers = numbers.reduce(0, {$0 + $1}) 61 | print("Sum of numbers - version 1: \(sumOfNumbers)") 62 | 63 | // Example 2: Sum of numbers - version 2 64 | let sumOfNumbers2 = numbers.reduce(0,+) 65 | print("Sum of numbers - version 2: \(sumOfNumbers)") 66 | 67 | // Example 3: Longest planet name 68 | let longestPlanetName = planetNames.reduce("", {$0.count > $1.count ? $0 : $1 } ) 69 | print("Longest planet name: \(longestPlanetName)") 70 | 71 | // SORTED 72 | 73 | // Example 1: Sorting numbers ascending 74 | let sortedNumbersAscending = numbers.sorted() 75 | print("Sorted numbers ascending: \(sortedNumbersAscending)") 76 | 77 | //Example 2: Sorted numbers descending - version 1 78 | let sortedNumbersDescending = numbers.sorted { (a, b) -> Bool in 79 | a > b 80 | } 81 | print("Sorted numbers descending - version 1: \(sortedNumbersDescending)") 82 | 83 | //Example 3: Sorted numbers descending - version 2 84 | let sortedNumbersDescending2 = numbers.sorted{($0 > $1)} 85 | print("Sorted numbers descending - version 2: \(sortedNumbersDescending2)") 86 | 87 | //Example 4: Sorted numbers descending - version 3 88 | let sortedNumbersDescending3 = numbers.sorted(by: >) 89 | print("Sorted numbers descending - version 3: \(sortedNumbersDescending3)") 90 | 91 | 92 | // FLATMAP 93 | 94 | // Example 1: Flatmap without optionals 95 | let flatNames = names.flatMap({$0.sorted()}) 96 | print("Flatmap of names sorted: \(flatNames)") 97 | 98 | //Example 2: Flat Flatmap without optionals 99 | let flatFlatNames = flatNames.flatMap({$0.sorted()}) 100 | print("Flat Flatmap of names sorted: \(flatFlatNames)") 101 | 102 | //Example 3: Flat with optionals 103 | let flatMapNumbers: [Int?] = scores.flatMap { str in Int(str) } 104 | print("Flatmap numbers: \(flatMapNumbers)") 105 | 106 | 107 | // COMPACTMAP 108 | 109 | // Example 1: Compact map with optionals 110 | let compactMapped: [Int?] = scores.compactMap { str in Int(str) } 111 | print("Compact map with optionals: \(compactMapped)") 112 | 113 | //Example 2: Compact map without optionals 114 | let compactMapNumbers: [Int] = scores.compactMap { str in Int(str) } 115 | print("Compact map without optionals: \(compactMapNumbers)") 116 | 117 | 118 | // CHAINING 119 | 120 | // Example 1: An array of street names from a specific zipcode 121 | let streetNamesFromZipcode = addresses.filter {$0.zipcode == 1200}.map {$0.street} 122 | print("Street names from specific zipcode: \(streetNamesFromZipcode)") 123 | 124 | // Example 2: An array of sorted capitalized planet names 125 | let sortedCapitalizedPlanetNames = planetNames.map { $0.capitalized }.sorted() 126 | print("Sorted capitalized plane names: \(sortedCapitalizedPlanetNames)") 127 | 128 | // Example 3: Names flat map sorted descending 129 | let descendingFlatNames = names.flatMap({$0.sorted{$0 > $1}}) 130 | print("Descending sorted flat map names: \(descendingFlatNames)") 131 | 132 | 133 | --------------------------------------------------------------------------------