Last active
September 26, 2017 00:39
-
-
Save glowcap/15062f7363da5bcad0c162c9d5faeee4 to your computer and use it in GitHub Desktop.
//: Filter, Map, Reduce - Interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Filter, Map, Reduce - Interview question | |
| import UIKit | |
| // Our Skate shop just got a big order of wheels delivered | |
| // We enter the wheels into the system like this: | |
| struct Wheel { | |
| let brand: String | |
| let color: String | |
| let size: Int | |
| var price: Double | |
| } | |
| // Today's shipment included: | |
| var Stacked = Wheel(brand: "Baker", color: "orange", size: 52, price: 24.99) | |
| var OneHundreds = Wheel(brand: "Bones", color: "white", size: 54, price: 23.99) | |
| var Haslam = Wheel(brand: "Bones", color: "white", size: 52, price: 32.99) | |
| var Party = Wheel(brand: "Bones", color: "multi", size: 53, price: 23.99) | |
| var HardBoiled = Wheel(brand: "OJ", color: "orange", size: 54, price: 29.99) | |
| var BlackCats = Wheel(brand: "OJ", color: "black", size: 58, price: 29.99) | |
| var HouseBrand = Wheel(brand: "HouseBrand", color: "white", size: 50, price: 14.99) | |
| var Classic = Wheel(brand: "Spitfire", color: "white", size: 56, price: 32.99) | |
| var BigSwirl = Wheel(brand: "Spitfire", color: "multi", size: 51, price: 26.99) | |
| // One of the shop guys put them in the case | |
| var allWheels = [ | |
| Stacked, OneHundreds, Haslam, Party, HardBoiled, | |
| BlackCats, HouseBrand, Classic, BigSwirl | |
| ] | |
| //MARK: - Filter | |
| // A lot has changed in how we operate things in our shop. | |
| // For example, when a team rider needed wheels for a tour. | |
| // The riders usually stick to one size of wheel, so we | |
| // would pick them out like this: | |
| func getRiderWheels2017(size: Int) -> [Wheel] { | |
| var wheels = [Wheel]() | |
| for wheel in allWheels { | |
| if wheel.size == size { | |
| wheels.append(wheel) | |
| } | |
| } | |
| return wheels | |
| } | |
| // It worked but it wasn't really efficient, | |
| // so we decided to do it another way: | |
| func getRiderWheels2018(size: Int) -> [Wheel] { | |
| return allWheels.filter { $0.size == size } | |
| } | |
| // This saved us a lot of time...which is awesome! | |
| // MARK: - Map | |
| // We also have sales on wheels in the summer. | |
| // Last summer the staff marked down the prices like this: | |
| func markDownPrices2017(by percentage: Double) { | |
| var saleWheels = [Wheel]() | |
| for wheel in allWheels { | |
| var wheel = wheel | |
| wheel.price = wheel.price - ((percentage / 100) * wheel.price) | |
| saleWheels.append(wheel) | |
| } | |
| allWheels = saleWheels | |
| } | |
| // This worked. But again, it was a lot | |
| // of extra work. We decided to try something | |
| // new this summer | |
| func markDownPrices2018(by percentage: Double) { | |
| allWheels = allWheels.map { wheel in | |
| let price = wheel.price - ((percentage / 100) * wheel.price) | |
| return Wheel(brand: wheel.brand, color: wheel.color, size: wheel.size, price: price) | |
| } | |
| } | |
| // It may seem like the same amount of work, but | |
| // we never created `saleWheels`. That means the | |
| // wheels could stay in the case without being moved. | |
| // Pretty sweet. | |
| // MARK: - Reduce | |
| // Like every business we have to audit our inventory. | |
| // Last year, it took forever doing it this way: | |
| func totalWheelInventoryValue2017() -> Double { | |
| var totalValue: Double = 0.0 | |
| for wheel in allWheels { | |
| totalValue += wheel.price | |
| } | |
| return totalValue | |
| } | |
| // It was a nightmare. Fortunately, we came up | |
| // with a new way to do things using our new skills. | |
| func totalWheelInventoryValue2018() -> Double { | |
| let wheelPrices = allWheels.map { return $0.price } | |
| return wheelPrices.reduce(0, +) | |
| } | |
| // As you can see we really "reduced" the amount | |
| // of work involved. ;) | |
| // So that's how our skate shop is doing business | |
| // nowadays. We have really improved the efficiency | |
| // so now we have more time to watch skate videos! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment