List

A singly-linked, lazy list. Head-tail decomposition can be accomplished with a switch statement:

extension List {
  public func map<T>(f: Element -> T) -> List<T> {
    switch self {
    case .Nil: return .Nil
    case let .Cons(x, xs): return f(x) |> xs().map(f)
    }
  }
}

Where |> is the cons operator.

Operations on the beginning of the list are O(1), whereas other operations are O(n).

Discussion of this specific implementation is available here.

Full documentation is available here.

  • Returns the next element if it exists, or nil if it does not.

    Declaration

    Swift

    public mutating func next() -> Element?
  • Construct from an arbitrary sequence with elements of type Element. If the underlying sequence is lazy, the list constructed will also be lazy. (i.e, the underlying sequence will not be evaluated.)

    Declaration

    Swift

    public init<S : SequenceType where S.Generator.Element == Element>(_ seq: S)
  • Create an instance containing elements.

    Declaration

    Swift

    public init(arrayLiteral elements: Element...)
  • A textual representation of self, suitable for debugging.

    Declaration

    Swift

    public var debugDescription: String
  • The number of elements in self

    • Complexity: O(count)

    Declaration

    Swift

    public var count: Int
  • Returns true iff self is empty.

    Declaration

    Swift

    public var isEmpty: Bool
  • Returns the first element of self, if it exists, or nil if self is empty.

    Declaration

    Swift

    public var first: Element?
  • Returns the last element of self, if it exists, or nil if self is empty.

    • Complexity: O(count)

    Declaration

    Swift

    public var last: Element?
  • Returns a List with with appended.

    • Complexity: O(count)

    Declaration

    Swift

    public func appended(@autoclosure(escaping) with: () -> Element) -> List<Element>
  • Returns a List extended by the elements of with.

    • Complexity: O(count)

    Declaration

    Swift

    public func extended(@autoclosure(escaping) with: () -> List<Element>) -> List<Element>
  • Returns a List extended by the elements of with.

    • Complexity: O(count)

    Declaration

    Swift

    public func extended<
        S : SequenceType where S.Generator.Element == Element
        >(@autoclosure(escaping) with: () -> S) -> List<Element>
  • Return self prepended with the elements of with.

    Declaration

    Swift

    public func prextended(with: List<Element>) -> List<Element>
  • Return self prepended with the elements of with.

    Declaration

    Swift

    public func prextended<
        S : SequenceType where S.Generator.Element == Element
        >(newElements: S) -> List<Element>
  • Returns a List containing all but the first N elements.

    • Complexity: O(n)

    Declaration

    Swift

    public func dropFirst(n: Int) -> List<Element>
  • Returns a List containing all but the last n elements.

    • Requires: n >= 0
    • Complexity: O(count)

    Declaration

    Swift

    public func dropLast(n: Int) -> List<Element>
  • Returns a List of the initial elements of self, up until the first element that returns false for isElement

    Declaration

    Swift

    public func prefixWhile(isElement: Element -> Bool) -> List<Element>
  • Returns a List of self with the first elements that satisfy isNotElement dropped.

    Declaration

    Swift

    public func dropWhile(@noescape isNotElement: Element -> Bool) -> List<Element>
  • Returns a List of the initial elements of self, of maximum length n.

    Declaration

    Swift

    public func prefix(n: Int) -> List<Element>
  • Returns a List of the final elements of self, of maximum length n.

    Declaration

    Swift

    public func suffix(n: Int) -> List<Element>
  • Returns the maximal Lists of self, in order, that don’t contain elements satisfying the predicate isSeparator.

    • Parameter maxSplits: The maximum number of Lists to return, minus 1. If maxSplit + 1 Lists are returned, the last one is a suffix of self containing the remaining elements. The default value is Int.max.
    • Parameter allowEmptySubsequences: If true, an empty List is produced in the result for each pair of consecutive elements satisfying isSeparator. The default value is false.
    • Requires: maxSplit >= 0

    Declaration

    Swift

    public func split(maxSplit: Int, allowEmptySlices: Bool, @noescape isSeparator: Element -> Bool) -> [List<Element>]
  • Remove the first element and return it.

    • Complexity: O(1)
    • Requires: !self.isEmpty.

    Declaration

    Swift

    public mutating func removeFirst() -> Element
  • Remove the first element and return it, if it exists. Otherwise, return nil.

    • Complexity: O(1)

    Declaration

    Swift

    public mutating func popFirst() -> Element?
  • Returns a List of the result of calling combine on successive elements of self

    let nums: List = [1, 2, 3]
    nums.scan(0, combine: +)
    // [1, 3, 6]
    

    Declaration

    Swift

    public func scan<T>(initial: T, combine: (accumulator: T, element: Element) -> T) -> List<T>
  • Returns a List of the result of calling combine on successive elements of self. Initial is taken to be the first element of self.

    let nums: List = [1, 2, 3]
    nums.scan(+)
    // [3, 6]
    

    Declaration

    Swift

    public func scan(combine: (accumulator: Element, element: Element) -> Element) -> List<Element>
  • Return the result of repeatedly calling combine with an initial value and each element of self, in turn, i.e. return combine(combine(…combine(combine(self[0], self[1]), self[2]),…self[count-2]), self[count-1]).

    [1, 2, 3].reduce(+) // 6
    

    Declaration

    Swift

    public func reduce<T>(initial: T, @noescape combine: (accumulator: T, element: Element) -> T) -> T
  • Return the result of repeatedly calling combine with an accumulated value initialized to the first element of self and each element of self, in turn, i.e. return combine(combine(…combine(combine(self[0], self[1]), self[2]),…self[count-2]), self[count-1]).

    [1, 2, 3].reduce(+) // 6
    

    Declaration

    Swift

    public func reduce(@noescape combine: (accumulator: Element, element: Element) -> Element) -> Element?
  • The same as the reduce function, except that combine is called on self in reverse, and the arguments are flipped.

    Declaration

    Swift

    public func reduceR<T>(initial: T, combine: (element: Element, accumulator: T) -> T) -> T
  • The same as the reduce function, except that combine is called on self in reverse, and the arguments are flipped. The initial agument to accumulator for combine is taken as the final element of self.

    Declaration

    Swift

    public func reduceR(combine: (element: Element, accumulator: Element) -> Element) -> Element?