Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let v1 = vec!(1, 2, 3);
- let v2 = vec!(4, 5, 6);
- let r = Vec::zipped(v1, v2, |a, b| a + b);
- println!("{}", r.len())
- }
- trait Foldable<T> {
- fn fold_right<R>(self, acc: R, f: impl Fn(R, &T) -> R) -> R;
- }
- impl <T> Foldable<T> for Vec<T> {
- fn fold_right<R>(self, acc: R, f: impl Fn(R, &T) -> R) -> R {
- return self.iter().fold(acc, |r, t| f(r, t));
- }
- }
- trait ZipResult<T> {
- fn zipped<A, B>(first: impl Foldable<A>, second: impl Foldable<B>, op: impl Fn(&A, &B) -> T) -> Self;
- }
- impl <T> ZipResult<T> for Vec<T> {
- fn zipped<A, B>(first: impl Foldable<A>, second: impl Foldable<B>, op: impl Fn(&A, &B) -> T) -> Vec<T> {
- return Vec::new();
- }
- }
- trait Zippable<A> {
- fn zip_with<B, T, R: ZipResult<T>>(self, other: impl Foldable<B>, f: impl Fn(&A, &B) -> T) -> R;
- }
- impl <F, A> Zippable<A> for F where F: Foldable<A> {
- fn zip_with<B, T, R: ZipResult<T>>(self, other: impl Foldable<B>, f: impl Fn(&A, &B) -> T) -> R {
- return R::zipped(self, other, f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement