@@ -12,9 +12,9 @@ import Foundation
1212
1313public 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}
0 commit comments