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),

  Product(name: "Soccer Ball", description: "FIFA-approved size and weight",price: 19.5, stock: 32)];


func calculateTax(product:Product) -> Double {
  return product.price * 0.2;

}

//这个函数是什么意思?

func calculateStockValue(productsArray:[Product]) -> Double {
  return productsArray. reduce(0, {(total, product) -> Double in
     return total + (product.price * Double(product.stock))
});
}

你可能感兴趣的:(swift)