Skip to content

Instantly share code, notes, and snippets.

@orzklv
Created March 8, 2026 13:37
Show Gist options
  • Select an option

  • Save orzklv/bf8ed1d5e528eec077822da8357682b2 to your computer and use it in GitHub Desktop.

Select an option

Save orzklv/bf8ed1d5e528eec077822da8357682b2 to your computer and use it in GitHub Desktop.
Attempt on Functor in Rust
trait Higher<U> {
type C;
type T;
}
impl<U, T> Higher<U> for Vec<T> {
type C = T;
type T = Vec<U>;
}
trait Functor<U>: Higher<U> {
fn fmap<F>(&self, f: F) -> Self::T
where
F: Fn(&Self::C) -> U;
}
impl<T, U> Functor<U> for Vec<T> {
fn fmap<F>(&self, f: F) -> Vec<U>
where
F: Fn(&T) -> U,
{
let mut result = Vec::with_capacity(self.len());
for value in self {
result.push(f(value));
}
result
}
}
fn main() {
let example = vec!["some"];
let transform = example.fmap(|v| v.to_string());
println!("{:?}", transform);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment