-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphabetize.swift
55 lines (45 loc) · 1.26 KB
/
alphabetize.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
48
49
50
51
52
53
54
55
import Foundation
func main() {
let inputImportLines: [String] = readInputStringLines()
guard inputImportLines.isEmpty == false else {
return
}
sortImports(inputImportLines).forEach {
print($0)
}
}
func sortImports(_ inputLines: [String]) -> [String] {
let systemImports: Set<String>
= Set([
"Foundation",
"UIKit",
"CoreML"
])
let inputImports: Set<String>
= inputLines
.filter { $0.starts(with: "import ") }
.map { $0.trimStart(by: "import ") }
|> Set.init
let usedSystemImports: Set<String> = inputImports.intersection(systemImports)
let usedCustomImports: Set<String> = inputImports.subtracting(usedSystemImports)
return usedSystemImports.sorted().map { "import \($0)" }
+ [""]
+ usedCustomImports.sorted().map { "import \($0)" }
}
infix operator |>: AdditionPrecedence
func |> <T, U>(value: T, function: ((T) -> U)) -> U {
return function(value)
}
extension String {
func trimStart(by toTrim: String) -> String {
return String(self[self.index(self.startIndex, offsetBy: toTrim.count)...])
}
}
func readInputStringLines() -> [String] {
var inputStringList: [String] = []
while let inputString: String = readLine() {
inputStringList.append(inputString)
}
return inputStringList
}
main()