Safe Haskell | Safe-Inferred |
---|
Memoize IO actions, performing them at most once,
but recalling their result for subsequent invocations.
This library provides two sequencing strategies:
lazy (once
), and concurrent (eagerlyOnce
).
The following property holds: join . once === id
.
The same is true for eagerlyOnce
.
The memory allocated for a memoized result will obviously not be available for garbage collection until the corresponding memoized action is also available for garbage collection.
Documentation
once :: IO a -> IO (IO a)Source
Memoize an IO action. The action will be performed the first time that it its value is demanded; all subsequent invocations will simply recall the value acquired from the first call. If the value is never demanded, then the action will never be performed.
This is basically a safe version of unsafeInterleaveIO
.
Exceptions will be propagated to the caller, and the action will be retried
at each invocation, only until it has successfully completed once.
Example usage:
>>>
getLine' <- once getLine
>>>
replicateM 3 getLine'
Hello ["Hello", "Hello", "Hello"]
eagerlyOnce :: IO a -> IO (IO a)Source
Memoize an IO action. The action will be started immediately in a new thread. Attempts to access the result will block until the action is finished. If the action produces an error, then attempts to access its value will re-raise the same error each time.