Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array.init(numberOfElements:,element:) initializer #1051

Merged
merged 7 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
### Added
- **UIButton**
- Added `setBackgroundColor(_:for:)` method for setting background color for the specified UI state. [#1041](https://github.com/SwifterSwift/SwifterSwift/pull/1041) by [TwizzyIndy](https://github.com/TwizzyIndy)

- **Array**
- Added `init(numberOfElements:element:)` initializer for creating an array of a given size with a closure. [#1051](https://github.com/SwifterSwift/SwifterSwift/pull/1051) by [viktart](https://github.com/viktart)
viktart marked this conversation as resolved.
Show resolved Hide resolved
### Fixed
- **Digest**
- `DigestExtensions.swift` would not compile on Xcode 14 due to an `ambiguous use of 'makeIterator()'` error. [#1042](https://github.com/SwifterSwift/SwifterSwift/issues/1042) by [theedov](https://github.com/theedov)
Expand Down
19 changes: 19 additions & 0 deletions Sources/SwifterSwift/SwiftStdlib/ArrayExtensions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// ArrayExtensions.swift - Copyright 2020 SwifterSwift

// MARK: - Initializers

public extension Array {
/// SwifterSwift: Creates an array with specified number of elements, for each element it calls specified closure.
/// - Parameters:
/// - numberOfElements: The number of elements in the new array.
viktart marked this conversation as resolved.
Show resolved Hide resolved
/// - element: A closure that initializes each element.
/// - Parameter *index*: An index of initialized element in the array.
/// - Returns: element of the array.
init(count: Int, element: (Int) -> Element) {
viktart marked this conversation as resolved.
Show resolved Hide resolved
self.init(unsafeUninitializedCapacity: count) { buffer, initializedCount in
for index in 0..<count {
buffer.baseAddress?.advanced(by: index).initialize(to: element(index))
}
initializedCount = count
}
}
}

// MARK: - Methods

public extension Array {
Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftStdlibTests/ArrayExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
import XCTest

final class ArrayExtensionsTests: XCTestCase {
func testInitClosure() {
let squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
let array = [Int](count: 11) { $0 * $0 }
XCTAssertEqual(squares, array)
}

func testPrepend() {
var arr = [2, 3, 4, 5]
arr.prepend(1)
Expand Down