Module

Data.Function.Memoize

This module defines functions for memoizing functions, i.e. creating functions which remember their results.

This module works by turning a function into a lazily-evaluated data structure depending on its domain type.

#Tabulate

class Tabulate a  where

The Tabulate class identifies those types which can be used as the domain of a memoized function, i.e. those for which the results can be tabulated.

Members

Instances

#memoize

memoize :: forall a b. Tabulate a => (a -> b) -> a -> b

Memoize a function of one argument

#memoize2

memoize2 :: forall a b c. Tabulate a => Tabulate b => (a -> b -> c) -> a -> b -> c

Memoize a function of two arguments

#memoize3

memoize3 :: forall a b c d. Tabulate a => Tabulate b => Tabulate c => (a -> b -> c -> d) -> a -> b -> c -> d

Memoize a function of three arguments

#genericTabulate

genericTabulate :: forall a r rep. Generic a rep => Tabulate rep => (a -> r) -> a -> Lazy r

A default implementation of Tabulate for Generic types.

Given a data type made up of data types with Tabulate instances:

data MyDataType
  = A Int
  | B String

First, derive an instance of Data.Generics.Rep.Generic:

derive instance genericMyDataType :: Generic MyDataType _

Now, Tabulate can be defined in terms of genericTabulate:

instance tabulateMyDataType :: Tabulate MyDataType where
  tabulate = genericTabulate

Note: this function should not be used to derive instances for recursive data types, and attempting to do so will lead to stack overflow errors at runtime.

Modules