Skip to content

Instantly share code, notes, and snippets.

View guoye-zhang's full-sized avatar

Guoye Zhang guoye-zhang

View GitHub Profile
@guoye-zhang
guoye-zhang / Peano.swift
Created January 20, 2019 21:57
Representing Peano integers using only Swift Optional
func toPeano(_ n: Int) -> Any {
precondition(n >= 0)
if n == 0 {
return nil as Any?
} else {
return toPeano(n - 1) as Any?
}
}
protocol OptionalProtocol {
@guoye-zhang
guoye-zhang / SwiftIf.swift
Last active January 17, 2021 03:40
Implementing if without using if in Swift
func i(_ condition: Bool, then: () -> ()) -> (() -> ()) -> () {
func run(_ condition: Bool) -> (() -> ()) -> () {
return { _ = condition && $0() as Any is () }
}
run(condition)(then)
return run(!condition)
}
// Usage:
let els = i(true) {