Swift - Map array element to new value with closure

Introduction

The code to apply a GST to the array looks like the following:

Demo

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)

Result

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:

Demo

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)

Related Example