Skip to content

Commit 1918c72

Browse files
authored
Merge pull request #14 from RougeWare/feature/ElementalOperators-2
Added a new elemental operator for prepending
2 parents 5ee6a70 + e86203f commit 1918c72

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

Sources/CollectionTools/RangeReplaceableCollection + elemental operators.swift

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import Foundation
1212

1313
public extension RangeReplaceableCollection {
1414

15-
/// Creates a new collection by concatenating the given elements onto the end of the collections.
15+
/// Creates a new collection by concatenating the given element onto the end of the collection.
1616
///
17-
/// The first argument's `Element` type must be the same type as the second element. For example, you can
17+
/// The first argument's `Element` subtype must be the same type as the second element. For example, you can
1818
/// concatenate an integer onto an integer array.
1919
///
2020
/// ```swift
@@ -25,7 +25,7 @@ public extension RangeReplaceableCollection {
2525
/// ```
2626
///
2727
/// The resulting collection has the type of the argument on the left-hand side. In the example above,
28-
/// fibonacciUpTo8 has the same type as fibonacciUpTo5, which is `[Int]`.
28+
/// `fibonacciUpTo8` has the same type as `fibonacciUpTo5`, which is `[Int]`.
2929
///
3030
/// - Parameters:
3131
/// - collection: A range-replaceable collection.
@@ -39,7 +39,7 @@ public extension RangeReplaceableCollection {
3939
/// Appends the given element to a range-replaceable collection.
4040
///
4141
/// Use this operator to append an element to the end of a range-replaceable collection whose elements are that
42-
/// same `Element` type. This example appends the an `Int` to an array of integers.
42+
/// same `Element` type. This example appends an `Int` to an array of integers:
4343
///
4444
/// ```swift
4545
/// var fibonacci = [0, 1, 1, 2, 3, 5]
@@ -57,4 +57,28 @@ public extension RangeReplaceableCollection {
5757
static func +=(_ collection: inout Self, _ newElement: Element) {
5858
collection += [newElement]
5959
}
60+
61+
62+
/// Creates a new collection by prepending the given element onto the end of the collection.
63+
///
64+
/// The first argument's `Element` type must be the same type as the second element. For example, you can
65+
/// prepend an integer onto an integer array.
66+
///
67+
/// ```swift
68+
/// let fibonacciUpTo5 = [1, 1, 2, 3, 5]
69+
/// let fibonacciUpTo5_withZero = 0 + fibonacciUpTo5
70+
/// print(fibonacciUpTo5_withZero)
71+
/// // Prints "[0, 1, 1, 2, 3, 5]"
72+
/// ```
73+
///
74+
/// The resulting collection has the type of the argument on the right-hand side. In the example above,
75+
/// `fibonacciUpTo5_withZero` has the same type as `fibonacciUpTo5`, which is `[Int]`.
76+
///
77+
/// - Parameters:
78+
/// - newElement: An element to go at the beginning of the new collection
79+
/// - collection: A range-replaceable collection.
80+
@inline(__always)
81+
static func +(_ newElement: Element, _ collection: Self) -> Self {
82+
[newElement] + collection
83+
}
6084
}

Tests/CollectionToolsTests/RangeReplaceableCollection + elemental operator Tests.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import CollectionTools
1313

1414
final class RangeReplaceableCollection_plus_elemental_operator_Tests: XCTestCase {
1515

16-
func test_plus() {
16+
func test_plusAppend() {
1717
let fibonacciUpTo5 = [0, 1, 1, 2, 3, 5]
1818
let fibonacciUpTo8 = fibonacciUpTo5 + 8
1919
XCTAssertEqual(fibonacciUpTo5, [0, 1, 1, 2, 3, 5])
@@ -29,8 +29,17 @@ final class RangeReplaceableCollection_plus_elemental_operator_Tests: XCTestCase
2929
}
3030

3131

32+
func test_plusPrepend() {
33+
let fibonacciUpTo5 = [1, 1, 2, 3, 5]
34+
let fibonacciUpTo5_withZero = 0 + fibonacciUpTo5
35+
XCTAssertEqual(fibonacciUpTo5, [1, 1, 2, 3, 5])
36+
XCTAssertEqual(fibonacciUpTo5_withZero, [0, 1, 1, 2, 3, 5])
37+
}
38+
39+
3240
static var allTests = [
33-
("test_plus", test_plus),
41+
("test_plusAppend", test_plusAppend),
42+
("test_plusPrepend", test_plusPrepend),
3443
("test_plusEquals", test_plusEquals),
3544
]
3645
}

0 commit comments

Comments
 (0)