Skip to content

Instantly share code, notes, and snippets.

@DarrenHurst
Created February 11, 2023 17:30
Show Gist options
  • Save DarrenHurst/8dec5882f88b6e0fd546a662b0854971 to your computer and use it in GitHub Desktop.
Save DarrenHurst/8dec5882f88b6e0fd546a662b0854971 to your computer and use it in GitHub Desktop.
Find the lowest priced hotdog
import UIKit
struct Item: Equatable {
var name: String?
var price: Double?
}
class a : ObservableObject {
@Published var items: [Item] = [
Item(name: "hotdogs", price:3.99),
Item(name: "hotdogs", price:2.99),
Item(name: "hotdogs", price:2.99),
Item(name: "hotdogs", price:4.99),
Item(name: "cheese", price:3.99),
Item(name: "cheese", price:2.99),
Item(name: "cheese", price:4.99),
Item(name: "milk", price:5.99),
Item(name: "milk", price:2.99)
]
init(){
}
func Test() {
var ar: [Item] = []
let searchItem: String = "hotdogs"
// You could do this.. but.
for item in items {
if item.name == searchItem {
ar.append(item)
}
}
//reduce with the filter on the reference - less expensive
let results = items.filter { Item in
Item.name == searchItem
}
print(results)
//find the lowest price
var lowestHotdogPrice: Double = 0.0
for hotdog: Item in results {
if (lowestHotdogPrice == 0.0) {
lowestHotdogPrice = hotdog.price ?? 0.0
}
if hotdog.price ?? 0.00 <= lowestHotdogPrice {
lowestHotdogPrice = hotdog.price ?? 0.0
}
}
//return all objects with lowestPrice
let prices:[Item] = results.filter{ $0.price?.isLessThanOrEqualTo(lowestHotdogPrice) ?? false }.map{ $0 }
print("matches for lowest price is: \(prices)")
print(" had \(ar.count) hotdogs, \(lowestHotdogPrice) was the lowest")
}
}
let x: a = a()
x.Test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment