implement function caching (for newton)