Created
March 22, 2012 01:35
-
-
Save dacap/2155047 to your computer and use it in GitHub Desktop.
More closure magic in pseudo-code
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
// Holly crap! Now we can give the "sum_and_count" function as a parameter. | |
FUNCTION Calculate_average ( sum_and_count, N1, N2, ..., NN ) | |
LOCAL VARIABLE sum = 0 | |
LOCAL VARIABLE count = 0 | |
FOR_EACH N = N1, N2, ..., NN | |
CALL_THE_FOLLOWING_FUNCTION sum_and_count ( N ) | |
RETURN num / count | |
END FUNCTION | |
// -------------------------------------------- | |
// So the user can define its own sum_and_count | |
// function and pass it to Calculate_average | |
FUNCTION My_sum_and_count(x) | |
sum = sum + x // Oh, here we are referencing unbound variables, | |
count = count + 1 // but this is not a problem yet (inside Calculate_average they | |
// will be bound) | |
END LOCAL FUNCTION | |
// Here we pass "My_sum_and_count" function as a parameter to "Calculate_average" | |
// function. The interpreter will try to bound all unbound-variables when | |
// "Calculate_average" uses the "sum_and_count" function. | |
CALL_THE_FOLLOWING_FUNCTION Calculate_average ( My_sum_and_count, 8, 4, 6, 2, 7 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment