Skip to content

Commit

Permalink
Merge pull request #14 from RougeWare/feature/ElementalOperators-2
Browse files Browse the repository at this point in the history
Added a new elemental operator for prepending
  • Loading branch information
KyNorthstar authored Nov 25, 2022
2 parents 5ee6a70 + e86203f commit 1918c72
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import Foundation

public extension RangeReplaceableCollection {

/// Creates a new collection by concatenating the given elements onto the end of the collections.
/// Creates a new collection by concatenating the given element onto the end of the collection.
///
/// The first argument's `Element` type must be the same type as the second element. For example, you can
/// The first argument's `Element` subtype must be the same type as the second element. For example, you can
/// concatenate an integer onto an integer array.
///
/// ```swift
Expand All @@ -25,7 +25,7 @@ public extension RangeReplaceableCollection {
/// ```
///
/// The resulting collection has the type of the argument on the left-hand side. In the example above,
/// fibonacciUpTo8 has the same type as fibonacciUpTo5, which is `[Int]`.
/// `fibonacciUpTo8` has the same type as `fibonacciUpTo5`, which is `[Int]`.
///
/// - Parameters:
/// - collection: A range-replaceable collection.
Expand All @@ -39,7 +39,7 @@ public extension RangeReplaceableCollection {
/// Appends the given element to a range-replaceable collection.
///
/// Use this operator to append an element to the end of a range-replaceable collection whose elements are that
/// same `Element` type. This example appends the an `Int` to an array of integers.
/// same `Element` type. This example appends an `Int` to an array of integers:
///
/// ```swift
/// var fibonacci = [0, 1, 1, 2, 3, 5]
Expand All @@ -57,4 +57,28 @@ public extension RangeReplaceableCollection {
static func +=(_ collection: inout Self, _ newElement: Element) {
collection += [newElement]
}


/// Creates a new collection by prepending the given element onto the end of the collection.
///
/// The first argument's `Element` type must be the same type as the second element. For example, you can
/// prepend an integer onto an integer array.
///
/// ```swift
/// let fibonacciUpTo5 = [1, 1, 2, 3, 5]
/// let fibonacciUpTo5_withZero = 0 + fibonacciUpTo5
/// print(fibonacciUpTo5_withZero)
/// // Prints "[0, 1, 1, 2, 3, 5]"
/// ```
///
/// The resulting collection has the type of the argument on the right-hand side. In the example above,
/// `fibonacciUpTo5_withZero` has the same type as `fibonacciUpTo5`, which is `[Int]`.
///
/// - Parameters:
/// - newElement: An element to go at the beginning of the new collection
/// - collection: A range-replaceable collection.
@inline(__always)
static func +(_ newElement: Element, _ collection: Self) -> Self {
[newElement] + collection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import CollectionTools

final class RangeReplaceableCollection_plus_elemental_operator_Tests: XCTestCase {

func test_plus() {
func test_plusAppend() {
let fibonacciUpTo5 = [0, 1, 1, 2, 3, 5]
let fibonacciUpTo8 = fibonacciUpTo5 + 8
XCTAssertEqual(fibonacciUpTo5, [0, 1, 1, 2, 3, 5])
Expand All @@ -29,8 +29,17 @@ final class RangeReplaceableCollection_plus_elemental_operator_Tests: XCTestCase
}


func test_plusPrepend() {
let fibonacciUpTo5 = [1, 1, 2, 3, 5]
let fibonacciUpTo5_withZero = 0 + fibonacciUpTo5
XCTAssertEqual(fibonacciUpTo5, [1, 1, 2, 3, 5])
XCTAssertEqual(fibonacciUpTo5_withZero, [0, 1, 1, 2, 3, 5])
}


static var allTests = [
("test_plus", test_plus),
("test_plusAppend", test_plusAppend),
("test_plusPrepend", test_plusPrepend),
("test_plusEquals", test_plusEquals),
]
}

0 comments on commit 1918c72

Please sign in to comment.