Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fun/fn.ts>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 memoize
import { memoize } from "https://deno.land/x/fun@v.2.0.0-alpha.11/fn.ts";

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