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

Contents

Instance Methods

Iteration methods

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.each { item in println(item) }
/* Prints → */
// 10
// 0
// 1
// 5
With indexes
let array = [5, 1, 0, 10]
array.each { item, index in println(index, item) }
/* Prints → */
// (3, 10)
// (2, 0)
// (1, 1)
// (0, 5)

Items accessor methods

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]

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 - n) 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"]

Search methods

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

indexOf

  • indexOf <T: Equatable> (item: T) -> Int

If self contains any item, returns the index of the first one, -1 if item is not in self.

Example

let array = [5, 6, 7]

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

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

lastIndexOf

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

If self contains any item, returns the index of the last one, -1 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)
// → -1

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

Testing methods

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 methods

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].

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(+)
// → "CAB"

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[]

Flattens a nested array.

Example

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

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"]

Sets methods

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