type MemoizedFunction<T> = (...args: any[]) => T;

export function memoize<T>(func: (...args: any[]) => T): MemoizedFunction<T> {
  const cache = new Map<string, T>();
  
  return (...args: any[]): T => {
    const key = JSON.stringify(args);

    if (cache.has(key)) {
      return cache.get(key)!;
    }
    
    const result = func(...args);
    cache.set(key, result);
    
    return result;
  };
}
