SHARE
TWEET

Untitled

a guest May 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.     let v1 = vec!(1, 2, 3);
  3.     let v2 = vec!(4, 5, 6);
  4.     let r = Vec::zipped(v1, v2, |a, b| a + b);
  5.     println!("{}", r.len())
  6. }
  7.  
  8. trait Foldable<T> {
  9.     fn fold_right<R>(self, acc: R, f: impl Fn(R, &T) -> R) -> R;
  10. }
  11.  
  12. impl <T> Foldable<T> for Vec<T> {
  13.     fn fold_right<R>(self, acc: R, f: impl Fn(R, &T) -> R) -> R {
  14.         return self.iter().fold(acc, |r, t| f(r, t));
  15.     }
  16. }
  17.  
  18. trait ZipResult<T> {
  19.     fn zipped<A, B>(first: impl Foldable<A>, second: impl Foldable<B>, op: impl Fn(&A, &B) -> T) -> Self;
  20. }
  21.  
  22. impl <T> ZipResult<T> for Vec<T> {
  23.     fn zipped<A, B>(first: impl Foldable<A>, second: impl Foldable<B>, op: impl Fn(&A, &B) -> T) -> Vec<T> {
  24.         return Vec::new();
  25.     }
  26. }
  27.  
  28. trait Zippable<A> {
  29.     fn zip_with<B, T, R: ZipResult<T>>(self, other: impl Foldable<B>, f: impl Fn(&A, &B) -> T) -> R;
  30. }
  31.  
  32. impl <F, A> Zippable<A> for F where F: Foldable<A> {
  33.     fn zip_with<B, T, R: ZipResult<T>>(self, other: impl Foldable<B>, f: impl Fn(&A, &B) -> T) -> R {
  34.         return R::zipped(self, other, f);
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top