memo-map: Memoization in a Map

[ library, memoization, mit ] [ Propose Tags ] [ Report a vulnerability ]

Utility for memoizing an effectful function (e.g. database query) using a Map.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0.0
Change log CHANGELOG.md
Dependencies base (>=4.16.4.0 && <5), containers (>=0.6.5.1), unliftio (>=0.2.25.0) [details]
License MIT
Author
Maintainer Freckle Education
Category Memoization
Home page https://github.com/freckle/memo-map#readme
Bug tracker https://github.com/freckle/memo-map/issues
Source repo head: git clone https://github.com/freckle/memo-map
Uploaded by PatrickBrisbin at 2025-03-26T20:51:28Z
Distributions
Downloads 4 total (4 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2025-03-26 [all 1 reports]

Readme for memo-map-0.0.0.0

[back to package description]

memo-map

Utility for memoizing an effectful function (e.g. database query) using a map.

Example

import MemoMap

For demonstration, a silly little function that performs an effect and returns a value:

f :: Int -> IO Int
f x = do
  putStrLn ("f " <> show x)
  pure (x * 10)

If f may be applied to the same argument repeatedly, perhaps it would be advantageous to save the results from a previous application rather than repeating the action.

Create a MemoMap

Turn f into a MemoMap.

  mm <- newMemoMap f

Use the memoized function

Use runMemoMap mm instead of f to run a memoized version of f.

  print =<< runMemoMap mm 2
  print =<< runMemoMap mm 2
  print =<< runMemoMap mm 3

Output:

f 2
20
20
f 3
30

Inspect the cache

You can also use getMemoMap to inspect the stored values.

  print =<< getMemoMap mm

Output:

fromList [(2,20),(3,30)]

CHANGELOG | LICENSE