97 lines
1.8 KiB
C
97 lines
1.8 KiB
C
|
|
|
|
#include "stdio.h"
|
|
#include "stdlib.h"
|
|
#include "stdint.h"
|
|
#include "string.h"
|
|
#include "main.h"
|
|
#include "lambda.h"
|
|
|
|
typedef struct test_struct{
|
|
int a;
|
|
int b;
|
|
int (*sub)(int a, int b);
|
|
int (*add)(int a, int b);
|
|
int (*sum)(struct test_struct* self);
|
|
} test_struct;
|
|
|
|
|
|
lambda_use
|
|
|
|
|
|
|
|
|
|
|
|
void run_callback(int (*fun)(int a, int b)) {
|
|
int a = fun(1, 2);
|
|
printf("a = %d\n", a);
|
|
}
|
|
|
|
|
|
|
|
static test_struct test_struct_var = {
|
|
.a = 1,
|
|
.b = 2,
|
|
.sub = lambda(int(int a, int b) {
|
|
return a - b;
|
|
}),
|
|
.add = lambda(int(int a, int b) {
|
|
return a + b;
|
|
}),
|
|
.sum = lambda(int(struct test_struct* self) {
|
|
return self->a + self->b;
|
|
})
|
|
};
|
|
|
|
|
|
|
|
|
|
int thread_fun(void* t)
|
|
{
|
|
void (*fun1)(int a, int b);
|
|
fun1 = lambda(void(int a, int b) {
|
|
printf("a = %d, b = %d\n", a, b);
|
|
});
|
|
fun1(1, 2);
|
|
void (*fun2)(int a, int b);
|
|
fun2 = lambda(void(int a, int b) {
|
|
printf("a = %d, b = %d a*b=%d\n", a, b, a * b);
|
|
});
|
|
fun2(3, 4);
|
|
void (*fun3)(void);
|
|
fun3 = lambda(void(void) {
|
|
int a = 3;
|
|
int b = 4;
|
|
printf("a = %d, b = %d a*b=%d\n", a, b, a * b);
|
|
});
|
|
fun3();
|
|
void (*fun4)();
|
|
fun4 = lambda(void() {
|
|
int a = 5, b = 6;
|
|
printf("a = %d, b = %d a*b=%d\n", a, b, a * b);
|
|
});
|
|
fun4();
|
|
unsigned int (*fun5)(int a, int b);
|
|
fun5 = lambda(unsigned int (int a,int b) {
|
|
printf("a = %d, b = %d a*b=%d\n", a, b, a * b);
|
|
return 6;
|
|
});
|
|
fun5(3, 4);
|
|
|
|
run_callback(lambda(int(int a, int b) {
|
|
return a * b;
|
|
}));
|
|
run_callback(lambda(int(int a, int b) {
|
|
return a * b * 10;
|
|
}));
|
|
|
|
printf("test_struct_var.sub(1,2) = %d\n", test_struct_var.sub(1, 2));
|
|
printf("test_struct_var.add(1,2) = %d\n", test_struct_var.add(1, 2));
|
|
printf("test_struct_var.sum(&test_struct_var) = %d\n", test_struct_var.sum(&test_struct_var));
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
|