Skip to content

Instantly share code, notes, and snippets.

@tkersey
Forked from CodaFi/Y.swift
Created November 29, 2021 18:19
Show Gist options
  • Save tkersey/8fc95487b3e98cc7408eff9f87102c9e to your computer and use it in GitHub Desktop.
Save tkersey/8fc95487b3e98cc7408eff9f87102c9e 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