Last active
July 13, 2018 06:29
-
-
Save kyouko-taiga/fd4cec8341b9f59d629034d2b8959d08 to your computer and use it in GitHub Desktop.
How to do dumb things with Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator β‘οΈ: AdditionPrecedence | |
enum π<π€>: Collection, CustomStringConvertible { | |
indirect case π(π€, π) | |
case π« | |
func makeIterator() -> AnyIterator<π€> { | |
var π = self | |
return AnyIterator { | |
switch π { | |
case .π«: | |
return nil | |
case .π(let π, let π¦): | |
π = π¦ | |
return π | |
} | |
} | |
} | |
var startIndex: Int { return 0 } | |
var endIndex : Int { | |
var βΉοΈ = 0 | |
var π = self | |
while case .π(_, let π¦) = π { | |
βΉοΈ += 1 | |
π = π¦ | |
} | |
return βΉοΈ | |
} | |
func index(after βΉοΈ: Int) -> Int { | |
return βΉοΈ + 1 | |
} | |
subscript(βΉοΈ: Int) -> π€ { | |
var π = self | |
var π: π€? = nil | |
for _ in 0 ... βΉοΈ { | |
guard case .π(let π, let π¦) = π else { fatalError() } | |
π = π | |
π = π¦ | |
} | |
return π! | |
} | |
var description: String { return Array(self).description } | |
static func β‘οΈ(π: π, π: π) -> π { | |
switch π { | |
case .π«: | |
return π | |
case .π(let π, let π¦): | |
return .π(π, π¦ β‘οΈ π) | |
} | |
} | |
} | |
func πΆ<π€>(_ π: π€) -> π<π€.Element> where π€: Collection, π€.Element: Comparable { | |
guard !π.isEmpty else { return .π« } | |
guard π.count > 1 else { return .π(π.first!, .π«) } | |
let π = π.first! | |
return πΆ(π.dropFirst().filter { $0 < π }) | |
β‘οΈ .π(π, .π«) | |
β‘οΈ πΆ(π.dropFirst().filter { $0 > π }) | |
} | |
print(πΆ([2, 1, 3])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment