Skip to content

Instantly share code, notes, and snippets.

@yossan
Created December 24, 2016 08:05
Show Gist options
  • Save yossan/845a9bcc942b8813e0831ea3a151c05c to your computer and use it in GitHub Desktop.
Save yossan/845a9bcc942b8813e0831ea3a151c05c to your computer and use it in GitHub Desktop.
[swift3] Array Extesions
extension Array {
/**
Create a new array containing the generating element.
- parameter generator: The Function to generate element.
- parameter count: The number of times to repeat the value passed in the repeatingFunc parameter.
*/
init(repeatingFunc generator: () -> Element, count: Int) {
self.init()
for _ in 0..<count {
self.append(generator())
}
}
}
// Example of use
import Foundation
func genInteger() -> Int {
return Int(arc4random()) % 100
}
let arr = Array(repeatingFunc: genInteger, count: 5)
print(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment