Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
hyeonjae edited this page May 29, 2015 · 55 revisions

Contents

Instance Methods

Iteration

each

  • each (call: (Element) -> ())
  • each (call: (Int, Element) -> ())

Iterates over each element in self.

Examples

Without indexes
let array = [5, 1, 0, 10]
array.each { item in println(item) }
/* Prints → */
// 5
// 1
// 0
// 10

eachRight

  • eachRight (call: (Element) -> ())
  • eachRight (call: (Int, Element) -> ())

Iterates over each element in self from right to left.

Examples

Without indexes
let array = [5, 1, 0, 10]
array.eachRight { item in println(item) }
/* Prints → */
// 10
// 0
// 1
// 5
With indexes
let array = [5, 1, 0, 10]
array.eachRight { item, index in println(index, item) }
/* Prints → */
// (3, 10)
// (2, 0)
// (1, 1)
// (0, 5)

Items accessing

first

  • first () -> Element?

If self contains at least one element (count > 0), returns its first element (self[0]).

Example

Non empty array
let array = [1, 2, 3, 4, 5]
array.first()!
// → 1
Empty array
let array: Int[] = []
array.first()
// → nil

last

  • last () -> Element?

If self contains at least one element (count > 0), returns its last element (self[count - 1]).

Examples

Non empty array
let array = [1, 2, 3, 4, 5]
array.last()!
// → 5
Empty array
let array: Int[] = []
array.last()
// → nil

get

If n is Int
  • get (n: Int) -> Element?

If n is positive and self contains at least n elements, returns the n-th element.
If n is positive and self contains n > count > 0 elements, returns the n % count element.
If n is negative, returns the abs(n)-th element counting from the end where -1 is the last element.

If r is Range<Int>
  • get (r: Range<Int>) -> Array

Returns a subarray of self containing the elements in the range r.

Examples

Considering let array = [1, 2, 3, 4]

n: Int, n > 0
array.get(2)!
// → 3
n: Int, n > count > 0
array.get(4)!
// → 1
n: Int, n < 0
array.get(-1)!
// → 4
r: Range<Int>
array.get(0..2)!
// → [1, 2]

at

  • at (indexes: Int...) -> Array

Maps each index in indexes to its value in self.

Example

let array = ["A", "B", "C", "D"]
array.at(0, 2)
// → ["A", "C"]

take

  • take (n: Int) -> Array

Returns the first n elements of self.

Example

let array = ["A", "B", "C", "D"]
array.take(2)
// → ["A", "B"]

takeWhile

  • takeWhile (condition: (Element) -> Bool) -> Array

Returns the elements of the array up until an element does not meet the condition.

Example

let array = [1, 2, 3, 4, 5]
array.takeWhile { $0 < 3 }
// → [1 , 2]

takeFirst

  • takeFirst (condition: (Element) -> Bool) -> Element?

Returns the first element in the array to meet the condition.

Example

let array = [1, 2, 3, 4, 5]
array.takeFirst { $0 % 2 == 0 }
// → 2

tail

  • tail (n: Int) -> Array

Returns the last n elements of self.

Example

let array = ["A", "B", "C", "D"]
array.tail(2)
// → ["C", "D"]

skip

  • skip (n: Int) -> Array

Returns the subsequence n..count of self.

Example

let array = ["A", "B", "C", "D"]
array.skip(1)
// → ["B", "C", "D"]

skipWhile

  • skipWhile (condition: (Element) -> Bool) -> Array

Skips the elements of the array up until the condition returns false.

Example

let array = [1, 2, 3, 4, 5]
array.skipWhile { $0 < 3 }
// → [3, 4, 5]

sample

  • sample (size n: Int = 1) -> Array<T>

Returns an array of n random self elements.

Example

let array = ["A", "B", "C", "D"]
array.sample(2)
// → ["D", "A"]

Searching

contains

  • contains <T: Equatable> (item: T...) -> Bool

Returns true if self contains items, false otherwise.

Example

let array = ["A", "B", "C", "D"]
array.contains("A", "C")
// → true

array.contains("Z")
// → false

find

With an Equatable element
  • find <T: Equatable> (item: T) -> T?

Returns the first element in the array equal to item.

With a closure
  • find (condition: Element -> Bool) -> Element?

Returns the first element in the array to meet the condition.

Example

let array = [1, 2, 3, 4, 5]
array.find(3)
// → 3

array.find { $0 % 2 == 0 }
// → 2

array.find(6)
// → nil

indexOf

With an Equatable element
  • indexOf <T: Equatable> (item: T) -> Int?

Returns the index of the first element in the array equal to item.

With a closure
  • indexOf (condition: Element -> Bool) -> Int?

Returns the first index of the element in the array to meet the condition.

Example

let array = [5, 6, 7]

/* array contains 7 */
array.indexOf(7)!
// → 2

/* array does not contain 10 */
array.indexOf(10)
// → nil

/* index of the first element > 5 */
array.indexOf { $0 > 5 }!
// → 1

lastIndexOf

  • lastIndexOf <U: Equatable> (item: U) -> Int?

If self contains any item, returns the index of the last one, nil if item is not in self.

Example

let array = [5, 6, 7, 2, 7]

/* array contains 7 */
array.lastIndexOf(7)!
// → 4

/* array does not contain 10 */
array.lastIndexOf(10)
// → nil

max

  • max <T: Comparable> () -> T

If self's elements implement Comparable, returns the biggest one.

Example

let array = [5, 1, 0, 10, 3]
array.max() as Int
// → 10

min

  • min <T: Comparable> () -> T

If self's elements implement Comparable, returns the smallest one.

Example

let array = [5, 1, 0, 10, 3]
array.min() as Int
// → 0

Items testing

any

  • any (call: (Element) -> Bool) -> Bool

Executes call on each element in self and returns true if call returns true for anyone of them.

Example

let array = ["A", "B", "C", "D"]

/* Positive */
array.any { $0 == "B" }
// → true

/* Negative */
array.any { $0 == "Z" }
// → false

all

  • all (call: (Element) -> Bool) -> Bool

Executes call on each element in self and returns true if call returns true for each one of them.

Example

let array = [4, 6, 8, 10]

/* Positive */
array.all { $0.isEven() }
// → true

/* Negative */
array.all { $0 < 8 }
// → false

Transformation

unique

  • unique <T: Equatable> () -> Array<T>

Returns a copy of self without any duplicate element.

Example

let array = ["A", "B", "A", "B"]
array.unique() as String[]
// → ["A", "B"]

zip

  • zip (arrays: Array<Any>...) -> Array<Array<Any?>>

Creates an array of arrays. The first one, contains the first elements of self and the given arrays. The second one, contains the second elements of self and the given arrays, ...

Example

[1, 2].zip(["A", "B"])
// → [[1, "A"], [2, "B"]]

shuffled

  • shuffled () -> Array

Returns a shuffled copy of self using the Fisher-Yates shuffle.

Example

let array = ["A", "B", "C", "D"]
array.shuffled()
// → ["D", "A", "B", "C"]

reject

  • reject (exclude: (Element -> Bool)) -> Array

Opposite of filter. Returns a copy of self containing self's items for which exclude returns true.

Example

let array = [5, 6, 7, 8]
array.reject{ $0.isEven() }
// → [5, 7]

groupBy

  • groupBy <U> (groupingFunction group: (Element) -> (U)) -> Dictionary<U, Array>

Collect self into arrays, grouped by the result of groupingFunction.

Example

let array = [5, 6, 7, 8]
array.groupBy{ value in 
    return value.isEven() ? "even" : "odd"
}
// → ["even": [6, 8], "odd": [5, 7]]

countBy

  • countBy <U> (groupingFunction group: (Element) -> (U)) -> Dictionary<U, Int>

Similar to groupBy, instead of returning a list of values, returns the number of values for each group.

Example

let array = [5, 6, 7, 8]
let result = array.countBy{ value -> String in
    return value.isEven() ? "even" : "odd"
}
// → ["even": 2, "odd": 2]

reduce

  • reduce (combine: (Element, Element) -> Element) -> Element?

Swift.reduce with initial argument self[0] (if self is not empty)

Example

let list = [[0, 1], [2, 3], [4, 5]]
list.reduce { return $0 + $1 }
// → [0, 1, 2, 3, 4, 5]

reduceRight

  • reduceRight <U>(initial: U, combine: (U, Element) -> U) -> U
  • reduceRight (combine: (Element, Element) -> Element) -> Element?

Similar to reduce, but the elements are enumerated from right to left.

Examples

With initial argument
let list = [[0, 1], [2, 3], [4, 5]]
list.reduceRight(Array<Int>(), { return $0 + $1 })
// → [4, 5, 2, 3, 0, 1]
Without initial argument
let list = ["A", "B", "C"]
list.reduceRight(+)
// → "CBA"

implode

  • implode <C: ExtensibleCollection> (separator: C) -> C?

Joins the elements in self using separator.

Example

let array = ["Hello", "World"]
array.implode(", ")
// → "Hello, World"

flatten

  • flatten <OutType> () -> [OutType]
  • flattenAny () -> [AnyObject]

Flattens a nested array.

Example

let array = [1, 2, [3, 4, [5]], 6]
array.flatten() as NSNumber[]
// → [1, 2, 3, 4, 5, 6]

partition

  • partition (var n: Int, var step: Int? = nil) -> Array<Array<Element>>
  • partition (var n: Int, var step: Int? = nil, pad: Element[]?) -> Array<Array<Element>>

Produces an array of arrays, each containing n elements, each offset by step.
pad is an array of elements to pad the last partition if it is not long enough to contain n elements. If nil is passed or there are not enough pad elements the last partition may less than n elements long.

Example

let array = [1, 2, 3, 4, 5]
array.partition(2, step: 1)
// → [[1, 2], [2, 3], [3, 4], [4, 5]]

partitionAll

  • partitionAll (var n: Int, var step: Int? = nil) -> Array<Array<Element>>

Produces an array of arrays, each containing n elements, each offset by step.
This method allows the last partition to be less than n elements long without using padding elements.

Example

let array = [1, 2, 3, 4, 5]
array.partitionAll(2, step: 1)
// → [[1, 2], [2, 3], [3, 4], [4, 5], [5]]

partitionBy

  • partitionBy <T: Equatable> (cond: (Element) -> T) -> Array<Array<Element>>

Applies cond to each element in array, splitting it each time cond returns a new value.

Example

let array = [1, 7, 3, 6, 10, 12]
array.partitionBy { $0 % 3 }
// → [[1, 7], [3, 6], [10], [12]]

toDictionary

  • toDictionary <U> (keySelector:(Element) -> U) -> Dictionary<U, Element>

Converts the array to a dictionary with the keys supplied via the keySelector.

Example

class Person {
    let name: String, age: Int, id: String
    init(_ name: String, _ age: Int, _ id: String){
        self.name = name
        self.age = age
        self.id = id
    }
}

let people = [
    Person("bob", 25, "P1"),
    Person("frank", 45, "P2"),
    Person("ian", 35, "P3")
]

let dictionary = people.toDictionary { $0.id }
// → [P3: Person (has 3 children), P1: Person (has 3 children), P2: Person (has 3 children)]

mapFilter

  • mapFilter <V> (mapFunction map: (Element) -> (V)?) -> [V]

Creates an array with values generated by running each value of self through the mapFunction and discarding nil return values. Works as a Element -> V? map followed by a filter taking only != nil elements.

Example

let array = [1, 7, 3, 6, 10, 12]
array.mapFilter { value -> Int? in
    if value > 7 { return nil }
    return value + 1
}
// → [2, 8, 4, 7]

Mutating methods

remove

  • remove <U: Equatable> (element: U)

If self contains item, removes it.

Example

let array = ["A", "B", "C", "D"]
array.remove("B")
array
// → ["A", "C", "D"]

shuffle

  • shuffle ()

Shuffles the items in self using the Fisher-Yates shuffle.

Example

let array = ["A", "B", "C", "D"]
array.shuffle()
array
// → ["D", "A", "B", "C"]

pop

  • pop() -> Element

Removes self's last element and returns it.

Example

let array = ["A", "B", "C", "D"]
array.pop()
// → "D"
array
// → ["A", "B", "C"]

push

  • push (newElement: Element)

Appends newElement to self. Equivalent to append(element).

Example

let array = ["A", "B", "C", "D"]
array.push("E")
array
// → ["A", "B", "C", "D", "E"]

shift

  • shift() -> Element

Removes self's first element and returns it.

Example

let array = ["A", "B", "C", "D"]
array.shift()
// → "A"
array
// → ["B", "C", "D"]

unshift

  • unshift (newElement: Element)

Prepends newElement to self.

Example

let array = ["A", "B", "C", "D"]
array.unshift("0")
array
// → ["0", "A", "B", "C", "D"]

insert

  • insert (newArray: Array, atIndex: Int)

Like the default insert function with an array as first parameter. Inserts newArray at atIndex in self.

Example

var array = ["A", "B", "C", "D"]
array.insert(["F", "G"], 3)
println(array)
// → ["A", "B", "C", "F", "G", "D"]

Sorting

sortBy

  • sortBy (isOrderedBefore: (T, T) -> Bool) -> Array<T>

Sorts the array by the given comparison function.

Example

let sourceArray = [2, 3, 6, 5]
let sortedArray = sourceArray.sortBy {$0 < $1}
// → [2, 3, 5, 6]

Sets

intersection

  • intersection <U: Equatable> (values: Array<U>...) -> Array
  • & <T: Equatable> (first: Array<T>, second: Array<T>) -> Array<T>

Constructs an array containing all the common elements between self and each array in values.

Example

let array_1 = [1, 2, 3, 4, 5]
let array_2 = [4, 5]
let array_3 = [3, 5, 4]
array_1.intersection(array_2, array_3)
// → [4, 5]

/* Equivalent to */
array_1 & array_2 & array_3
// → [4, 5]

union

  • union <U: Equatable> (values: Array<U>...) -> Array
  • | <T: Equatable> (first: Array, second: Array) -> Array

Joins self and each array in values.

Example

let array_1 = [1, 2, 3, 4]
let array_2 = [4, 5]
let array_3 = [3, 5, 4]
array_1.union(array_2, array_3)
// → [1, 2, 3, 4, 5]

/* Equivalent to */
array_1 | array_2 | array_3
// → [1, 2, 3, 4, 5]

difference

  • difference <T: Equatable> (values: Array<T>...) -> Array<T>
  • - <T: Equatable> (first: Array<T>, second: Array<T>) -> Array<T>

Constructs an array containing all the elements that are in self and not in each array in values.

Example

let array_1 = [1, 2, 3, 4]
let array_2 = [4, 5]
let array_3 = [3, 6, 8]
array_1.difference(array_2, array_3)
// → [1, 2]

/* Equivalent to */
array_1 - array_2 - array_3
// → [1, 2]

Class Methods

range

  • range <U: ForwardIndex> (range: Range<U>) -> Array<U>

Given a range (range), returns an array containing the elements of that range.

Example

Array<Int>.range(0...2)
// → [0, 1, 2]

#Operators ##Minus

If the second argument is an Array
  • - <T: Equatable> (first: Array<T>, second: Array<T>) -> Array<T>

Performs the difference: first - second.

If the second argument is an Element of Array
  • - <T: Equatable> (first: Array<T>, second: T) -> Array<T>

Performs the difference: first - [second].

##And

  • & <T: Equatable> (first: Array<T>, second: Array<T>) -> Array<T>

Performs the intersection: first & second.

##Or

  • | <T: Equatable> (first: Array, second: Array) -> Array

Performs the union: first | second.

##Star

  • * <ItemType> (array: ItemType[], n: Int) -> ItemType[]

Returns an Array containing the elements of array repeated n times.

  • * (array: String[], separator: String) -> String

Equivalent to array.implode(separator).

##Subscript

  • subscript (var range: Range<Int>) -> Array, e.g. [1..10]

Returns an array containing the elements of self whose indexes are in the range range.

  • subscript (first: Int, second: Int, rest: Int...) -> Array, e.g. [1, 3, 5]

Returns an array containing the elements of self at indexes first, second and indexes.

Clone this wiki locally