friedbrice / elm-teaching-tools / ElmTeachingTools.Lib.Memoize

Optimize program performance by converting a heavily-used function into a data structure.


type alias MemoizeStrategy c x y =
{ toKey : x -> c
, fromKey : c -> x
, domain : List x
, default : y 
}

A strategy for turning a function into data structure.

Note: You should define any MemoizeStrategys at the top-level of a file so that you're guaranteed to neven recompute the domain.

memoize : MemoizeStrategy comparable x y -> (x -> y) -> x -> y

Converts a function into a data structure, and outputs a new function that uses that data structure instead of the old function. Sometimes useful for enhancing program performance.

Note: The new function will agree with the old function only on the domain provided in the MemoizeStrategy. Outside of that domain, the new function will return the default provided in the MemoizeStrategy.