Skip to main content
Module

x/fun/mod.ts>fn.memoize

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Go to Latest
function fn.memoize
import { fn } from "https://deno.land/x/fun@v2.0.0-alpha.12/mod.ts";
const { memoize } = fn;

Memoize a unary function using a Map. This means that this algorithm puposefully leaks memory.

TODO: Extend memoize to be variadic.

Examples

Example 1

import * as F from "./fn.ts";

// Big old expensive recursive algorithm
const fib = (n: number): number =>
  n < 1 ? 0 :
  n <= 1 ? 1 :
  fib(n - 2) + fib(n - 1);

const mfib = F.memoize(fib);

const result1 = mfib(10); // 55
const result2 = mfib(10); // 55 but does not recompute