Created
March 8, 2026 13:37
-
-
Save orzklv/bf8ed1d5e528eec077822da8357682b2 to your computer and use it in GitHub Desktop.
Attempt on Functor in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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