I have a function func f() -> Int and I would like to call it n: Int times, and get the list of returned values.
In ruby, you would do n.times.collect { f }.
Does swift have a similar functional approach?
At the moment, I use the following handmade implementation:
extension Int {
func collect<T>(f: () -> T) -> [T] {
var l: [T] = []
for _ in 0..<self {
l.append(f())
}
return l
}
}
// Usage
let myList = 42.collect { UIView(frame: self.bounds) }