Skip to content

Instantly share code, notes, and snippets.

@kyouko-taiga
Last active July 13, 2018 06:29
Show Gist options
  • Save kyouko-taiga/fd4cec8341b9f59d629034d2b8959d08 to your computer and use it in GitHub Desktop.
Save kyouko-taiga/fd4cec8341b9f59d629034d2b8959d08 to your computer and use it in GitHub Desktop.
How to do dumb things with Swift
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