4 ms·
Reminiscent of Haskell's bracket: bracket :: IO a -- computation to run first ("acquire resource") -> (a -> IO b) -- computation t
by ky3 11y ago
Reminiscent of Haskell's bracket:
bracket :: IO a -- computation to run first ("acquire resource")
-> (a -> IO b) -- computation to run last ("release resource")
-> (a -> IO c) -- computation to run in-between
-> IO c
And because of the order of parameters, one may write e.g.
bracketReadFile :: FilePath -> (Handle -> IO c) -> IO c
bracketReadFile fname = bracket (openFile fname ReadMode) hClose
to specialize bracket to specific domains.
Everything becomes neat, tidy, orthogonal, compositional.
The function bracketReadFile is itself a specialization of the library function:
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
which also builds on top of bracket in the same way. See https://hackage.haskell.org/package/base-4.8.1.0/docs/src/System-IO.html#withFile https://hackage.haskell.org/package/base-4.8.1.0/docs/src/Sy...
(Yes, I'm perfectly aware of the wrinkles that Lazy I/O causes. Assume the above are the strict equivalents.)