Open popeyelau opened 5 years ago
//Dictionary
let citys = ["Shanghai": 200, "Shenzhen": 300, "Guangzhou": 400]
print(citys.filter { $0.value > 200}) //["Shenzhen": 300, "Guangzhou": 400]
print(citys.mapValues { $0 * 2 }) //["Shanghai": 400, "Shenzhen": 600, "Guangzhou": 800]
//grouping
let grouped = Dictionary(grouping: citys.keys) { $0.first!}
print(grouped) //["S": ["Shanghai", "Shenzhen"], "G": ["Guangzhou"]]
// instead of citys["Hongkong"] ?? 500
let value = citys["Hongkong", default: 500]
print(value) //500
//One-sided ranges
let nums = [0,1,2,3,4,5]
let bigParts = nums[..<3] //0, 1, 2
let smallParts = nums[3...] //3, 4, 5
print(bigParts)
print(smallParts)
//random
let randomInt = Int.random(in: 1..<10)
print(randomInt)
//Boolean toggle
var isMarked = false
isMarked.toggle() //true