Skip to content

Instantly share code, notes, and snippets.

@CodaFi
Last active November 29, 2021 18:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodaFi/15146202629d1832eb2b to your computer and use it in GitHub Desktop.
Save CodaFi/15146202629d1832eb2b to your computer and use it in GitHub Desktop.
The Y Combinator in Swift
public func unsafeCoerce<A, B>(_ x : A) -> B {
return unsafeBitCast(x, to: B.self)
}
func Y<A>(_ f : @escaping (A) -> A) -> A {
return { (x : @escaping (A) -> A) in
return f((x(unsafeCoerce(x))))
}({ (x : A) -> A in
let fn : (A) -> A = unsafeCoerce(x)
return f(fn(x))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment