SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type Context = i32;
  2.  
  3. fn run_sync_1(ctx: Context, next: impl 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: impl 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: impl Fn(Context) -> Context) -> Context {
  16.     ctx - 1
  17. }
  18.  
  19. fn main() {
  20.     // Sets to 1
  21.     let chain_1 = |context, end| {
  22.         run_sync_1(context, |context| {
  23.             run_sync_2(context, |context| {
  24.                 run_sync_3(context, end)
  25.             })
  26.         })
  27.     };
  28.    
  29.     // Adds 3
  30.     let chain_2 = |context, end| {
  31.         run_sync_1(context, |context| {
  32.             run_sync_1(context, |context| {
  33.                 run_sync_1(context, end)
  34.             })
  35.         })
  36.     };
  37.  
  38.     // ((3 + 1) * 2) - 1 = 7
  39.     println!("results: {}", chain_2(0, |context| {
  40.         chain_1(context, |_| {
  41.             panic!("Error!")
  42.         })
  43.     }));
  44. }
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