-
Notifications
You must be signed in to change notification settings - Fork 315
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.each { item in println(item) }
/* Prints → */
// 10
// 0
// 1
// 5
let array = [5, 1, 0, 10]
array.each { 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]
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 - n)
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
indexOf <T: Equatable> (item: T) -> Int
If
self
contains anyitem
, returns the index of the first one,-1
ifitem
is not inself
.
let array = [5, 6, 7]
/* array contains 7 */
array.indexOf(7)
// → 2
/* array does not contain 10 */
array.indexOf(10)
// → -1
lastIndexOf <U: Equatable> (item: U) -> Int
If
self
contains anyitem
, returns the index of the last one,-1
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)
// → -1
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]
.
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(+)
// → "CAB"
implode <C: ExtensibleCollection> (separator: C) -> C?
Joins the elements in
self
usingseparator
.
let array = ["Hello", "World"]
array.implode(", ")
// → "Hello, World"
flatten <OutType> () -> OutType[]
Flattens a nested array.
let array = [1, 2, [3, 4, [5]], 6]
array.flatten() as Int[]
// → [1, 2, 3, 4, 5, 6]
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"]
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
.