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
- Complexity: O(
-
Returns
true
iffself
is empty.Declaration
Swift
public var isEmpty: Bool
-
Returns the first element of
self
, if it exists, ornil
ifself
is empty.Declaration
Swift
public var first: Element?
-
Returns the last element of
self
, if it exists, ornil
ifself
is empty.- Complexity: O(
count
)
Declaration
Swift
public var last: Element?
- Complexity: O(
-
Returns a
List
withwith
appended.- Complexity: O(
count
)
Declaration
Swift
public func appended(@autoclosure(escaping) with: () -> Element) -> List<Element>
- Complexity: O(
-
Returns a
List
extended by the elements ofwith
.- Complexity: O(
count
)
Declaration
Swift
public func extended(@autoclosure(escaping) with: () -> List<Element>) -> List<Element>
- Complexity: O(
-
Returns a
List
extended by the elements ofwith
.- Complexity: O(
count
)
Declaration
Swift
public func extended< S : SequenceType where S.Generator.Element == Element >(@autoclosure(escaping) with: () -> S) -> List<Element>
- Complexity: O(
-
Return
self
prepended with the elements ofwith
.Declaration
Swift
public func prextended(with: List<Element>) -> List<Element>
-
Return
self
prepended with the elements ofwith
.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>
- Requires:
-
Returns a
List
of the initial elements ofself
, up until the first element that returns false forisElement
Declaration
Swift
public func prefixWhile(isElement: Element -> Bool) -> List<Element>
-
Returns a
List
ofself
with the first elements that satisfyisNotElement
dropped.Declaration
Swift
public func dropWhile(@noescape isNotElement: Element -> Bool) -> List<Element>
-
Returns a
List
of the initial elements ofself
, of maximum lengthn
.Declaration
Swift
public func prefix(n: Int) -> List<Element>
-
Returns a
List
of the final elements ofself
, of maximum lengthn
.Declaration
Swift
public func suffix(n: Int) -> List<Element>
-
Returns the maximal
List
s ofself
, in order, that don’t contain elements satisfying the predicateisSeparator
.- Parameter maxSplits: The maximum number of
List
s to return, minus 1. IfmaxSplit
+ 1List
s are returned, the last one is a suffix ofself
containing the remaining elements. The default value isInt.max
. - Parameter allowEmptySubsequences: If
true
, an emptyList
is produced in the result for each pair of consecutive elements satisfyingisSeparator
. The default value is false. - Requires: maxSplit >= 0
Declaration
Swift
public func split(maxSplit: Int, allowEmptySlices: Bool, @noescape isSeparator: Element -> Bool) -> [List<Element>]
- Parameter maxSplits: The maximum number of
-
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 callingcombine
on successive elements ofself
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 callingcombine
on successive elements ofself
. Initial is taken to be the first element ofself
.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 thatcombine
is called onself
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 thatcombine
is called onself
in reverse, and the arguments are flipped. The initial agument toaccumulator
forcombine
is taken as the final element ofself
.Declaration
Swift
public func reduceR(combine: (element: Element, accumulator: Element) -> Element) -> Element?