Array
public extension Array
public extension Array where Element: Equatable
-
Flattens the current Array of any type
This method will help you convert an array like
[ [1, 2], [3, 4], 5 ]to[1, 2, 3, 4, 5]Here is how you use it:let array = [ [1, 2], [3, 4], 5 ] array.flatten() // => [1, 2, 3, 4, 5]Declaration
Swift
func flatten(_ index: Int = 0) -> [Any]Parameters
indexThe starting index of the array
-
This will create a range of number starting from the number
a, going to the numberbwithxnumbers in it.If you don’t have any idea of how it works, the example may help you:
Array<Any>.linspace(start: 0, end: 100, n: 10) // => [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]Declaration
Swift
static func linspace(start: Double, end: Double, n: Int) -> [Double]Parameters
startThe start of the array
endThe end of your array
nThe length of the outputed array
-
Similar as
Array.range, but with more options (start, end, step)Example:
Array<Any>.arange(start: 1, end: 11, step: 2, offset: 0) // => [ 1, 3, 5, 7, 9, 11 ]Declaration
Swift
static func arange(start: Double, end: Double, step: Double = 1, offset: Double = 0) -> [Double]Parameters
startThe start of the array
endThe end of the array
stepThe step the function will use to generate the array
offsetThe offset you want to use to generate your array
-
This will create a range of number, starting from
0up to a numbernExample:
Array<Any>.range(10) // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]Declaration
Swift
static func range(_ n: Int) -> [Int]Parameters
nThe number
nrepresent the end of the range -
The reshape function is the opposite of the
Array.flattenfunction. It will group items in an array byxitems:Example:
Array<Any>.range(10).reshape(part: 2) /* => [ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10] ] */Declaration
Swift
func reshape(part: Int) -> [[Element]]Parameters
partThe length of each part
-
Removes duplicate elements
Declaration
Swift
var uniques: Array { get }
View on GitHub
Array Extension Reference