Skip to content

Instantly share code, notes, and snippets.

@tomdiggle
Last active November 14, 2016 15:24
Show Gist options
  • Save tomdiggle/d7ddb54730e55045ec9dc2a3b20cbe44 to your computer and use it in GitHub Desktop.
Save tomdiggle/d7ddb54730e55045ec9dc2a3b20cbe44 to your computer and use it in GitHub Desktop.
Appends an element to an array n times
// Tested in Swift 3
mutating func appendN(element: Element, n: Int) {
guard n > 0 else {
return
}
for _ in 0..<n {
self.append(element)
}
}
// Usage
var array = ["Bob", "Steve", "Barry"]
array.appendN(element: "John", n: 2)
// ["Bob", "Steve", "Barry", "John", "John"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment