Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Context = i32;
- fn run_sync_1(ctx: Context, next: &Fn(Context) -> Context) -> Context {
- let ctx_new = ctx + 1;
- next(ctx_new)
- }
- fn run_sync_2(ctx: Context, next: &Fn(Context) -> Context) -> Context {
- let ctx_new = ctx * 2;
- next(ctx_new)
- }
- fn run_sync_3(ctx: Context, _next: &Fn(Context) -> Context) -> Context {
- ctx - 1
- }
- type ChainType<T> = fn(T, &Fn(T) -> T) -> T;
- fn main() {
- // Type of impl Fn(Context, impl Fn(Context) -> Context) -> Context
- // Sets to 1
- fn chain_a(context: Context, end: &Fn(Context) -> Context) -> Context {
- run_sync_1(context, &|context| {
- run_sync_2(context, &|context| {
- run_sync_3(context, end)
- })
- })
- };
- let chain_1: ChainType<Context> = chain_a;
- // Adds 3
- fn chain_b(context: Context, end: &Fn(Context) -> Context) -> Context {
- run_sync_1(context, &|context| {
- run_sync_1(context, &|context| {
- run_sync_1(context, end)
- })
- })
- };
- let chain_2: ChainType<Context> = chain_b;
- // ((3 + 1) * 2) - 1 = 7
- println!("results: {}", chain_2(0, &|context| {
- chain_1(context, &|_| {
- panic!("Error!")
- })
- }));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement