extension Collection {
func flatMapped<T>(with type: T.Type? = nil) -> [T] {
return flatMap { ($0 as? [Any])?.flatMapped() ?? ($0 as? T).map { [$0] } ?? [] }
}
}
let array: [Any] = [[1, 2, nil],3]
// Use the syntax of your choice
let flattened1: [Int] = array.flatMapped() // [1, 2, 3]
let flattened2 = array.flatMapped(with: Int.self) // [1, 2, 3]
let flattened3 = array.flatMapped() as [Int] // [1, 2, 3]