Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
Created February 6, 2025 04:57
Show Gist options
  • Save ClarkeRemy/d579486a800ff3580508dee56c0ff6d4 to your computer and use it in GitHub Desktop.
Save ClarkeRemy/d579486a800ff3580508dee56c0ff6d4 to your computer and use it in GitHub Desktop.
looking into fixpoint combinator in rust
// https://docs.rs/rust2fun/latest/src/rust2fun/combinator.rs.html#399-417
trait Rec<T, R> {
fn apply(&self, x: T) -> R;
}
impl<T, R, F> Rec<T, R> for F
where
F: Fn(&dyn Rec<T, R>, T) -> R,
{
fn apply(&self, x: T) -> R {
self(self, x)
}
}
pub fn fix<T, R, F>(f: F, x: T) -> R
where
F: Fn(&dyn Fn(T) -> R, T) -> R,
{
(|rec: &dyn Rec<T, R>, y| f(&|z| rec.apply(z), y)).apply(x)
}
const NONE : fn(())->() = |()| fix::<_,(),_>(|none : &dyn Fn(())->() , ()| none(()), ());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment