-
Notifications
You must be signed in to change notification settings - Fork 314
Array
- Instance methods
- Class methods
- Operators
each (call: (Element) -> ())each (call: (Int, Element) -> ())
Iterates over each element in
self.
let array = [5, 1, 0, 10]
array.each { item in println(item) }
/* Prints → */
// 5
// 1
// 0
// 10eachRight (call: (Element) -> ())eachRight (call: (Int, Element) -> ())
Iterates over each element in
selffrom right to left.
let array = [5, 1, 0, 10]
array.eachRight { item in println(item) }
/* Prints → */
// 10
// 0
// 1
// 5let array = [5, 1, 0, 10]
array.eachRight { item, index in println(index, item) }
/* Prints → */
// (3, 10)
// (2, 0)
// (1, 1)
// (0, 5)first () -> Element?
If
selfcontains at least one element (count > 0), returns its first element (self[0]).
let array = [1, 2, 3, 4, 5]
array.first()!
// → 1let array: Int[] = []
array.first()
// → nillast () -> Element?
If
selfcontains at least one element (count > 0), returns its last element (self[count - 1]).
let array = [1, 2, 3, 4, 5]
array.last()!
// → 5let array: Int[] = []
array.last()
// → nilget (n: Int) -> Element?
If
nis positive andselfcontains at leastnelements, returns then-thelement.
Ifnis positive andselfcontainsn > count > 0elements, returns then % countelement.
Ifnis negative, returns theabs(n)-thelement counting from the end where-1is the last element.
get (r: Range<Int>) -> Array
Returns a subarray of
selfcontaining the elements in the ranger.
Considering let array = [1, 2, 3, 4]
array.get(2)!
// → 3array.get(4)!
// → 1array.get(-1)!
// → 4array.get(0..2)!
// → [1, 2]at (indexes: Int...) -> Array
Maps each index in
indexesto its value inself.
let array = ["A", "B", "C", "D"]
array.at(0, 2)
// → ["A", "C"]take (n: Int) -> Array
Returns the first
nelements ofself.
let array = ["A", "B", "C", "D"]
array.take(2)
// → ["A", "B"]takeWhile (condition: (Element) -> Bool) -> Array
Returns the elements of the array up until an element does not meet the condition.
let array = [1, 2, 3, 4, 5]
array.takeWhile { $0 < 3 }
// → [1 , 2]takeFirst (condition: (Element) -> Bool) -> Element?
Returns the first element in the array to meet the condition.
let array = [1, 2, 3, 4, 5]
array.takeFirst { $0 % 2 == 0 }
// → 2tail (n: Int) -> Array
Returns the last
nelements ofself.
let array = ["A", "B", "C", "D"]
array.tail(2)
// → ["C", "D"]skip (n: Int) -> Array
Returns the subsequence
n..countofself.
let array = ["A", "B", "C", "D"]
array.skip(1)
// → ["B", "C", "D"]skipWhile (condition: (Element) -> Bool) -> Array
Skips the elements of the array up until the condition returns false.
let array = [1, 2, 3, 4, 5]
array.skipWhile { $0 < 3 }
// → [3, 4, 5]sample (size n: Int = 1) -> Array<T>
Returns an array of
nrandomselfelements.
let array = ["A", "B", "C", "D"]
array.sample(2)
// → ["D", "A"]contains <T: Equatable> (item: T...) -> Bool
Returns
trueifselfcontainsitems,falseotherwise.
let array = ["A", "B", "C", "D"]
array.contains("A", "C")
// → true
array.contains("Z")
// → falsefind <T: Equatable> (item: T) -> T?
Returns the first element in the array equal to
item.
find (condition: Element -> Bool) -> Element?
Returns the first element in the array to meet the condition.
let array = [1, 2, 3, 4, 5]
array.find(3)
// → 3
array.find { $0 % 2 == 0 }
// → 2
array.find(6)
// → nilindexOf <T: Equatable> (item: T) -> Int?
Returns the index of the first element in the array equal to
item.
indexOf (condition: Element -> Bool) -> Int?
Returns the first index of the element in the array to meet the condition.
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 }!
// → 1lastIndexOf <U: Equatable> (item: U) -> Int?
If
selfcontains anyitem, returns the index of the last one,nilifitemis not inself.
let array = [5, 6, 7, 2, 7]
/* array contains 7 */
array.lastIndexOf(7)!
// → 4
/* array does not contain 10 */
array.lastIndexOf(10)
// → nilmax <T: Comparable> () -> T
If
self's elements implementComparable, returns the biggest one.
let array = [5, 1, 0, 10, 3]
array.max() as Int
// → 10min <T: Comparable> () -> T
If
self's elements implementComparable, returns the smallest one.
let array = [5, 1, 0, 10, 3]
array.min() as Int
// → 0any (call: (Element) -> Bool) -> Bool
Executes
callon each element inselfand returnstrueifcallreturnstruefor anyone of them.
let array = ["A", "B", "C", "D"]
/* Positive */
array.any { $0 == "B" }
// → true
/* Negative */
array.any { $0 == "Z" }
// → falseall (call: (Element) -> Bool) -> Bool
Executes
callon each element inselfand returnstrueifcallreturnstruefor each one of them.
let array = [4, 6, 8, 10]
/* Positive */
array.all { $0.isEven() }
// → true
/* Negative */
array.all { $0 < 8 }
// → falseunique <T: Equatable> () -> Array<T>
Returns a copy of
selfwithout any duplicate element.
let array = ["A", "B", "A", "B"]
array.unique() as String[]
// → ["A", "B"]zip (arrays: Array<Any>...) -> Array<Array<Any?>>
Creates an array of arrays. The first one, contains the first elements of
selfand the givenarrays. The second one, contains the second elements ofselfand the givenarrays, ...
[1, 2].zip(["A", "B"])
// → [[1, "A"], [2, "B"]]shuffled () -> Array
Returns a shuffled copy of
selfusing the Fisher-Yates shuffle.
let array = ["A", "B", "C", "D"]
array.shuffled()
// → ["D", "A", "B", "C"]reject (exclude: (Element -> Bool)) -> Array
Opposite of
filter. Returns a copy ofselfcontainingself's items for whichexcludereturnstrue.
let array = [5, 6, 7, 8]
array.reject{ $0.isEven() }
// → [5, 7]groupBy <U> (groupingFunction group: (Element) -> (U)) -> Dictionary<U, Array>
Collect
selfinto arrays, grouped by the result ofgroupingFunction.
let array = [5, 6, 7, 8]
array.groupBy{ value in
return value.isEven() ? "even" : "odd"
}
// → ["even": [6, 8], "odd": [5, 7]]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.
let array = [5, 6, 7, 8]
let result = array.countBy{ value -> String in
return value.isEven() ? "even" : "odd"
}
// → ["even": 2, "odd": 2]reduce (combine: (Element, Element) -> Element) -> Element?
Swift.reducewith initial argumentself[0](ifselfis not empty)
let list = [[0, 1], [2, 3], [4, 5]]
list.reduce { return $0 + $1 }
// → [0, 1, 2, 3, 4, 5]reduceRight <U>(initial: U, combine: (U, Element) -> U) -> UreduceRight (combine: (Element, Element) -> Element) -> Element?
Similar to
reduce, but the elements are enumerated from right to left.
let list = [[0, 1], [2, 3], [4, 5]]
list.reduceRight(Array<Int>(), { return $0 + $1 })
// → [4, 5, 2, 3, 0, 1]let list = ["A", "B", "C"]
list.reduceRight(+)
// → "CBA"implode <C: ExtensibleCollection> (separator: C) -> C?
Joins the elements in
selfusingseparator.
let array = ["Hello", "World"]
array.implode(", ")
// → "Hello, World"flatten <OutType> () -> [OutType]flattenAny () -> [AnyObject]
Flattens a nested array.
let array = [1, 2, [3, 4, [5]], 6]
array.flatten() as NSNumber[]
// → [1, 2, 3, 4, 5, 6]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
nelements, each offset bystep.
padis an array of elements to pad the last partition if it is not long enough to containnelements. Ifnilis passed or there are not enough pad elements the last partition may less thannelements long.
let array = [1, 2, 3, 4, 5]
array.partition(2, step: 1)
// → [[1, 2], [2, 3], [3, 4], [4, 5]]partitionAll (var n: Int, var step: Int? = nil) -> Array<Array<Element>>
Produces an array of arrays, each containing
nelements, each offset bystep.
This method allows the last partition to be less thannelements long without using padding elements.
let array = [1, 2, 3, 4, 5]
array.partitionAll(2, step: 1)
// → [[1, 2], [2, 3], [3, 4], [4, 5], [5]]partitionBy <T: Equatable> (cond: (Element) -> T) -> Array<Array<Element>>
Applies
condto each element in array, splitting it each time cond returns a new value.
let array = [1, 7, 3, 6, 10, 12]
array.partitionBy { $0 % 3 }
// → [[1, 7], [3, 6], [10], [12]]toDictionary <U> (keySelector:(Element) -> U) -> Dictionary<U, Element>
Converts the array to a dictionary with the keys supplied via the
keySelector.
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 <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?mapfollowed by afiltertaking only!= nilelements.
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]remove <U: Equatable> (element: U)
If
selfcontainsitem, removes it.
let array = ["A", "B", "C", "D"]
array.remove("B")
array
// → ["A", "C", "D"]shuffle ()
Shuffles the items in
selfusing the Fisher-Yates shuffle.
let array = ["A", "B", "C", "D"]
array.shuffle()
array
// → ["D", "A", "B", "C"]pop() -> Element
Removes
self's last element and returns it.
let array = ["A", "B", "C", "D"]
array.pop()
// → "D"
array
// → ["A", "B", "C"]push (newElement: Element)
Appends
newElementtoself. Equivalent toappend(element).
let array = ["A", "B", "C", "D"]
array.push("E")
array
// → ["A", "B", "C", "D", "E"]shift() -> Element
Removes
self's first element and returns it.
let array = ["A", "B", "C", "D"]
array.shift()
// → "A"
array
// → ["B", "C", "D"]unshift (newElement: Element)
Prepends
newElementtoself.
let array = ["A", "B", "C", "D"]
array.unshift("0")
array
// → ["0", "A", "B", "C", "D"]insert (newArray: Array, atIndex: Int)
Like the default
insertfunction with an array as first parameter. InsertsnewArrayatatIndexinself.
var array = ["A", "B", "C", "D"]
array.insert(["F", "G"], 3)
println(array)
// → ["A", "B", "C", "F", "G", "D"]sortBy (isOrderedBefore: (T, T) -> Bool) -> Array<T>
Sorts the array by the given comparison function.
let sourceArray = [2, 3, 6, 5]
let sortedArray = sourceArray.sortBy {$0 < $1}
// → [2, 3, 5, 6]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
selfand each array invalues.
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 <U: Equatable> (values: Array<U>...) -> Array| <T: Equatable> (first: Array, second: Array) -> Array
Joins
selfand each array invalues.
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 <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
selfand not in each array invalues.
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]range <U: ForwardIndex> (range: Range<U>) -> Array<U>
Given a range (
range), returns an array containing the elements of that range.
Array<Int>.range(0...2)
// → [0, 1, 2]#Operators ##Minus
- <T: Equatable> (first: Array<T>, second: Array<T>) -> Array<T>
Performs the
difference:first - second.
- <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
arrayrepeatedntimes.
* (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
selfwhose indexes are in the rangerange.
-
subscript (first: Int, second: Int, rest: Int...) -> Array, e.g.[1, 3, 5]
Returns an array containing the elements of
selfat indexesfirst,secondandindexes.