Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. type Context = i32;
  2.  
  3. fn run_sync_1(ctx: Context, next: &Fn(Context) -> Context) -> Context {
  4. let ctx_new = ctx + 1;
  5.  
  6. next(ctx_new)
  7. }
  8.  
  9. fn run_sync_2(ctx: Context, next: &Fn(Context) -> Context) -> Context {
  10. let ctx_new = ctx * 2;
  11.  
  12. next(ctx_new)
  13. }
  14.  
  15. fn run_sync_3(ctx: Context, _next: &Fn(Context) -> Context) -> Context {
  16. ctx - 1
  17. }
  18.  
  19. type ChainType<T> = fn(T, &Fn(T) -> T) -> T;
  20.  
  21. fn main() {
  22. // Type of impl Fn(Context, impl Fn(Context) -> Context) -> Context
  23.  
  24. // Sets to 1
  25. fn chain_a(context: Context, end: &Fn(Context) -> Context) -> Context {
  26. run_sync_1(context, &|context| {
  27. run_sync_2(context, &|context| {
  28. run_sync_3(context, end)
  29. })
  30. })
  31. };
  32. let chain_1: ChainType<Context> = chain_a;
  33.  
  34. // Adds 3
  35. fn chain_b(context: Context, end: &Fn(Context) -> Context) -> Context {
  36. run_sync_1(context, &|context| {
  37. run_sync_1(context, &|context| {
  38. run_sync_1(context, end)
  39. })
  40. })
  41. };
  42. let chain_2: ChainType<Context> = chain_b;
  43.  
  44. // ((3 + 1) * 2) - 1 = 7
  45. println!("results: {}", chain_2(0, &|context| {
  46. chain_1(context, &|_| {
  47. panic!("Error!")
  48. })
  49. }));
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement