Created
April 3, 2016 16:20
-
-
Save sekrasoft/9a66b67c856b9104df01dfa0580e96fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef double number; | |
typedef number* numbers; | |
typedef size_t index; | |
typedef number (*function)(number); | |
numbers map(function f, numbers xs, size_t size) { | |
numbers ys = malloc(size * sizeof(number)); | |
if(!ys) return 0; | |
for(index i=0; i<size; ++i) ys[i] = f(xs[i]); | |
return ys; | |
} | |
number sqr(number x) { | |
return x*x; | |
} | |
int main(void) { | |
number nats[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; | |
numbers squares = map(sqr, nats, 10); | |
if(squares) { | |
printf("squares: "); | |
for(int i=0; i<10; ++i) | |
printf("%.0f ", squares[i]); | |
free(squares); | |
} else { | |
printf("Sorry! No memory."); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Вывод программы: