Created
June 14, 2011 19:21
-
-
Save cwoodall/1025640 to your computer and use it in GitHub Desktop.
Just playing around with Objective-C Blocks... I like them and all things functional...
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
// | |
// blockTest.m | |
// blocksTest | |
// | |
// Created by Christopher Woodall on 6/14/11. | |
// Copyright 2011 Christopher Woodall. | |
// MIT License | |
// | |
#import <Foundation/Foundation.h> | |
#import <Block.h> | |
#import <stdio.h> | |
typedef float(^Equation)(float[]); // To clean things up a little | |
// Impractical, but sets up a function which will process a list of values through an function. | |
// The function will process the list as if it were a series of inputs into some equation. | |
float equation (float a[], Equation func) | |
{ | |
return func(a); | |
} | |
int main (int argc, const char * argv[]) | |
{ | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
float res; | |
float list[3] = {9, 12.2, 5.7}; | |
// Define the function through which our list will be subjected to | |
Equation sum = ^ float (float a[]){ | |
float store = 0; // initial value of zero. | |
// BAD: should set to the first value in the array a[] | |
// Go through the array until the end is reached and sum all the numbers together | |
for (int i = 0; i <= sizeof(a)/sizeof(float); i++) | |
store += a[i]; | |
return store; | |
}; | |
res = equation(list, sum); // Result is: 26.900 | |
NSLog(@"Val of a + b is %.3f", res); | |
[pool drain]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment