Last active
December 27, 2015 00:19
-
-
Save binford2k/7237047 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// This is a quick example to show how something like ruby blocks might be implemented in another language | |
// psuedo code follows, don't get hung up on syntax | |
class Array { | |
std::vector<int> array; | |
Array (std::vector in) { | |
self.array = in; | |
} | |
// the array itself has a .each member function that accepts a block (anonymous function) as an argument | |
void each(&block) { | |
// .each will iterate over itself in a totally old school procedural fashion | |
for(unsigned int i=0; i < self.array.size(); i++) { | |
// for each element of the array, it just calls the block (anonymous function) | |
// you gave .each with that element as an argument | |
block(self.array[i]); | |
} | |
} | |
} | |
// this function is the "block" that we'll pass in to the .each method | |
void printstuff (string arg) { | |
printf("%s\n", arg); | |
} | |
foo = Array.new(['foo','bar','baz']); | |
// ruby's language syntax makes it easy to declare the block anonymously, right inline. | |
// here, we pass in a function pointer instead to show what's going on behind the scenes | |
foo.each(&printstuff); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment