The code to apply a GST to the array looks like the following:
let prices = [12.0,45.0,23.5,78.9,12.5] var pricesWithGST = prices.map ( {//from w ww . jav a2 s . c om (price:Double) -> Double in if price > 20 { return price * 1.07 } else { return price } } ) print(pricesWithGST)
Here, the closure accepts the price as the argument and returns a Double result.
Applying type inheritance and using the ternary operator, the code can now be reduced to this:
var pricesWithGST = prices.map ( {//ww w . j a v a 2s. c om (price) in price>20 ? price * 1.07 : price } ) let prices = [12.0,45.0,23.5,78.9,12.5] print(pricesWithGST)