// Playing with Swift Array
var a = Array<Int>([1,2,3])
a // [1,2,3]
a[0] // 1
a[1] // 2
a[2] // 3
var b = Array<Int>(count:5, repeatedValue:42)
b // [42, 42, 42, 42, 42]
a.startIndex // 0
b.startIndex // 0
a.endIndex // 3
b.endIndex // 5
a.count // 3
b.count // 5
a.capacity // 4
b.capacity // 6
var c = Array<Int>()
c // 0 elements
a.isEmpty // false
c.isEmpty // true
a.first // {Some 1}
b.first // {Some 42}
c.first // nil
a.last // {Some 3}
b.last // {Some 42}
c.last // nil
a.description
// "[1, 2 3]"
b.description
// "[42, 42, 42, 42, 42]"
c.description
// "[]"
a.debugDescription
// "1, 2, 3]"
c.debugDescription
// "[]"
var f = a.generate()
// {{1, 2, 3}, _position 0}
f.next() // {Some 1}
f.next() // {Some 2}
f.next() // {Some 3}
f.next() // nil
a.reserveCapacity(12)
a.capacity // 12
a.append(-1)
a // [1, 2, 3, -1]
b.extend(a)
b // [42, 42, 42, 42, 1, 2, 3, -1]
b.removeLast() // -1
b // [42, 42, 42, 42, 1, 2, 3]
a.insert(31, atIndex: 0)
a // [31, 1, 2, 3, -1]
a.removeAtIndex(2)
a // [31, 1, 3, -1]
a.insert(13, atIndex: 2)
a // [31, 1, 13, 3, -1]
a.removeAll()
a // 0 elements
var aa = [10]
var bb = [[-1],[-2],[-3]]
var cc = aa.join(bb)
cc // [-1, 10, -2, 10, -3]
-1 + 10 + -2 + 10 + -3 // 14
var dd = cc.reduce(0, combine: {(Int u, Int t)->Int in u+t})
dd // 14
dd = cc.reduce(0, combine: {$0 + $1})
dd // 14
-1 * 10 * -2 * 10 * -3 // -600
dd = cc.reduce(1, combine: {(Int u, Int t)->Int in u*t})
dd // -600
cc.reduce(1, combine: {$0 * $1})
dd // -600
cc.sort(>) // [10, 10, -1, -2, -3]
cc // [10, 10, -1, -2, -3]
cc.sort(<) // [-3, -2, -1, 10, 10]
cc // [-3, -2, -1, 10, 10]
cc = [5, 2, 4, 8]
cc.sorted(<) // [2, 4, 5, 8]
cc // unchanged = [5, 2, 4, 8]
cc.sorted(>) // [8, 5, 4, 2]
cc // unchanged = [5, 2, 4, 8]
cc = [5, 2, 4, 8, 1, 3, 6, 7]
let ee = cc.sorted( {(Int v, Int w) -> Bool in
if (v % 2) == 0 && (w % 2) != 0 { // even, odd
return true
}
else if (v % 2) != 0 && (w % 2) == 0 { // odd, even
return false
}
else if (v % 2) == 0 && (w % 2) == 0 { // even, even
return v < w
}
else { // if (v % 2) != 0 && (w % 2) != 0 // odd, odd
return w > v
}
})
ee // [2, 4, 6, 8, 1, 3, 5, 7]
let ff = ee.map( {100 + $0} )
ff // [102, 104, 106, 108, 101, 103, 105, 107]
cc = [1, 2, 3, 4]
cc.reverse() // [4, 3, 2, 1]
cc // unchanged = [1, 2, 3, 4]
let gg = cc.filter({$0 % 2 == 0})
gg // [2, 4]
let hh = cc.filter({$0 % 2 != 0})
hh // [1, 3]
cc = [1, 2, 3, 4]
cc.replaceRange( Range(start: 1, end: 3), with: [5,6])
cc // [1, 5, 6, 4]
cc.splice([13, 14], atIndex: 2)
cc // [1, 5, 13, 14, 6, 4]
cc.removeRange(Range(start: 2, end: 4))
cc // [1, 5, 6, 4]
//
// a.getMirror() // {{1, 2, 3}}