Skip to content

Instantly share code, notes, and snippets.

@rxwei
Last active July 23, 2017 10:10
Show Gist options
  • Save rxwei/b3e0b644cae8f15fc8da69cea9776bd1 to your computer and use it in GitHub Desktop.
Save rxwei/b3e0b644cae8f15fc8da69cea9776bd1 to your computer and use it in GitHub Desktop.
Staged Y combinator with NNKit
/// Y combinator
func fix<D, R>(
_ f: @escaping (Rep<(D) -> R>) -> Rep<(D) -> R>) -> Rep<(D) -> R> {
return lambda { d in f(fix(f))[d] }
}
let fac: Rep<(Int) -> Int> = fix { f in
lambda { (n: Rep<Int>) in
.if(n == 0, then: ^1, else: n * f[n - 1])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment