-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to make elements in an array unique
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
Sources/AppFoundations/Extensions/Array/Array+Modifying.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Mark DiFranco on 2023-09-04. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension Array { | ||
|
||
/// Returns a new array with the provided `element` inserted. | ||
/// - parameter element: The element to insert. | ||
/// - returns: A new array with the provided `element` inserted. | ||
func inserting(_ element: Element) -> Self { | ||
self + [element] | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/AppFoundations/Extensions/Array/Array+Unique.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Array+Unique.swift | ||
// | ||
// | ||
// Created by Mark DiFranco on 2023-09-04. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension Array where Element: Equatable { | ||
|
||
mutating func madeUnique() { | ||
self = makingUnique() | ||
} | ||
|
||
func makingUnique() -> Self { | ||
reduce([]) { (partialResult, element) in | ||
if partialResult.contains(element) { | ||
return partialResult | ||
} | ||
return partialResult.inserting(element) | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
Tests/AppFoundationsTests/Extensions/Array/Array+Unique.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Array+Unique.swift | ||
// | ||
// | ||
// Created by Mark DiFranco on 2023-09-04. | ||
// | ||
|
||
import XCTest | ||
|
||
final class Array_Unique: XCTestCase { | ||
|
||
var sut = [String]() | ||
|
||
func test_madeUnique_returnsUniqueElements() { | ||
// Arrange | ||
sut = ["One", "One", "Two", "One", "Three"] | ||
let expectedResult = ["One", "Two", "Three"] | ||
|
||
// Act | ||
sut.madeUnique() | ||
|
||
// Assert | ||
XCTAssertEqual(sut, expectedResult) | ||
} | ||
|
||
func test_makingUnique_returnsUniqueElements() { | ||
// Arrange | ||
sut = ["One", "One", "Two", "One", "Three"] | ||
let expectedResult = ["One", "Two", "Three"] | ||
|
||
// Act | ||
let result = sut.makingUnique() | ||
|
||
// Assert | ||
XCTAssertEqual(result, expectedResult) | ||
} | ||
|
||
func test_makingUnique_performance() { | ||
// Arrange | ||
sut = Array(repeating: "Hello World", count: 100_000) | ||
let expectedResult = ["Hello World"] | ||
|
||
// Act | ||
self.measure { | ||
let result = sut.makingUnique() | ||
|
||
// Assert | ||
XCTAssertEqual(result, expectedResult) | ||
} | ||
} | ||
} |