Pebble Coding

ソフトウェアエンジニアによるIT技術、数学の備忘録

Arrayにおけるmap, flatMapの動作(swift3)

ググると引っかかるのか今だにswift2のドキュメントなので、swift3用のメモ

let a = [1, 2, 3]
let b = a.map { Array(repeating: $0, count: $0) }
print(b)
// [[1], [2, 2], [3, 3, 3]]

let c = [1, 2, 3]
let d = c.flatMap { Array(repeating: $0, count: $0) } 
// [1, 2, 2, 3, 3, 3]
print(d)

これはちゃんとメソッド通りの動作で安心。