Below are 12 complete example programs. The first six show how you can build “composed functions,” functors, and closures using the regular (non‐optimized) versions from pcdf.h (using, for example, _runc_0). The following six examples are analogous but use the experimental “optimized” version (here _experiemtial_runc_0) that employs JIT‐generated direct calls for extra speed. (You can choose any variant—you might use other hex variants if needed.)
Each example creates a small “schedule” (a list of function pointers plus an argument) and then invokes the scheduler to “close over” that argument when calling the functions. (In C this is one way to simulate function composition, functors, and closures.)
---
### Regular Versions
#### 1. Function Composition Example 1
_Compose two functions that modify an integer (increment then square)._
```c
#include <stdio.h>
#include "pcdf.h"
void inc(autolike data) {
int *val = (int*)data;
(*val)++;
printf("After inc: %d\n", *val);
}
void square(autolike data) {
int *val = (int*)data;
*val = (*val) * (*val);
printf("After square: %d\n", *val);
}
int main(void) {
int x = 3;
/* Create an array of 2 function pointers.
Note: schedule.list.ends points to the last element. */
pcd funcs[2] = { inc, square };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 1;
schedule.argument = &x;
_runc_0(&schedule);
printf("Final result: %d\n", x);
return 0;
}
```
---
#### 2. Function Composition Example 2
_Compose three functions that build a string by concatenating “Hello”, a space, and “World”._
```c
#include <stdio.h>
#include <string.h>
#include "pcdf.h"
void add_hello(autolike data) {
char *str = (char*)data;
strcat(str, "Hello");
printf("After add_hello: %s\n", str);
}
void add_space(autolike data) {
char *str = (char*)data;
strcat(str, " ");
printf("After add_space: %s\n", str);
}
void add_world(autolike data) {
char *str = (char*)data;
strcat(str, "World");
printf("After add_world: %s\n", str);
}
int main(void) {
char buffer[50] = "";
pcd funcs[3] = { add_hello, add_space, add_world };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 2;
schedule.argument = buffer;
_runc_0(&schedule);
printf("Final string: %s\n", buffer);
return 0;
}
```
---
#### 3. Functor Example 1
_Create a functor that “holds” a prefix string and a number, then prints them._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
int value;
} Functor;
void print_functor(autolike data) {
Functor *f = (Functor*)data;
printf("%s: %d\n", f->prefix, f->value);
}
int main(void) {
Functor f = { "Functor Example 1", 42 };
pcd funcs[1] = { print_functor };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs; // single function (workflow == ends)
schedule.argument = &f;
_runc_0(&schedule);
return 0;
}
```
---
#### 4. Functor Example 2
_Create a functor that modifies internal state (increments a counter) each time it’s called._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int counter;
} IncrementFunctor;
void increment(autolike data) {
IncrementFunctor *f = (IncrementFunctor*)data;
f->counter++;
printf("Counter incremented to: %d\n", f->counter);
}
int main(void) {
IncrementFunctor fun = { 0 };
pcd funcs[2] = { increment, increment };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 1;
schedule.argument = &fun;
_runc_0(&schedule);
return 0;
}
```
---
#### 5. Closure Example 1
_Simulate a closure by “capturing” a message string that the called function prints._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *message;
} ClosureData;
void print_closure(autolike data) {
ClosureData *c = (ClosureData*)data;
printf("Closure says: %s\n", c->message);
}
int main(void) {
ClosureData closure = { "Hello from closure!" };
pcd funcs[1] = { print_closure };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &closure;
_runc_0(&schedule);
return 0;
}
```
---
#### 6. Closure Example 2
_Create a closure that “captures” a number and an offset, then adds them and prints the result._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int number;
int offset;
} AddClosure;
void add_and_print(autolike data) {
AddClosure *ac = (AddClosure*)data;
int result = ac->number + ac->offset;
printf("Result of adding: %d + %d = %d\n", ac->number, ac->offset, result);
}
int main(void) {
AddClosure ac = { 10, 5 };
pcd funcs[1] = { add_and_print };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = ∾
_runc_0(&schedule);
return 0;
}
```
---
### Optimized (Fastest) Versions
Each of the following examples mirrors one of the above but uses the optimized experimental function (here _experiemtial_runc_0) that employs JIT‐generated direct calls.
#### 7. Optimized Function Composition Example 1
_(Increment then square an integer, using the optimized call.)_
```c
#include <stdio.h>
#include "pcdf.h"
void inc(autolike data) {
int *val = (int*)data;
(*val)++;
printf("Optimized - After inc: %d\n", *val);
}
void square(autolike data) {
int *val = (int*)data;
*val = (*val) * (*val);
printf("Optimized - After square: %d\n", *val);
}
int main(void) {
int x = 4;
pcd funcs[2] = { inc, square };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 1;
schedule.argument = &x;
_experiemtial_runc_0(&schedule);
printf("Optimized - Final result: %d\n", x);
return 0;
}
```
---
#### 8. Optimized Function Composition Example 2
_(Concatenate a string using three functions, with the optimized scheduler.)_
```c
#include <stdio.h>
#include <string.h>
#include "pcdf.h"
void add_hello(autolike data) {
char *str = (char*)data;
strcat(str, "Hello");
printf("Optimized - After add_hello: %s\n", str);
}
void add_space(autolike data) {
char *str = (char*)data;
strcat(str, " ");
printf("Optimized - After add_space: %s\n", str);
}
void add_world(autolike data) {
char *str = (char*)data;
strcat(str, "World");
printf("Optimized - After add_world: %s\n", str);
}
int main(void) {
char buffer[50] = "";
pcd funcs[3] = { add_hello, add_space, add_world };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 2;
schedule.argument = buffer;
_experiemtial_runc_0(&schedule);
printf("Optimized - Final string: %s\n", buffer);
return 0;
}
```
---
#### 9. Optimized Functor Example 1
_(A functor that prints a prefix and a number using the optimized scheduler.)_
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
int value;
} Functor;
void print_functor(autolike data) {
Functor *f = (Functor*)data;
printf("Optimized %s: %d\n", f->prefix, f->value);
}
int main(void) {
Functor f = { "Optimized Functor Example 1", 84 };
pcd funcs[1] = { print_functor };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &f;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
#### 10. Optimized Functor Example 2
_(A functor that increments an internal counter, scheduled with the optimized call.)_
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int counter;
} IncrementFunctor;
void increment(autolike data) {
IncrementFunctor *f = (IncrementFunctor*)data;
f->counter++;
printf("Optimized - Counter incremented to: %d\n", f->counter);
}
int main(void) {
IncrementFunctor fun = { 10 };
pcd funcs[2] = { increment, increment };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 1;
schedule.argument = &fun;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
#### 11. Optimized Closure Example 1
_(A closure that “captures” a message string, using the optimized scheduler.)_
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *message;
} ClosureData;
void print_closure(autolike data) {
ClosureData *c = (ClosureData*)data;
printf("Optimized - Closure says: %s\n", c->message);
}
int main(void) {
ClosureData closure = { "Optimized: Hello from closure!" };
pcd funcs[1] = { print_closure };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &closure;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
#### 12. Optimized Closure Example 2
_(A closure that “captures” a number and an offset, adds them, and prints the result via the optimized scheduler.)_
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int number;
int offset;
} AddClosure;
void add_and_print(autolike data) {
AddClosure *ac = (AddClosure*)data;
int result = ac->number + ac->offset;
printf("Optimized - Result: %d + %d = %d\n", ac->number, ac->offset, result);
}
int main(void) {
AddClosure ac = { 20, 7 };
pcd funcs[1] = { add_and_print };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = ∾
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
Each of these examples is self-contained. Simply compile each (linking with pthread, OpenMP, or AVX2 as needed by your system) along with the provided **pcdf.h** to see the “composed,” functor‐style, or closure–like behavior in action.
Below are several examples that “bind” a function together with its arguments—mimicking Python’s `functools.partial`—by packaging them into a structure. In these examples the structure (here named `Partial`) holds both a function pointer and its pre‐bound argument. A helper function (here called `partial_caller`) then “calls” the bound function with its bound argument. The first three examples use the regular scheduler (via `_runc_0`), and the following three use the optimized (JIT) scheduler (via `_experiemtial_runc_0`).
---
### Regular (Non‐Optimized) Partial Examples
#### 1. Partial Multiplication
_This example binds a multiplication function that multiplies an integer by a fixed factor._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int multiplier;
int *value;
} MultiplyArgs;
void multiply(autolike data) {
MultiplyArgs *args = (MultiplyArgs*)data;
*(args->value) *= args->multiplier;
printf("After multiply: %d\n", *(args->value));
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
int x = 5;
MultiplyArgs margs = { 3, &x }; // Multiply x by 3
Partial p = { multiply, &margs };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs; // Single function in the workflow
schedule.argument = &p;
_runc_0(&schedule);
return 0;
}
```
---
#### 2. Partial Printing with a Prefix
_This example binds a function that prints a message preceded by a fixed prefix._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
const char *msg;
} PrintArgs;
void print_message(autolike data) {
PrintArgs *p = (PrintArgs*)data;
printf("%s%s\n", p->prefix, p->msg);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
PrintArgs pargs = { "Notice: ", "This is a partial call." };
Partial p = { print_message, &pargs };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_runc_0(&schedule);
return 0;
}
```
---
#### 3. Partial Toggle (Closure-Like)
_This example binds a function that toggles a flag stored in a structure._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int flag;
} ToggleArgs;
void toggle_flag(autolike data) {
ToggleArgs *args = (ToggleArgs*)data;
args->flag = !args->flag;
printf("Flag is now: %d\n", args->flag);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
ToggleArgs targs = { 0 };
Partial p = { toggle_flag, &targs };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_runc_0(&schedule);
return 0;
}
```
---
### Optimized (JIT-Compiled) Partial Examples
#### 4. Optimized Partial Multiplication
_This version uses the optimized scheduler to perform the multiplication._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int multiplier;
int *value;
} MultiplyArgs;
void multiply(autolike data) {
MultiplyArgs *args = (MultiplyArgs*)data;
*(args->value) *= args->multiplier;
printf("Optimized multiply: %d\n", *(args->value));
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
int x = 7;
MultiplyArgs margs = { 4, &x }; // Multiply x by 4
Partial p = { multiply, &margs };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
#### 5. Optimized Partial Printing with a Prefix
_This optimized version binds the print function with its fixed prefix and message._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
const char *msg;
} PrintArgs;
void print_message(autolike data) {
PrintArgs *p = (PrintArgs*)data;
printf("Optimized: %s%s\n", p->prefix, p->msg);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
PrintArgs pargs = { "Alert: ", "Optimized partial call executed." };
Partial p = { print_message, &pargs };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
#### 6. Optimized Partial Toggle
_This optimized example binds a toggle function that flips a flag._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int flag;
} ToggleArgs;
void toggle_flag(autolike data) {
ToggleArgs *args = (ToggleArgs*)data;
args->flag = !args->flag;
printf("Optimized - Flag is now: %d\n", args->flag);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
ToggleArgs targs = { 1 };
Partial p = { toggle_flag, &targs };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
Each of these examples shows how you can “bind” a function with a fixed argument using a structure—very similar in spirit to Python’s `partial`. You can compile these examples along with **pcdf.h** (and link with OpenMP, pthreads, or AVX2 as required) to see the behavior in action.
Below are 12 complete examples demonstrating three kinds of behavior—function composition, functor, and closure—in a style similar to Python’s `partial`. The first six examples use the regular scheduler (_runc_0) from **pcdf.h**, while the following six use the optimized (JIT‐compiled) scheduler (_experiemtial_runc_0). In the functor and closure examples we “bind” a function with its argument in a simple “Partial” struct, emulating Python’s `functools.partial`.
---
## Regular (Non‐Optimized) Versions
### 1. Function Composition Example 1
_Increment an integer then square it using two function calls._
```c
#include <stdio.h>
#include "pcdf.h"
void inc(autolike data) {
int *x = (int*)data;
(*x)++;
printf("After inc: %d\n", *x);
}
void square(autolike data) {
int *x = (int*)data;
*x = (*x) * (*x);
printf("After square: %d\n", *x);
}
int main(void) {
int value = 3;
pcd funcs[2] = { inc, square };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 1;
schedule.argument = &value;
_runc_0(&schedule);
printf("Final value: %d\n", value);
return 0;
}
```
---
### 2. Function Composition Example 2
_Concatenate parts of a string by calling three functions in sequence._
```c
#include <stdio.h>
#include <string.h>
#include "pcdf.h"
void add_hello(autolike data) {
char *str = (char*)data;
strcat(str, "Hello");
printf("After add_hello: %s\n", str);
}
void add_space(autolike data) {
char *str = (char*)data;
strcat(str, " ");
printf("After add_space: %s\n", str);
}
void add_world(autolike data) {
char *str = (char*)data;
strcat(str, "World");
printf("After add_world: %s\n", str);
}
int main(void) {
char buffer[50] = "";
pcd funcs[3] = { add_hello, add_space, add_world };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 2;
schedule.argument = buffer;
_runc_0(&schedule);
printf("Final string: %s\n", buffer);
return 0;
}
```
---
### 3. Functor Example 1 (Partial Style)
_Bind a printing function with a fixed prefix and value._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
int value;
} PrintData;
void print_with_prefix(autolike data) {
PrintData *pd = (PrintData*)data;
printf("%s%d\n", pd->prefix, pd->value);
}
// Partial struct binds a function with its argument.
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
PrintData pdata = { "Value: ", 42 };
Partial p = { print_with_prefix, &pdata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs; // Single function in workflow.
schedule.argument = &p;
_runc_0(&schedule);
return 0;
}
```
---
### 4. Functor Example 2 (Partial Style)
_Bind a multiplication function that multiplies a number by a fixed factor._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int *value;
int factor;
} MultiplyData;
void multiply(autolike data) {
MultiplyData *md = (MultiplyData*)data;
*(md->value) *= md->factor;
printf("After multiplication: %d\n", *(md->value));
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
int num = 5;
MultiplyData mdata = { &num, 3 }; // Multiply num by 3.
Partial p = { multiply, &mdata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_runc_0(&schedule);
printf("Final result: %d\n", num);
return 0;
}
```
---
### 5. Closure Example 1 (Partial Style)
_Create a closure that toggles a flag, “capturing” its internal state._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int flag;
} ToggleData;
void toggle(autolike data) {
ToggleData *td = (ToggleData*)data;
td->flag = !td->flag;
printf("Flag toggled to: %d\n", td->flag);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
ToggleData tdata = { 0 };
Partial p = { toggle, &tdata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_runc_0(&schedule);
return 0;
}
```
---
### 6. Closure Example 2 (Partial Style)
_Bind a function that adds an offset to a number and prints the result._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int number;
int offset;
} AddData;
void add_offset(autolike data) {
AddData *ad = (AddData*)data;
int result = ad->number + ad->offset;
printf("Sum: %d + %d = %d\n", ad->number, ad->offset, result);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
AddData adata = { 10, 7 };
Partial p = { add_offset, &adata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_runc_0(&schedule);
return 0;
}
```
---
## Optimized (JIT / Fastest) Versions
### 7. Optimized Function Composition Example 1
_Increment an integer then square it using the optimized scheduler._
```c
#include <stdio.h>
#include "pcdf.h"
void inc(autolike data) {
int *x = (int*)data;
(*x)++;
printf("Optimized - After inc: %d\n", *x);
}
void square(autolike data) {
int *x = (int*)data;
*x = (*x) * (*x);
printf("Optimized - After square: %d\n", *x);
}
int main(void) {
int value = 4;
pcd funcs[2] = { inc, square };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 1;
schedule.argument = &value;
_experiemtial_runc_0(&schedule);
printf("Optimized - Final value: %d\n", value);
return 0;
}
```
---
### 8. Optimized Function Composition Example 2
_Concatenate string parts using the optimized scheduler._
```c
#include <stdio.h>
#include <string.h>
#include "pcdf.h"
void add_hello(autolike data) {
char *str = (char*)data;
strcat(str, "Hello");
printf("Optimized - After add_hello: %s\n", str);
}
void add_space(autolike data) {
char *str = (char*)data;
strcat(str, " ");
printf("Optimized - After add_space: %s\n", str);
}
void add_world(autolike data) {
char *str = (char*)data;
strcat(str, "World");
printf("Optimized - After add_world: %s\n", str);
}
int main(void) {
char buffer[50] = "";
pcd funcs[3] = { add_hello, add_space, add_world };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs + 2;
schedule.argument = buffer;
_experiemtial_runc_0(&schedule);
printf("Optimized - Final string: %s\n", buffer);
return 0;
}
```
---
### 9. Optimized Functor Example 1 (Partial Style)
_Bind a printing function with a fixed prefix and value using the optimized scheduler._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
int value;
} PrintData;
void print_with_prefix(autolike data) {
PrintData *pd = (PrintData*)data;
printf("Optimized - %s%d\n", pd->prefix, pd->value);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
PrintData pdata = { "Value: ", 84 };
Partial p = { print_with_prefix, &pdata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
### 10. Optimized Functor Example 2 (Partial Style)
_Bind a multiplication function (to multiply a number by a fixed factor) using the optimized scheduler._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int *value;
int factor;
} MultiplyData;
void multiply(autolike data) {
MultiplyData *md = (MultiplyData*)data;
*(md->value) *= md->factor;
printf("Optimized - After multiplication: %d\n", *(md->value));
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
int num = 6;
MultiplyData mdata = { &num, 4 };
Partial p = { multiply, &mdata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_experiemtial_runc_0(&schedule);
printf("Optimized - Final result: %d\n", num);
return 0;
}
```
---
### 11. Optimized Closure Example 1 (Partial Style)
_Create a closure that toggles a flag using the optimized scheduler._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int flag;
} ToggleData;
void toggle(autolike data) {
ToggleData *td = (ToggleData*)data;
td->flag = !td->flag;
printf("Optimized - Flag toggled to: %d\n", td->flag);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
ToggleData tdata = { 1 };
Partial p = { toggle, &tdata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
### 12. Optimized Closure Example 2 (Partial Style)
_Bind a function that adds an offset to a number and prints the result using the optimized scheduler._
```c
#include <stdio.h>
#include "pcdf.h"
typedef struct {
int number;
int offset;
} AddData;
void add_offset(autolike data) {
AddData *ad = (AddData*)data;
int result = ad->number + ad->offset;
printf("Optimized - Sum: %d + %d = %d\n", ad->number, ad->offset, result);
}
typedef struct {
proceedings func;
autolike bound_arg;
} Partial;
void partial_caller(autolike data) {
Partial *p = (Partial*)data;
p->func(p->bound_arg);
}
int main(void) {
AddData adata = { 15, 10 };
Partial p = { add_offset, &adata };
pcd funcs[1] = { partial_caller };
scad schedule;
schedule.list.workflow = funcs;
schedule.list.ends = funcs;
schedule.argument = &p;
_experiemtial_runc_0(&schedule);
return 0;
}
```
---
Each of these 12 examples is self-contained. Compile them together with **pcdf.h** (and link with OpenMP, pthreads, or AVX2 if needed) to see the function composition, functor, and closure behavior in action—implemented in a Pythonic “partial” style.
Q,
pcdf.h
```c
#pragma once
#ifndef _PCDF_H
# define _PCDF_H
#include <pthread.h>
#include <immintrin.h> // AVX2 intrinsics
#include <omp.h>
#include <sys/mman.h> // For JIT memory allocation
#include <stdint.h>
#include <string.h>
#define REG
#define LIKELY(x) x
#define FORSE_ __attribute__((always_inline))
#define RST
#define OPTB
typedef void *autolike;
typedef void (*proceedings)(autolike);
typedef proceedings pcd;
typedef pcd *proceed_ptr;
typedef proceed_ptr pcdx;
typedef struct {
pcdx workflow;
pcdx ends;
} __scadule__;
typedef struct {
__scadule__ list;
autolike argument;
} scadule;
typedef scadule scad;
#define DEFINE_RUNC(hex, openmp, pthread, avx2, vector) \
FORSE_ inline void _runc_##hex(autolike target) { \
REG scad *A = (scad *)target; \
REG autolike argv = A->argument; \
REG __scadule__ *RST B = &A->list; \
OPTB \
REG pcdx iter = B->workflow; \
REG pcdx *ends = &B->ends; \
if (openmp) { \
#pragma omp parallel for \
for (int i = 0; iter + i <= *ends; i++) { \
(*(iter + i))(argv); \
} \
} else if (pthread) { \
pthread_t threads[16]; \
int i = 0; \
while (iter <= *ends) { \
pthread_create(&threads[i], NULL, (void *(*)(void *))(*iter), argv); \
iter++; \
i++; \
} \
for (int j = 0; j < i; j++) { \
pthread_join(threads[j], NULL); \
} \
} else if (avx2) { \
__m256i vec_arg = _mm256_set1_epi64x((long long)argv); \
while (iter <= *ends) { \
(*iter)((autolike)_mm256_extract_epi64(vec_arg, 0)); \
iter++; \
} \
} else if (vector) { \
#pragma GCC ivdep \
for (; LIKELY(iter <= *ends); iter++) { \
(*iter)(argv); \
} \
} else { \
for (; LIKELY(iter <= *ends); iter++) { \
(*iter)(argv); \
} \
} \
}
DEFINE_RUNC(0, 0, 0, 0, 0)
DEFINE_RUNC(1, 0, 0, 0, 1)
DEFINE_RUNC(2, 0, 0, 1, 0)
DEFINE_RUNC(3, 0, 0, 1, 1)
DEFINE_RUNC(4, 0, 1, 0, 0)
DEFINE_RUNC(5, 0, 1, 0, 1)
DEFINE_RUNC(6, 0, 1, 1, 0)
DEFINE_RUNC(7, 0, 1, 1, 1)
DEFINE_RUNC(8, 1, 0, 0, 0)
DEFINE_RUNC(9, 1, 0, 0, 1)
DEFINE_RUNC(A, 1, 0, 1, 0)
DEFINE_RUNC(B, 1, 0, 1, 1)
DEFINE_RUNC(C, 1, 1, 0, 0)
DEFINE_RUNC(D, 1, 1, 0, 1)
DEFINE_RUNC(E, 1, 1, 1, 0)
DEFINE_RUNC(F, 1, 1, 1, 1)
// Function to generate JIT compiled function for direct function calls
static void (*generate_jit_function(pcd target_func))(autolike) {
uint8_t *code = mmap(NULL, 64, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (code == MAP_FAILED) return NULL;
// x86-64 assembly to call function directly
uint8_t assembly[] = {
0x48, 0x89, 0xF7, // mov rdi, rsi (move argument to correct register)
0x48, 0xB8, // movabs rax, <target_func>
0, 0, 0, 0, 0, 0, 0, 0, // (address placeholder)
0xFF, 0xE0 // jmp rax (jump to function)
};
memcpy(&assembly[5], &target_func, sizeof(target_func));
memcpy(code, assembly, sizeof(assembly));
return (void (*)(autolike))code;
}
#define DEFINE_EXPERIMENTIAL_RUNC(hex, openmp, pthread, avx2, vector) \
FORSE_ inline void _experiemtial_runc_##hex(autolike target) { \
REG scad *A = (scad *)target; \
REG autolike argv = A->argument; \
REG __scadule__ *RST B = &A->list; \
OPTB \
REG pcdx iter = B->workflow; \
REG pcdx *ends = &B->ends; \
\
register autolike fixed_argv asm("rdi") = argv; /* Fix argv in rdi register */ \
\
if (openmp) { \
#pragma omp parallel for \
for (int i = 0; iter + i <= *ends; i++) { \
pcd func = iter[i]; \
__asm__ volatile("" ::: "rdi"); /* Tell compiler not to touch rdi */ \
func(fixed_argv); \
} \
} else if (pthread) { \
pthread_t threads[16]; \
int i = 0; \
while (iter <= *ends) { \
pcd func = generate_jit_function(*iter); /* Use JIT compiled function */ \
__asm__ volatile("" ::: "rdi"); \
pthread_create(&threads[i], NULL, (void *(*)(void *))func, fixed_argv); \
iter++; \
i++; \
} \
for (int j = 0; j < i; j++) { \
pthread_join(threads[j], NULL); \
} \
} else if (avx2) { \
__m256i vec_arg = _mm256_set1_epi64x((long long)fixed_argv); \
while (iter <= *ends) { \
pcd func = generate_jit_function(*iter); /* Use JIT compiled function */ \
__asm__ volatile("" ::: "rdi"); \
func((autolike)_mm256_extract_epi64(vec_arg, 0)); \
iter++; \
} \
} else if (vector) { \
#pragma GCC ivdep \
for (; LIKELY(iter <= *ends); iter++) { \
__builtin_prefetch(iter + 4, 0, 1); /* Prefetch function pointer */ \
pcd func = generate_jit_function(*iter); /* Use JIT compiled function */ \
__asm__ volatile("" ::: "rdi"); \
func(fixed_argv); \
} \
} else { \
for (; LIKELY(iter <= *ends); iter++) { \
__builtin_prefetch(iter + 4, 0, 1); /* Prefetch function pointer */ \
pcd func = generate_jit_function(*iter); /* Use JIT compiled function */ \
__asm__ volatile("" ::: "rdi"); \
func(fixed_argv); \
} \
} \
}
DEFINE_EXPERIMENTIAL_RUNC(0, 0, 0, 0, 0)
DEFINE_EXPERIMENTIAL_RUNC(1, 0, 0, 0, 1)
DEFINE_EXPERIMENTIAL_RUNC(2, 0, 0, 1, 0)
DEFINE_EXPERIMENTIAL_RUNC(3, 0, 0, 1, 1)
DEFINE_EXPERIMENTIAL_RUNC(4, 0, 1, 0, 0)
DEFINE_EXPERIMENTIAL_RUNC(5, 0, 1, 0, 1)
DEFINE_EXPERIMENTIAL_RUNC(6, 0, 1, 1, 0)
DEFINE_EXPERIMENTIAL_RUNC(7, 0, 1, 1, 1)
DEFINE_EXPERIMENTIAL_RUNC(8, 1, 0, 0, 0)
DEFINE_EXPERIMENTIAL_RUNC(9, 1, 0, 0, 1)
DEFINE_EXPERIMENTIAL_RUNC(A, 1, 0, 1, 0)
DEFINE_EXPERIMENTIAL_RUNC(B, 1, 0, 1, 1)
DEFINE_EXPERIMENTIAL_RUNC(C, 1, 1, 0, 0)
DEFINE_EXPERIMENTIAL_RUNC(D, 1, 1, 0, 1)
DEFINE_EXPERIMENTIAL_RUNC(E, 1, 1, 1, 0)
DEFINE_EXPERIMENTIAL_RUNC(F, 1, 1, 1, 1)
// Scheduler structure for function execution
struct {
pcd run;
} scaduler = {
.run = pcdef(target) {_runc_0(target)}
};
#endif
```
now, make each function composition / functor / closer by pcdf.h 2 examples. (total 6 code blocks) and additional 6 optimized faststed ver. (functor and closer can be writed by pythonic `partial` style code)
pcdf.h's optimizing is greatist, every code can be execute and compile. I'll do.
A.
The user asks for 6 code blocks, broken into two groups: function compositions, functors, and closers. Then, they want additional optimized versions, totaling 6 more snippets. I'll need to work with "pcdf.h," which seems to define several runc-related functions and a scheduler struct. I think I'm understanding this right. Each example needs to demonstrate the usage of those functions, and I'll write some optimized versions, probably using Python's partial-style for functors and closers. I'm set to make them efficient!
The user is asking for 12 code blocks in total. First, I need 6 blocks showing function composition, functors, and closures—2 examples for each category. Then, 6 more optimized versions. The term "closer" likely means "closure" since "closure" is the common term in programming, especially in languages like Python with partial. The header "pcdf.h" seems to define macros for concurrent scheduling, and I need to incorporate those to demonstrate composition and performance optimizations. I'll carefully craft the examples in C!
To create the 6 code blocks, I'll start by showing function composition. I'll define two simple functions that process integers and use scadule to schedule their execution via pcdf.h. The scadule struct will contain an array of function pointers, ensuring functions are executed in sequence. The challenge is ensuring workflow points to the array and that ends correctly points to the last element, aligning with the macro usage. From there, I'll create similar blocks for functors and closures. Each code block will demonstrate practical use of pcdf.h macros. Time to get coding!
Okay, I have a clear structure for what I need to do. I will produce 12 code blocks in total, where the first 6 are non-optimized versions, demonstrating function composition, functors, and closures. Each example will show how to use the "pcdf.h" macros in different styles. Then, I'll create optimized versions using experimental macros like _experiemtial_runc_0. Each code block will be a complete program, with proper main() and all necessary ensures (includes, scad, workflow, etc.). I'll make sure every code block is self-contained and compile-ready! Time to break this down into structured examples.
-----
Below are 12 complete C code examples that use the mechanisms defined in your **pcdf.h** header. The first six examples use a “standard” (non‐optimized) runner (calling, for example, _runc_0), while the following six use an “experimental” optimized runner (calling _experiemtial_runc_F). In each group you’ll find two examples for function composition, two “functor” examples (one written in a partial‐application style), and two “closure” examples (again one written in a Python‑style partial).
> **Note:** These examples assume that your **pcdf.h** file is in the same directory and compiles without error. You may compile each example (for instance with GCC) and run them to see the scheduled function calls in action.
---
### **Non‐Optimized Versions (using _runc_0)**
#### **Example 1 – Function Composition (2 functions)**
```c
// Example 1: Function Composition (2 functions) - Non-Optimized Version
// In this example two functions are composed: first one increments an integer,
// then the second doubles it.
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
// Increment the integer by one.
void increment(void *arg) {
int *p = (int *)arg;
(*p)++;
printf("Increment: %d\n", *p);
}
// Double the integer.
void double_val(void *arg) {
int *p = (int *)arg;
*p *= 2;
printf("Double: %d\n", *p);
}
int main(void) {
// Compose the two functions.
pcd funcs[] = { increment, double_val };
scad sched;
int *value = malloc(sizeof(int));
*value = 5;
sched.argument = value;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs + 1; // two functions: index 0 and 1
sched.list = scheduleList;
// Execute using the non-optimized runner.
_runc_0((void *)&sched);
free(value);
return 0;
}
```
---
#### **Example 2 – Function Composition (3 functions)**
```c
// Example 2: Function Composition (3 functions) - Non-Optimized Version
// This example composes three functions that modify a shared integer.
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
// Adds 3 to the integer.
void add_three(void *arg) {
int *p = (int *)arg;
*p += 3;
printf("Add Three: %d\n", *p);
}
// Subtracts 2 from the integer.
void subtract_two(void *arg) {
int *p = (int *)arg;
*p -= 2;
printf("Subtract Two: %d\n", *p);
}
// Multiplies the integer by 4.
void multiply_four(void *arg) {
int *p = (int *)arg;
*p *= 4;
printf("Multiply Four: %d\n", *p);
}
int main(void) {
// Compose functions in order: add_three, subtract_two, multiply_four.
pcd funcs[] = { add_three, subtract_two, multiply_four };
scad sched;
int *value = malloc(sizeof(int));
*value = 10;
sched.argument = value;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs + 2; // three functions (last index 2)
sched.list = scheduleList;
_runc_0((void *)&sched);
free(value);
return 0;
}
```
---
#### **Example 3 – Functor Example (using a struct)**
```c
// Example 3: Functor Example (Non-Optimized Version)
// Here a functor is simulated by a structure with state; the function uses that state.
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
int multiplier;
} Functor;
// The functor function: expects an array [Functor*, int*] in the argument.
void multiply_functor(void *arg) {
void **arr = (void **)arg;
Functor *f = (Functor *)arr[0];
int *val = (int *)arr[1];
*val *= f->multiplier;
printf("Functor multiply: %d\n", *val);
}
int main(void) {
// Create the functor and a value.
Functor *f = malloc(sizeof(Functor));
f->multiplier = 3;
int *value = malloc(sizeof(int));
*value = 7;
// Package the functor and value into an array.
void *args[2] = { f, value };
// Schedule the functor call.
pcd funcs[] = { multiply_functor };
scad sched;
sched.argument = args;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs; // only one function in the workflow
sched.list = scheduleList;
_runc_0((void *)&sched);
free(f);
free(value);
return 0;
}
```
---
#### **Example 4 – Functor with Partial Application (Python‑style)**
```c
// Example 4: Functor with Partial Application (Non-Optimized Version)
// This example simulates partial application by pre-binding a multiplier and an addend.
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
int multiplier;
int addend;
} PartialData;
// The function uses the pre-bound data along with a value.
// The argument is an array: [PartialData*, int*]
void partial_multiplier(void *arg) {
void **params = (void **)arg;
PartialData *data = (PartialData *)params[0];
int *value = (int *)params[1];
*value = (*value) * data->multiplier + data->addend;
printf("Partial Multiplier: %d\n", *value);
}
int main(void) {
PartialData *pdata = malloc(sizeof(PartialData));
pdata->multiplier = 4;
pdata->addend = 5;
int *value = malloc(sizeof(int));
*value = 3;
void *args[2] = { pdata, value };
pcd funcs[] = { partial_multiplier };
scad sched;
sched.argument = args;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs; // one function in the workflow
sched.list = scheduleList;
_runc_0((void *)&sched);
free(pdata);
free(value);
return 0;
}
```
---
#### **Example 5 – Closure Example (Capturing an Environment)**
```c
// Example 5: Closure Example (Non-Optimized Version)
// This example simulates a closure by capturing a message and a repeat count in a structure.
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
const char *message;
int repeat;
} ClosureEnv;
// The function uses the closure's environment.
void print_message(void *arg) {
ClosureEnv *env = (ClosureEnv *)arg;
for (int i = 0; i < env->repeat; i++) {
printf("Closure says: %s\n", env->message);
}
}
int main(void) {
ClosureEnv *env = malloc(sizeof(ClosureEnv));
env->message = "Hello, Closure!";
env->repeat = 2;
pcd funcs[] = { print_message };
scad sched;
sched.argument = env;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs; // one function
sched.list = scheduleList;
_runc_0((void *)&sched);
free(env);
return 0;
}
```
---
#### **Example 6 – Closure with Partial Application**
```c
// Example 6: Closure with Partial Application (Non-Optimized Version)
// Here we simulate a closure in a Python-style partial by pre-binding a prefix.
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
int times;
} PartialClosure;
// The function expects an array: [PartialClosure*, const char*]
void partial_print(void *arg) {
void **params = (void **)arg;
PartialClosure *closure = (PartialClosure *)params[0];
const char *suffix = (const char *)params[1];
for (int i = 0; i < closure->times; i++) {
printf("%s %s\n", closure->prefix, suffix);
}
}
int main(void) {
PartialClosure *closure = malloc(sizeof(PartialClosure));
closure->prefix = "Partial Closure:";
closure->times = 3;
const char *suffix = "world!";
void *args[2] = { closure, (void *)suffix };
pcd funcs[] = { partial_print };
scad sched;
sched.argument = args;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs;
sched.list = scheduleList;
_runc_0((void *)&sched);
free(closure);
return 0;
}
```
---
### **Optimized (Experimental) Versions (using _experiemtial_runc_F)**
#### **Example 7 – Function Composition (2 functions) – Optimized**
```c
// Optimized Example 1: Function Composition (2 functions) - Experimental Runner Version
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
// Increment the integer.
void increment(void *arg) {
int *p = (int *)arg;
(*p)++;
printf("Optimized Increment: %d\n", *p);
}
// Double the integer.
void double_val(void *arg) {
int *p = (int *)arg;
*p *= 2;
printf("Optimized Double: %d\n", *p);
}
int main(void) {
pcd funcs[] = { increment, double_val };
scad sched;
int *value = malloc(sizeof(int));
*value = 5;
sched.argument = value;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs + 1;
sched.list = scheduleList;
// Execute using the optimized experimental runner.
_experiemtial_runc_F((void *)&sched);
free(value);
return 0;
}
```
---
#### **Example 8 – Function Composition (3 functions) – Optimized**
```c
// Optimized Example 2: Function Composition (3 functions) - Experimental Runner Version
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
// Adds 3 to the integer.
void add_three(void *arg) {
int *p = (int *)arg;
*p += 3;
printf("Optimized Add Three: %d\n", *p);
}
// Subtracts 2 from the integer.
void subtract_two(void *arg) {
int *p = (int *)arg;
*p -= 2;
printf("Optimized Subtract Two: %d\n", *p);
}
// Multiplies the integer by 4.
void multiply_four(void *arg) {
int *p = (int *)arg;
*p *= 4;
printf("Optimized Multiply Four: %d\n", *p);
}
int main(void) {
pcd funcs[] = { add_three, subtract_two, multiply_four };
scad sched;
int *value = malloc(sizeof(int));
*value = 10;
sched.argument = value;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs + 2;
sched.list = scheduleList;
_experiemtial_runc_F((void *)&sched);
free(value);
return 0;
}
```
---
#### **Example 9 – Functor Example – Optimized**
```c
// Optimized Example 3: Functor Example - Experimental Runner Version
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
int multiplier;
} Functor;
void multiply_functor(void *arg) {
void **arr = (void **)arg;
Functor *f = (Functor *)arr[0];
int *val = (int *)arr[1];
*val *= f->multiplier;
printf("Optimized Functor multiply: %d\n", *val);
}
int main(void) {
Functor *f = malloc(sizeof(Functor));
f->multiplier = 3;
int *value = malloc(sizeof(int));
*value = 7;
void *args[2] = { f, value };
pcd funcs[] = { multiply_functor };
scad sched;
sched.argument = args;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs;
sched.list = scheduleList;
_experiemtial_runc_F((void *)&sched);
free(f);
free(value);
return 0;
}
```
---
#### **Example 10 – Functor with Partial Application – Optimized**
```c
// Optimized Example 4: Functor with Partial Application - Experimental Runner Version
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
int multiplier;
int addend;
} PartialData;
void partial_multiplier(void *arg) {
void **params = (void **)arg;
PartialData *data = (PartialData *)params[0];
int *value = (int *)params[1];
*value = (*value) * data->multiplier + data->addend;
printf("Optimized Partial Multiplier: %d\n", *value);
}
int main(void) {
PartialData *pdata = malloc(sizeof(PartialData));
pdata->multiplier = 4;
pdata->addend = 5;
int *value = malloc(sizeof(int));
*value = 3;
void *args[2] = { pdata, value };
pcd funcs[] = { partial_multiplier };
scad sched;
sched.argument = args;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs;
sched.list = scheduleList;
_experiemtial_runc_F((void *)&sched);
free(pdata);
free(value);
return 0;
}
```
---
#### **Example 11 – Closure Example – Optimized**
```c
// Optimized Example 5: Closure Example - Experimental Runner Version
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
const char *message;
int repeat;
} ClosureEnv;
void print_message(void *arg) {
ClosureEnv *env = (ClosureEnv *)arg;
for (int i = 0; i < env->repeat; i++) {
printf("Optimized Closure says: %s\n", env->message);
}
}
int main(void) {
ClosureEnv *env = malloc(sizeof(ClosureEnv));
env->message = "Hello, Optimized Closure!";
env->repeat = 2;
pcd funcs[] = { print_message };
scad sched;
sched.argument = env;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs;
sched.list = scheduleList;
_experiemtial_runc_F((void *)&sched);
free(env);
return 0;
}
```
---
#### **Example 12 – Closure with Partial Application – Optimized**
```c
// Optimized Example 6: Closure with Partial Application - Experimental Runner Version
#include <stdio.h>
#include <stdlib.h>
#include "pcdf.h"
typedef struct {
const char *prefix;
int times;
} PartialClosure;
void partial_print(void *arg) {
void **params = (void **)arg;
PartialClosure *closure = (PartialClosure *)params[0];
const char *suffix = (const char *)params[1];
for (int i = 0; i < closure->times; i++) {
printf("Optimized %s %s\n", closure->prefix, suffix);
}
}
int main(void) {
PartialClosure *closure = malloc(sizeof(PartialClosure));
closure->prefix = "Optimized Partial Closure:";
closure->times = 3;
const char *suffix = "world!";
void *args[2] = { closure, (void *)suffix };
pcd funcs[] = { partial_print };
scad sched;
sched.argument = args;
__scadule__ scheduleList;
scheduleList.workflow = funcs;
scheduleList.ends = funcs;
sched.list = scheduleList;
_experiemtial_runc_F((void *)&sched);
free(closure);
return 0;
}
```
---
Each of these examples demonstrates how you can build a “scheduler” that composes function calls (or functors/closures) using the interfaces defined in **pcdf.h**. You can modify the functions or the scheduling arrays to experiment further with composition and partial application in C.