once

memoization for IO actions and functions

https://gitlab.com/kaction/haskell-once

Version on this page:0.3
LTS Haskell 22.17:0.4
Stackage Nightly 2024-04-18:0.4
Latest on Hackage:0.4

See all snapshots once appears in

GPL-3.0-only licensed by Dmitry Bogatov
Maintained by [email protected]
This version can be pinned in stack with:once-0.3@sha256:5fff6e0e1046eb12b780c1bdd06245d720c313adeff39e9bc8f48b57b0443389,2019

Module documentation for 0.3

Package provides single polymorphic function once, that allows you to memoize IO actions and functions, evaluating them at most once.

let mkStamp = (putStrLn "stamping" >> writeFile "/tmp/stamp" "") :: IO ()
-- onceStamp :: IO ()
onceStamp <- once mkStamp
-- onceStamp actually evaluates mkStamp it wraps first time.
onceStamp
stamping
-- but second time result `()' is memoized, no action is performed.
onceStamp
-- we can memoize functions too
foo <- once $ \x -> print "foo" >> print (x :: Int)
-- action will be performed once for every distinct argument
foo 10
foo
10
foo 10
10
foo 4
foo
4