Sequence

protocol Sequence
  • Transforms the sequence into a dictionary grouped by the specified Key type.

    Example

    Given the following Person struct:

    struct Person {
      let firstName: String
      let lastName: String
    }
    

    This method allows to easily group a list of Person by their last name:

    let people = [Person(firstName: "Stephane", lastName: "Foo"), Person(firstName: "Leonty", lastName: "Bar"), Person(firstName: "Bastien", lastName: "Bar")]
    let groupedByLastNamesPeople = people.collate { $0.lastName }
    print(groupedByLastNamesPeople)
    

    This will output:

    [
      "Foo": [
        Person(firstName: "Stephane", lastName: "Foo"),
      ],
      "Bar": [
        Person(firstName: "Leonty", lastName: "Bar"),
        Person(firstName: "Bastien", lastName: "Bar"),
      ]
    ]
    

    Declaration

    Swift

    public func collate<Key>(_ key: (Iterator.Element) -> Key?) -> [Key : [Iterator.Element]] where Key : Hashable

    Parameters

    key

    The key to use for the given element of the sequence. If the key returned is nil, the element will be ignored and not be included in the result dictionary.

    Return Value

    The values in the sequence grouped by keys as specified in the key closure.

  • Split the sequence according to the given closure.

    The sequence i

    Complexity

    O(n), where n is the length of the sequence.

    Declaration

    Swift

    public func splitBetween(_ areSeparated: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> [[Iterator.Element]]

    Parameters

    areSeparated

    The closure used to separate the list. The closure takes 2 parameters, the first is the previous element and the second is the current element. If true is returned to the closure, all previous elements that weren’t added to the subsequence array are added to it.

    Return Value

    An array of subsequences, split according to the given closure.