Advertisement
Guest User

Untitled

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