Swift 3 – Using .map to walk through and create an array of items with offset

Sometimes I want to create a quick way to add a number of items with a name; such as creating 10 decks with an offset number.

Usually this can be done like this;

Code:

// usual way;
var decks : [Deck] = [Deck]()
for idx in 1...10 {
decks.append( Deck.init(name: "#\(idx)") )
}

But I find this functional way a bit better

Code:

// functional way
let decks = (1...10).enumerated().map({ (offset, element) in
return (Deck.init(name: "Deck.\(offset)"))
})

Although my usual way is memorable, I like the functional one as its a bit cleaner and uses less code.