SHARE
TWEET

Untitled

a guest Feb 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. trait Church {
  2.     fn eval<T, E: Fn(T) -> T>(&self, t: T, e: &E) -> T;
  3. }
  4.  
  5. fn foo<C: Church>(c: C) {
  6.     let double = |n: u64| -> u64 { n * 2 };
  7.     let excitedly = |s: String| -> String {
  8.         let mut result = String::new();
  9.         result.push_str(&s);
  10.         result.push_str("!");
  11.         result
  12.     };
  13.     println!("{}", c.eval(10, &double));
  14.     println!("{}", c.eval("hello".to_string(), &excitedly));
  15. }
  16.  
  17. struct Zero;
  18. impl Church for Zero {
  19.     fn eval<T, E: Fn(T) -> T>(&self, t: T, _e: &E) -> T {
  20.         t
  21.     }
  22. }
  23.  
  24. struct Succ<N: Church>(N);
  25. impl<N: Church> Church for Succ<N> {
  26.     fn eval<T, E: Fn(T) -> T>(&self, t: T, e: &E) -> T {
  27.         e(self.0.eval(t, &e))
  28.     }
  29. }
  30.  
  31. fn main() {
  32.     foo(Zero {});
  33.     foo(Succ(Zero));
  34.     for i in 2..5 {
  35.         struct N(u64);
  36.         impl Church for N {
  37.             fn eval<T, E: Fn(T) -> T>(&self, t: T, e: &E) -> T {
  38.                 let mut result = t;
  39.                 for _ in 0..self.0 {
  40.                     result = e(result);
  41.                 }
  42.                 result
  43.             }
  44.         }
  45.         foo(N(i));
  46.     }
  47. }
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