try_memoize helper funtion