-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray+Scala.swift
47 lines (43 loc) · 1004 Bytes
/
Array+Scala.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//: Playground - noun: a place where people can play
import Foundation
extension Array where Element: Comparable, Element: Equatable {
var head: Element? {
return first
}
var tail: [Element] {
if count == 0 {
return self
} else {
return Array(self[1..<count])
}
}
func concat(list: [Element]) -> [Element] {
return self + list
}
func drop(at: Int) -> [Element] {
if count == 0 {
return self
} else {
if at == 0 {
return tail.drop(at: at - 1)
} else {
return [head!] + (tail.drop(at: at - 1))
}
}
}
var quickSorted: [Element] {
if count < 2 {
return self
} else {
return [self.min()!] + drop(at: index(of: self.min()!)!).quickSorted
}
}
}
let a = [1,2,3,10,5,8]
a[0..<a.count]
a.head
a.sorted()
a.max()
a.quickSorted
a.drop(at: 2)
a.drop(at: 3)