Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
72
Task/First-class-functions/C/first-class-functions-1.c
Normal file
72
Task/First-class-functions/C/first-class-functions-1.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
/* declare a typedef for a function pointer */
|
||||
typedef double (*Class2Func)(double);
|
||||
|
||||
/*A couple of functions with the above prototype */
|
||||
double functionA( double v)
|
||||
{
|
||||
return v*v*v;
|
||||
}
|
||||
double functionB(double v)
|
||||
{
|
||||
return exp(log(v)/3);
|
||||
}
|
||||
|
||||
/* A function taking a function as an argument */
|
||||
double Function1( Class2Func f2, double val )
|
||||
{
|
||||
return f2(val);
|
||||
}
|
||||
|
||||
/*A function returning a function */
|
||||
Class2Func WhichFunc( int idx)
|
||||
{
|
||||
return (idx < 4) ? &functionA : &functionB;
|
||||
}
|
||||
|
||||
/* A list of functions */
|
||||
Class2Func funcListA[] = {&functionA, &sin, &cos, &tan };
|
||||
Class2Func funcListB[] = {&functionB, &asin, &acos, &atan };
|
||||
|
||||
/* Composing Functions */
|
||||
double InvokeComposed( Class2Func f1, Class2Func f2, double val )
|
||||
{
|
||||
return f1(f2(val));
|
||||
}
|
||||
|
||||
typedef struct sComposition {
|
||||
Class2Func f1;
|
||||
Class2Func f2;
|
||||
} *Composition;
|
||||
|
||||
Composition Compose( Class2Func f1, Class2Func f2)
|
||||
{
|
||||
Composition comp = malloc(sizeof(struct sComposition));
|
||||
comp->f1 = f1;
|
||||
comp->f2 = f2;
|
||||
return comp;
|
||||
}
|
||||
|
||||
double CallComposed( Composition comp, double val )
|
||||
{
|
||||
return comp->f1( comp->f2(val) );
|
||||
}
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ix;
|
||||
Composition c;
|
||||
|
||||
printf("Function1(functionA, 3.0) = %f\n", Function1(WhichFunc(0), 3.0));
|
||||
|
||||
for (ix=0; ix<4; ix++) {
|
||||
c = Compose(funcListA[ix], funcListB[ix]);
|
||||
printf("Compostion %d(0.9) = %f\n", ix, CallComposed(c, 0.9));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
56
Task/First-class-functions/C/first-class-functions-2.c
Normal file
56
Task/First-class-functions/C/first-class-functions-2.c
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
typedef double (*f_dbl)(double);
|
||||
#define TAGF (f_dbl)0xdeadbeef
|
||||
#define TAGG (f_dbl)0xbaddecaf
|
||||
|
||||
double dummy(double x)
|
||||
{
|
||||
f_dbl f = TAGF;
|
||||
f_dbl g = TAGG;
|
||||
return f(g(x));
|
||||
}
|
||||
|
||||
f_dbl composite(f_dbl f, f_dbl g)
|
||||
{
|
||||
size_t len = (void*)composite - (void*)dummy;
|
||||
f_dbl ret = malloc(len);
|
||||
char *ptr;
|
||||
memcpy(ret, dummy, len);
|
||||
for (ptr = (char*)ret; ptr < (char*)ret + len - sizeof(f_dbl); ptr++) {
|
||||
if (*(f_dbl*)ptr == TAGF) *(f_dbl*)ptr = f;
|
||||
else if (*(f_dbl*)ptr == TAGG) *(f_dbl*)ptr = g;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
double cube(double x)
|
||||
{
|
||||
return x * x * x;
|
||||
}
|
||||
|
||||
/* uncomment next line if your math.h doesn't have cbrt() */
|
||||
/* double cbrt(double x) { return pow(x, 1/3.); } */
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
double x;
|
||||
|
||||
f_dbl A[3] = { cube, exp, sin };
|
||||
f_dbl B[3] = { cbrt, log, asin}; /* not sure about availablity of cbrt() */
|
||||
f_dbl C[3];
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
C[i] = composite(A[i], B[i]);
|
||||
|
||||
for (i = 0; i < 3; i++) {
|
||||
for (x = .2; x <= 1; x += .2)
|
||||
printf("C%d(%g) = %g\n", i, x, C[i](x));
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
17
Task/First-class-functions/C/first-class-functions-3.c
Normal file
17
Task/First-class-functions/C/first-class-functions-3.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
C0(0.2) = 0.2
|
||||
C0(0.4) = 0.4
|
||||
C0(0.6) = 0.6
|
||||
C0(0.8) = 0.8
|
||||
C0(1) = 1
|
||||
|
||||
C1(0.2) = 0.2
|
||||
C1(0.4) = 0.4
|
||||
C1(0.6) = 0.6
|
||||
C1(0.8) = 0.8
|
||||
C1(1) = 1
|
||||
|
||||
C2(0.2) = 0.2
|
||||
C2(0.4) = 0.4
|
||||
C2(0.6) = 0.6
|
||||
C2(0.8) = 0.8
|
||||
C2(1) = 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue