Created
December 24, 2016 08:05
-
-
Save yossan/845a9bcc942b8813e0831ea3a151c05c to your computer and use it in GitHub Desktop.
[swift3] Array Extesions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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