Added optional each function to memoization