swift 一疑问:reduce
var products = [
Product(name: "Kayak", description: "A boat for one person",price: 275, stock: 10),
Product(name: "Lifejacket", description: "Protective and fashionable",price: 48.95, stock: 14),
return product.price * 0.2;
return productsArray.reduce(0, {(total, product) -> Double in
return total + (product.price * Double(product.stock))
});
}
Product(name: "Kayak", description: "A boat for one person",price: 275, stock: 10),
Product(name: "Lifejacket", description: "Protective and fashionable",price: 48.95, stock: 14),
Product(name: "Soccer Ball", description: "FIFA-approved size and weight",price: 19.5, stock: 32)];
return product.price * 0.2;
}
//这个函数是什么意思?
return productsArray.reduce(0, {(total, product) -> Double in
return total + (product.price * Double(product.stock))
});
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步