Revised code to add the actual required functions.
1.0 was experimental based on example code.
1.1 I changed the variable names from 'mean' to 'cache' and added comments. This code is not so easy to follow because the first function creates a list of functions used by the second function.