Safe Haskell | Safe-Infered |
---|
Memoize IO actions,
performing them at most once,
but recalling their result for subsequent invocations.
This library provides three sequencing strategies:
lazy (ioMemo
), eager (ioMemo'
), and concurrent (ioMemoPar
).
The lazy and eager approaches give stronger sequencing guarantees.
The following property holds: join . ioMemo === id
.
The same is true for ioMemo'
and ioMemoPar
.
Also, for the three memoizers in this library, the memory allocated for the result will not be available for garbage collection until the corresponding memoized action is also available for garbage collection, unless your compiler performs deep magicks.
Documentation
ioMemo :: 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
.
This function is also thread-safe:
it is guaranteed that the action passed in
will be performed exactly 0 or 1 times
by this code. Exceptions will be propagated
to the caller.
Example usage:
>>>
getLine' <- ioMemo getLine
>>>
replicateM 3 getLine'
Hello ["Hello", "Hello", "Hello"]