SHARE
TWEET

welcome to hell

a guest Sep 11th, 2019 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //gcc 5.4.0
  2. // Example with lambda  (needs --std=gnu99)
  3. #include <stdio.h>
  4.  
  5.  
  6. float l[]={ 3.141, 6.02, 42.0, 0.7 };
  7.  
  8. #define lambda(lambda$_ret, lambda$_args, lambda$_body) \
  9.         ({                                              \
  10.                 lambda$_ret lambda$__anon$ lambda$_args \
  11.                 {return lambda$_body;}                  \
  12.                         &lambda$__anon$;                \
  13.         })
  14.  
  15. #define len(x) (sizeof(x)/sizeof(x[0]))
  16.  
  17. typedef void* auto_;
  18.  
  19. float apply(float (*fn)(float inp), float* l, int n)
  20. {
  21.         int i;
  22.         float avg=0.0;
  23.         printf("n=%i\n", n);
  24.         for (i=0;i<n;i++)
  25.         {
  26.                 avg+=fn(l[i]);
  27.                 printf("Running sum=%f\n", avg);
  28.         }
  29.  
  30.         return avg/n;
  31. }
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.         auto_ fn = lambda(float,(float x),2*x);
  36.  
  37.         printf("%f\n",apply(fn, l, len(l)));
  38.         printf("%f\n",apply(lambda(float,(float x),2*x), l, len(l)));
  39.         printf("%f\n",apply(lambda(float,(float x),x/3.0), l, len(l)));
  40.  
  41.         return 0;
  42. }
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