Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module expors single polymorphic function once
, that allows you
to memoize IO actions and functions, evaluating them at most once.
Here is example:
>>>
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
Documentation
once :: Once a => a -> IO a Source #
memoize IO action or function returning IO to be peformed only once.
Any IO
action is suitable, as is any function with Hashable
arguments, returning value in IO monad. Value of any of type below
are okay to pass to once
:
IO Int Int -> IO () Int -> Double -> IO (Char -> Int)
Due implementation limitations, only up to 7 arguments are supported.