Skip to content

Instantly share code, notes, and snippets.

@romyilano
Created February 8, 2013 19:42
Show Gist options
  • Select an option

  • Save romyilano/4741429 to your computer and use it in GitHub Desktop.

Select an option

Save romyilano/4741429 to your computer and use it in GitHub Desktop.
A beautiful example of blocks in Big Nerd Ranch. I like how they describe blocks as a hybridization of method handling in procedural and object-oriented programming languages. Methods in C and procedural langauges can be accessed anywhere but methods in object-oriented progrmaming langauges can only be accessed by classes and instances of classe…
// block variables - this one isn't defined
// block variable named adder that takes in an int parameter and an int b parameter
int (^adder)(int a, int b);
// defining block literals - here's the syntax
^int (int x, int y) {
return x + y;
};
int (^adder) (int, int);
// block variable named foo
// it can point to a block that takes no arguments and returns no values
void (^foo)(void);
// block variable named jump. it can point to a block
// that takes one Hurlde * and returns no value
void (^jump)(Hurdle *);
// a block variable named doubler; it can point to a block that takes one int
// and returns an int
int (^doubler)(int);
// defining block literals - the definition of a block is called a block literal
// caret, return type and then parenthesis containing arguments (like C)
^int (int x, int y)
{
return x + y;
};
int (^adder)(int, int) = ^int(int x, int y) {
return x + y;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment