-
Notifications
You must be signed in to change notification settings - Fork 318
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
// 10
eachRight (call: (Element) -> ())
eachRight (call: (Int, Element) -> ())
Iterates over each element in
self
from right to left.
let array = [5, 1, 0, 10]
array.eachRight { item in println(item) }
/* Prints → */
// 10
// 0
// 1
// 5
let 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
self
contains at least one element (count > 0
), returns its first element (self[0]
).
let array = [1, 2, 3, 4, 5]
array.first()!
// → 1
let array: Int[] = []
array.first()
// → nil
last () -> Element?
If
self
contains at least one element (count > 0
), returns its last element (self[count - 1]
).
let array = [1, 2, 3, 4, 5]
array.last()!
// → 5
let array: Int[] = []
array.last()
// → nil
get (n: Int) -> Element?
If
n
is positive andself
contains at leastn
elements, returns then-th
element.
Ifn
is positive andself
containsn > count > 0
elements, returns then % count
element.
Ifn
is negative, returns theabs(n)-th
element counting from the end where-1
is the last element.
get (r: Range<Int>) -> Array
Returns a subarray of
self
containing the elements in the ranger
.
Considering let array = [1, 2, 3, 4]
array.get(2)!
// → 3
array.get(4)!
// → 1
array.get(-1)!
// → 4
array.get(0..2)!
// → [1, 2]
at (indexes: Int...) -> Array
Maps each index in
indexes
to its value inself
.
let array = ["A", "B", "C", "D"]
array.at(0, 2)
// → ["A", "C"]
take (n: Int) -> Array
Returns the first
n
elements 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 }
// → 2
tail (n: Int) -> Array
Returns the last
n
elements ofself
.
let array = ["A", "B", "C", "D"]
array.tail(2)
// → ["C", "D"]
skip (n: Int) -> Array
Returns the subsequence
n..count
ofself
.
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
n
randomself
elements.
let array = ["A", "B", "C", "D"]
array.sample(2)
// → ["D", "A"]
contains <T: Equatable> (item: T...) -> Bool
Returns
true
ifself
containsitems
,false
otherwise.
let array = ["A", "B", "C", "D"]
array.contains("A", "C")
// → true
array.contains("Z")
// → false
find <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)
// → nil
indexOf <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 }!
// → 1
lastIndexOf <U: Equatable> (item: U) -> Int?
If
self
contains anyitem
, returns the index of the last one,nil
ifitem
is not inself
.
let array = [5, 6, 7, 2, 7]
/* array contains 7 */
array.lastIndexOf(7)!
// → 4
/* array does not contain 10 */
array.lastIndexOf(10)
// → nil
max <T: Comparable> () -> T
If
self
's elements implementComparable
, returns the biggest one.
let array = [5, 1, 0, 10, 3]
array.max() as Int
// → 10
min <T: Comparable> () -> T
If
self
's elements implementComparable
, returns the smallest one.
let array = [5, 1, 0, 10, 3]
array.min() as Int
// → 0
any (call: (Element) -> Bool) -> Bool
Executes
call
on each element inself
and returnstrue
ifcall
returnstrue
for anyone of them.
let array = ["A", "B", "C", "D"]
/* Positive */
array.any { $0 == "B" }
// → true
/* Negative */
array.any { $0 == "Z" }
// → false
all (call: (Element) -> Bool) -> Bool
Executes
call
on each element inself
and returnstrue
ifcall
returnstrue
for each one of them.
let array = [4, 6, 8, 10]
/* Positive */
array.all { $0.isEven() }
// → true
/* Negative */
array.all { $0 < 8 }
// → false
unique <T: Equatable> () -> Array<T>
Returns a copy of
self
without 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
self
and the givenarrays
. The second one, contains the second elements ofself
and the givenarrays
, ...
[1, 2].zip(["A", "B"])
// → [[1, "A"], [2, "B"]]
shuffled () -> Array
Returns a shuffled copy of
self
using 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 ofself
containingself
's items for whichexclude
returnstrue
.
let array = [5, 6, 7, 8]
array.reject{ $0.isEven() }
// → [5, 7]
groupBy <U> (groupingFunction group: (Element) -> (U)) -> Dictionary<U, Array>
Collect
self
into 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.reduce
with initial argumentself[0]
(ifself
is 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) -> U
reduceRight (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
self
usingseparator
.
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
n
elements, each offset bystep
.
pad
is an array of elements to pad the last partition if it is not long enough to containn
elements. Ifnil
is passed or there are not enough pad elements the last partition may less thann
elements 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
n
elements, each offset bystep
.
This method allows the last partition to be less thann
elements 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
cond
to 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?
map
followed by afilter
taking only!= nil
elements.
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
self
containsitem
, removes it.
let array = ["A", "B", "C", "D"]
array.remove("B")
array
// → ["A", "C", "D"]
shuffle ()
Shuffles the items in
self
using 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
newElement
toself
. 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
newElement
toself
.
let array = ["A", "B", "C", "D"]
array.unshift("0")
array
// → ["0", "A", "B", "C", "D"]
insert (newArray: Array, atIndex: Int)
Like the default
insert
function with an array as first parameter. InsertsnewArray
atatIndex
inself
.
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
self
and 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
self
and 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
self
and 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
array
repeatedn
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 rangerange
.
-
subscript (first: Int, second: Int, rest: Int...) -> Array
, e.g.[1, 3, 5]
Returns an array containing the elements of
self
at indexesfirst
,second
andindexes
.