RangeReplaceableCollection

public extension RangeReplaceableCollection
  • SwifterSwift: Creates a new collection of a given size where for each position of the collection the value will be the result of a call of the given expression.

    let values = Array(expression: "Value", count: 3)
    print(values)
    // Prints "["Value", "Value", "Value"]"
    

    Declaration

    Swift

    init(expression: @autoclosure () throws -> Element, count: Int) rethrows

    Parameters

    expression

    The expression to execute for each position of the collection.

    count

    The count of the collection.

Methods

  • SwifterSwift: Returns a new rotated collection by the given places.

    [1, 2, 3, 4].rotated(by: 1) -> [4,1,2,3]
    [1, 2, 3, 4].rotated(by: 3) -> [2,3,4,1]
    [1, 2, 3, 4].rotated(by: -1) -> [2,3,4,1]
    

    Declaration

    Swift

    func rotated(by places: Int) -> Self

    Parameters

    places

    Number of places that the array be rotated. If the value is positive the end becomes the start, if it negative it’s that start becom the end.

    Return Value

     The new rotated collection.

  • SwifterSwift: Rotate the collection by the given places.

    [1, 2, 3, 4].rotate(by: 1) -> [4,1,2,3]
    [1, 2, 3, 4].rotate(by: 3) -> [2,3,4,1]
    [1, 2, 3, 4].rotated(by: -1) -> [2,3,4,1]
    

    Declaration

    Swift

    @discardableResult
    mutating mutating func rotate(by places: Int) -> Self

    Parameters

    places

    The number of places that the array should be rotated. If the value is positive the end becomes the start, if it negative it’s that start become the end.

    Return Value

    self after rotating.

  • SwifterSwift: Removes the first element of the collection which satisfies the given predicate.

       [1, 2, 2, 3, 4, 2, 5].removeFirst { $0 % 2 == 0 } -> [1, 2, 3, 4, 2, 5]
       ["h", "e", "l", "l", "o"].removeFirst { $0 == "e" } -> ["h", "l", "l", "o"]
    

    Declaration

    Swift

    @discardableResult
    mutating mutating func removeFirst(where predicate: (Element) throws -> Bool) rethrows -> Element?

    Parameters

    predicate

    A closure that takes an element as its argument and returns a Boolean value that indicates whether the passed element represents a match.

    Return Value

    The first element for which predicate returns true, after removing it. If no elements in the collection satisfy the given predicate, returns nil.

  • SwifterSwift: Remove a random value from the collection.

    Declaration

    Swift

    @discardableResult
    mutating mutating func removeRandomElement() -> Element?
  • SwifterSwift: Keep elements of Array while condition is true.

       [0, 2, 4, 7].keep(while: { $0 % 2 == 0 }) -> [0, 2, 4]
    

    Throws

    provided condition exception.

    Declaration

    Swift

    @discardableResult
    mutating mutating func keep(while condition: (Element) throws -> Bool) rethrows -> Self

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    self after applying provided condition.

  • SwifterSwift: Take element of Array while condition is true.

       [0, 2, 4, 7, 6, 8].take( where: {$0 % 2 == 0}) -> [0, 2, 4]
    

    Declaration

    Swift

    func take(while condition: (Element) throws -> Bool) rethrows -> Self

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    All elements up until condition evaluates to false.

  • SwifterSwift: Skip elements of Array while condition is true.

       [0, 2, 4, 7, 6, 8].skip( where: {$0 % 2 == 0}) -> [6, 8]
    

    Declaration

    Swift

    func skip(while condition: (Element) throws -> Bool) rethrows -> Self

    Parameters

    condition

    condition to evaluate each element against.

    Return Value

    All elements after the condition evaluates to false.

  • SwifterSwift: Remove all duplicate elements using KeyPath to compare.

    Declaration

    Swift

    mutating mutating func removeDuplicates<E>(keyPath path: KeyPath<Element, E>) where E : Equatable

    Parameters

    path

    Key path to compare, the value must be Equatable.

  • SwifterSwift: Remove all duplicate elements using KeyPath to compare.

    Declaration

    Swift

    mutating mutating func removeDuplicates<E>(keyPath path: KeyPath<Element, E>) where E : Hashable

    Parameters

    path

    Key path to compare, the value must be Hashable.