Advertisement
Guest User

Untitled

a guest
Oct 7th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. const std = @import("std");
  2.  
  3. const warn = std.debug.warn;
  4. const allocator = std.debug.global_allocator;
  5.  
  6. fn foo(n: u32) u32 {
  7.     return n * 2;
  8. }
  9.  
  10. pub fn main() !void {
  11.  
  12.     var handle = try async<allocator> slow_action();
  13.    
  14.     var mapped = try async<allocator> map(u32, u32, handle, foo);
  15.  
  16.     resume mapped;
  17. }
  18.  
  19. async fn map(comptime T: type, comptime U: type, p: promise-> T, f: fn(T) U) U {
  20.     var result = await p;
  21.  
  22.     return f(result);
  23. }
  24.  
  25. async fn slow_action() u32 {
  26.     var n: usize = 0;
  27.  
  28.     while(n < 10): (n += 1) {
  29.         suspend;
  30.     }
  31.  
  32.     return 1;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement