HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleconduit-1.3.6.1Haskell2010

Conduit

Your intended one-stop-shop for conduit functionality. This re-exports functions from many commonly used modules. When there is a conflict with standard functions, functions in this module are disambiguated by adding a trailing C (or for chunked functions, replacing a trailing E with CE). This means that the Conduit module can be imported unqualified without causing naming conflicts.

For more information on the naming scheme and intended usages of the combinators, please see the Data.Conduit.Combinators documentation.

  • 5 types
  • 6 classes
  • 151 values
  • Packageconduit-1.3.6.1
  • Exports163
  • LanguageHaskell2010
  • LicenceMIT
  • SourceConduit.hs

Core conduit library

0 declarations

Commonly used combinators

147 declarations
valueyieldMany
  1. :: (Monad m, MonoFoldable mono)
  2. => mono
  3. -> ConduitT i (Element mono) m ()
#

Yield each of the values contained by the given MonoFoldable.

This will work on many data structures, including lists, ByteStrings, and Vectors.

Subject to fusion

valuesourceLazy
  1. :: (Monad m, LazySequence lazy strict)
  2. => lazy
  3. -> ConduitT i strict m ()
#

Generate a producer by yielding each of the strict chunks in a LazySequence.

For more information, see toChunks.

Subject to fusion

valuesourceHandle :: MonadIO m => Handle -> ConduitT i ByteString m ()
#

Stream the contents of a Handle as binary data. Note that this function will not automatically close the Handle when processing completes, since it did not acquire the Handle in the first place.

Same as sourceHandle, but instead of allocating a new buffer for each incoming chunk of data, reuses the same buffer. Therefore, the ByteStrings yielded by this function are not referentially transparent between two different yields.

This function will be slightly more efficient than sourceHandle by avoiding allocations and reducing garbage collections, but should only be used if you can guarantee that you do not reuse a ByteString (or any slice thereof) between two calls to await.

Stream the contents of the given directory, without traversing deeply.

This function will return all of the contents of the directory, whether they be files, directories, etc.

Note that the generated filepaths will be the complete path, not just the filename. In other words, if you have a directory foo containing files bar and baz, and you use sourceDirectory on foo, the results will be foo/bar and foo/baz.

valuesourceDirectoryDeep
  1. :: MonadResource m
  2. => Bool

    Follow directory symlinks

  3. -> FilePath

    Root directory

  4. -> ConduitT i FilePath m ()
#

Deeply stream the contents of the given directory.

This works the same as sourceDirectory, but will not return directories at all. This function also takes an extra parameter to indicate whether symlinks will be followed.

valuesinkLazy :: (Monad m, LazySequence lazy strict) => ConduitT strict o m lazy
#

Consume all incoming strict chunks into a lazy sequence. Note that the entirety of the sequence will be resident at memory.

This can be used to consume a stream of strict ByteStrings into a lazy ByteString, for example.

Subject to fusion

valuesinkList :: Monad m => ConduitT a o m [a]
#

Consume all values from the stream and return as a list. Note that this will pull all values into memory.

Subject to fusion

valuesinkVector :: (Vector v a, PrimMonad m) => ConduitT a o m (v a)
#

Sink incoming values into a vector, growing the vector as necessary to fit more elements.

Note that using this function is more memory efficient than sinkList and then converting to a Vector, as it avoids intermediate list constructors.

Subject to fusion

valuesinkVectorN
  1. :: (Vector v a, PrimMonad m)
  2. => Int

    maximum allowed size

  3. -> ConduitT a o m (v a)
#

Sink incoming values into a vector, up until size maxSize. Subsequent values will be left in the stream. If there are less than maxSize values present, returns a Vector of smaller size.

Note that using this function is more memory efficient than sinkList and then converting to a Vector, as it avoids intermediate list constructors.

Subject to fusion

Same as sinkBuilder, but afterwards convert the builder to its lazy representation.

Alternatively, this could be considered an alternative to sinkLazy, with the following differences:

  • This function will allow multiple input types, not just the strict version of the lazy structure.

  • Some buffer copying may occur in this version.

Subject to fusion

valuesinkNull :: Monad m => ConduitT a o m ()
#

Consume and discard all remaining values in the stream.

Subject to fusion

Cautious version of sinkFile. The idea here is to stream the values to a temporary file in the same directory of the destination file, and only on successfully writing the entire file, moves it atomically to the destination path.

In the event of an exception occurring, the temporary file will be deleted and no move will be made. If the application shuts down without running exception handling (such as machine failure or a SIGKILL), the temporary file will remain and the destination file will be untouched.

valuesinkTempFile
  1. :: MonadResource m
  2. => FilePath

    temp directory

  3. -> String

    filename pattern

  4. -> ConduitM ByteString o m FilePath
#

Stream data into a temporary file in the given directory with the given filename pattern, and return the temporary filename. The temporary file will be automatically deleted when exiting the active ResourceT block, if it still exists.

valuesinkHandleBuilder :: MonadIO m => Handle -> ConduitM Builder o m ()
#

Stream incoming builders, executing them directly on the buffer of the given Handle. Note that this function does not automatically close the Handle when processing completes. Pass flush to flush the buffer.

valueconduitVector
  1. :: (Vector v a, PrimMonad m)
  2. => Int

    maximum allowed size

  3. -> ConduitT a (v a) m ()
#

Break up a stream of values into vectors of size n. The final vector may be smaller than n if the total number of values is not a strict multiple of n. No empty vectors will be yielded.

Incrementally execute builders on the given buffer and pass on the filled chunks as bytestrings. Note that, if the given buffer is too small for the execution of a build step, a larger one will be allocated.

WARNING: This conduit yields bytestrings that are NOT referentially transparent. Their content will be overwritten as soon as control is returned from the inner sink!

typetype BufferAllocStrategy = (IO Buffer, Int -> Buffer -> IO (IO Buffer))
#

A buffer allocation strategy (buf0, nextBuf) specifies the initial buffer to use and how to compute a new buffer nextBuf minSize buf with at least size minSize from a filled buffer buf. The double nesting of the IO monad helps to ensure that the reference to the filled buffer buf is lost as soon as possible, but the new buffer doesn't have to be allocated too early.

The simplest buffer allocation strategy: whenever a buffer is requested, allocate a new one that is big enough for the next build step to execute.

NOTE that this allocation strategy may spill quite some memory upon direct insertion of a bytestring by the builder. Thats no problem for garbage collection, but it may lead to unreasonably high memory consumption in special circumstances.

An unsafe, but possibly more efficient buffer allocation strategy: reuse the buffer, if it is big enough for the next build step to execute.

valuemapAccumS
  1. :: Monad m
  2. => a -> s -> ConduitT b Void m s
  3. -> s
  4. -> ConduitT () b m ()
  5. -> ConduitT a Void m s
#

Consume a source with a strict accumulator, in a way piecewise defined by a controlling stream. The latter will be evaluated until it terminates.

Example2 expressions
let f a s = liftM (:s) $ mapC (*a) =$ CL.take areverse $ runIdentity $ yieldMany [0..3] $$ mapAccumS f [] (yieldMany [1..])[[],[1],[4,6],[12,15,18]] :: [[Int]]
valuepeekForever :: Monad m => ConduitT i o m () -> ConduitT i o m ()
#

Run a consuming conduit repeatedly, only stopping when there is no more data available from upstream.

valuepeekForeverE
  1. :: (Monad m, MonoFoldable i)
  2. => ConduitT i o m ()
  3. -> ConduitT i o m ()
#

Run a consuming conduit repeatedly, only stopping when there is no more data available from upstream.

In contrast to peekForever, this function will ignore empty chunks of data. So for example, if a stream of data contains an empty ByteString, it is still treated as empty, and the consuming function is not called.

valueenumFromToC :: (Monad m, Enum a, Ord a) => a -> a -> ConduitT i a m ()
#

Enumerate from a value to a final value, inclusive, via succ.

This is generally more efficient than using Prelude's enumFromTo and combining with sourceList since this avoids any intermediate data structures.

valueiterateC :: Monad m => (a -> a) -> a -> ConduitT i a m ()
#

Produces an infinite stream of repeated applications of f to x.

valuerepeatC :: Monad m => a -> ConduitT i a m ()
#

Produce an infinite stream consisting entirely of the given value.

valuerepeatMC :: Monad m => m a -> ConduitT i a m ()
#

Repeatedly run the given action and yield all values it produces.

valuerepeatWhileMC :: Monad m => m a -> (a -> Bool) -> ConduitT i a m ()
#

Repeatedly run the given action and yield all values it produces, until the provided predicate returns False.

valuedropC :: Monad m => Int -> ConduitT a o m ()
#

Ignore a certain number of values in the stream.

Note: since this function doesn't produce anything, you probably want to use it with (>>) instead of directly plugging it into a pipeline:

Example2 expressions
runConduit $ yieldMany [1..5] .| dropC 2 .| sinkList[]runConduit $ yieldMany [1..5] .| (dropC 2 >> sinkList)[3,4,5]
valuedropCE :: (Monad m, IsSequence seq) => Index seq -> ConduitT seq o m ()
#

Drop a certain number of elements from a chunked stream.

Note: you likely want to use it with monadic composition. See the docs for dropC.

valuedropWhileC :: Monad m => (a -> Bool) -> ConduitT a o m ()
#

Drop all values which match the given predicate.

Note: you likely want to use it with monadic composition. See the docs for dropC.

valuefoldMapC :: (Monad m, Monoid b) => (a -> b) -> ConduitT a o m b
#

Apply the provided mapping function and monoidal combine all values.

valueallC :: Monad m => (a -> Bool) -> ConduitT a o m Bool
#

Check that all values in the stream return True.

Subject to shortcut logic: at the first False, consumption of the stream will stop.

valueallCE
  1. :: (Monad m, MonoFoldable mono)
  2. => Element mono -> Bool
  3. -> ConduitT mono o m Bool
#

Check that all elements in the chunked stream return True.

Subject to shortcut logic: at the first False, consumption of the stream will stop.

valueanyC :: Monad m => (a -> Bool) -> ConduitT a o m Bool
#

Check that at least one value in the stream returns True.

Subject to shortcut logic: at the first True, consumption of the stream will stop.

valueanyCE
  1. :: (Monad m, MonoFoldable mono)
  2. => Element mono -> Bool
  3. -> ConduitT mono o m Bool
#

Check that at least one element in the chunked stream returns True.

Subject to shortcut logic: at the first True, consumption of the stream will stop.

valueandC :: Monad m => ConduitT Bool o m Bool
#

Are all values in the stream True?

Consumption stops once the first False is encountered.

valueorC :: Monad m => ConduitT Bool o m Bool
#

Are any values in the stream True?

Consumption stops once the first True is encountered.

valueelemC :: (Monad m, Eq a) => a -> ConduitT a o m Bool
#

Are any values in the stream equal to the given value?

Stops consuming as soon as a match is found.

valuenotElemC :: (Monad m, Eq a) => a -> ConduitT a o m Bool
#

Are no values in the stream equal to the given value?

Stops consuming as soon as a match is found.

valueheadDefC :: Monad m => a -> ConduitT a o m a
#

Same as headC, but returns a default value if none are available from the stream.

valuelastDefC :: Monad m => a -> ConduitT a o m a
#

Same as lastC, but returns a default value if none are available from the stream.

valuenullC :: Monad m => ConduitT a o m Bool
#

True if there are no values in the stream.

This function does not modify the stream.

valuenullCE :: (Monad m, MonoFoldable mono) => ConduitT mono o m Bool
#

True if there are no elements in the chunked stream.

This function may remove empty leading chunks from the stream, but otherwise will not modify it.

valuemapM_C :: Monad m => (a -> m ()) -> ConduitT a o m ()
#

Apply the action to all values in the stream.

Note: if you want to pass the values instead of consuming them, use iterM instead.

valuemapM_CE
  1. :: (Monad m, MonoFoldable mono)
  2. => Element mono -> m ()
  3. -> ConduitT mono o m ()
#

Apply the action to all elements in the chunked stream.

Note: the same caveat as with mapM_C applies. If you don't want to consume the values, you can use iterM:

iterM (omapM_ f)
valuefoldMapMC :: (Monad m, Monoid w) => (a -> m w) -> ConduitT a o m w
#

Apply the provided monadic mapping function and monoidal combine all values.

valuemapC :: Monad m => (a -> b) -> ConduitT a b m ()
#

Apply a transformation to all values in a stream.

valuemapCE :: (Monad m, Functor f) => (a -> b) -> ConduitT (f a) (f b) m ()
#

Apply a transformation to all elements in a chunked stream.

valueomapCE
  1. :: (Monad m, MonoFunctor mono)
  2. => Element mono -> Element mono
  3. -> ConduitT mono mono m ()
#

Apply a monomorphic transformation to all elements in a chunked stream.

Unlike mapE, this will work on types like ByteString and Text which are MonoFunctor but not Functor.

valueconcatMapC
  1. :: (Monad m, MonoFoldable mono)
  2. => a -> mono
  3. -> ConduitT a (Element mono) m ()
#

Apply the function to each value in the stream, resulting in a foldable value (e.g., a list). Then yield each of the individual values in that foldable value separately.

Generalizes concatMap, mapMaybe, and mapFoldable.

valueconcatMapCE
  1. :: (Monad m, MonoFoldable mono, Monoid w)
  2. => Element mono -> w
  3. -> ConduitT mono w m ()
#

Apply the function to each element in the chunked stream, resulting in a foldable value (e.g., a list). Then yield each of the individual values in that foldable value separately.

Generalizes concatMap, mapMaybe, and mapFoldable.

valuetakeC :: Monad m => Int -> ConduitT a a m ()
#

Stream up to n number of values downstream.

Note that, if downstream terminates early, not all values will be consumed. If you want to force exactly the given number of values to be consumed, see takeExactly.

valuetakeCE :: (Monad m, IsSequence seq) => Index seq -> ConduitT seq seq m ()
#

Stream up to n number of elements downstream in a chunked stream.

Note that, if downstream terminates early, not all values will be consumed. If you want to force exactly the given number of values to be consumed, see takeExactlyE.

valuetakeWhileC :: Monad m => (a -> Bool) -> ConduitT a a m ()
#

Stream all values downstream that match the given predicate.

Same caveats regarding downstream termination apply as with take.

valuetakeWhileCE
  1. :: (Monad m, IsSequence seq)
  2. => Element seq -> Bool
  3. -> ConduitT seq seq m ()
#

Stream all elements downstream that match the given predicate in a chunked stream.

Same caveats regarding downstream termination apply as with takeE.

valuetakeExactlyC :: Monad m => Int -> ConduitT a b m r -> ConduitT a b m r
#

Consume precisely the given number of values and feed them downstream.

This function is in contrast to take, which will only consume up to the given number of values, and will terminate early if downstream terminates early. This function will discard any additional values in the stream if they are unconsumed.

Note that this function takes a downstream ConduitT as a parameter, as opposed to working with normal fusion. For more information, see http://www.yesodweb.com/blog/2013/10/core-flaw-pipes-conduit, the section titled "pipes and conduit: isolate".

valueconcatC :: (Monad m, MonoFoldable mono) => ConduitT mono (Element mono) m ()
#

Flatten out a stream by yielding the values contained in an incoming MonoFoldable as individually yielded values.

valuescanlC :: Monad m => (a -> b -> a) -> a -> ConduitT b a m ()
#

Analog of Prelude.scanl for lists.

valuemapAccumWhileC
  1. :: Monad m
  2. => a -> s -> Either s (s, b)
  3. -> s
  4. -> ConduitT a b m s
#

mapWhileC with a break condition dependent on a strict accumulator. Equivalently, CL.mapAccum as long as the result is Right. Instead of producing a leftover, the breaking input determines the resulting accumulator via Left.

valuechunksOfCE
  1. :: (Monad m, IsSequence seq)
  2. => Index seq
  3. -> ConduitT seq seq m ()
#

Split input into chunk of size chunkSize

The last element may be smaller than the chunkSize (see also chunksOfExactlyE which will not yield this last element)

valuechunksOfExactlyCE
  1. :: (Monad m, IsSequence seq)
  2. => Index seq
  3. -> ConduitT seq seq m ()
#

Split input into chunk of size chunkSize

If the input does not split into chunks exactly, the remainder will be leftover (see also chunksOfE)

valuemapMC :: Monad m => (a -> m b) -> ConduitT a b m ()
#

Apply a monadic transformation to all values in a stream.

If you do not need the transformed values, and instead just want the monadic side-effects of running the action, see mapM_.

valueomapMCE
  1. :: (Monad m, MonoTraversable mono)
  2. => Element mono -> m (Element mono)
  3. -> ConduitT mono mono m ()
#

Apply a monadic monomorphic transformation to all elements in a chunked stream.

Unlike mapME, this will work on types like ByteString and Text which are MonoFunctor but not Functor.

valueconcatMapMC
  1. :: (Monad m, MonoFoldable mono)
  2. => a -> m mono
  3. -> ConduitT a (Element mono) m ()
#

Apply the monadic function to each value in the stream, resulting in a foldable value (e.g., a list). Then yield each of the individual values in that foldable value separately.

Generalizes concatMapM, mapMaybeM, and mapFoldableM.

valuefilterMC :: Monad m => (a -> m Bool) -> ConduitT a a m ()
#

Keep only values in the stream passing a given monadic predicate.

valueiterMC :: Monad m => (a -> m ()) -> ConduitT a a m ()
#

Apply a monadic action on all values in a stream.

This Conduit can be used to perform a monadic side-effect for every value, whilst passing the value through the Conduit as-is.

iterM f = mapM (\a -> f a >>= \() -> return a)
valuescanlMC :: Monad m => (a -> b -> m a) -> a -> ConduitT b a m ()
#

Analog of Prelude.scanl for lists, monadic.

valuelineC
  1. :: (Monad m, IsSequence seq, Element seq ~ Char)
  2. => ConduitT seq o m r
  3. -> ConduitT seq o m r
#

Stream in the entirety of a single line.

Like takeExactly, this will consume the entirety of the line regardless of the behavior of the inner Conduit.

valuelinesUnboundedC :: (Monad m, IsSequence seq, Element seq ~ Char) => ConduitT seq seq m ()
#

Convert a stream of arbitrarily-chunked textual data into a stream of data where each chunk represents a single line. Note that, if you have unknownuntrusted input, this function is unsafe/, since it would allow an attacker to form lines of massive length and exhaust memory.

valuevectorBuilderC
  1. :: (PrimMonad m, Vector v e, PrimMonad n, PrimState m ~ PrimState n)
  2. => Int

    size

  3. -> ((e -> n ()) -> ConduitT i Void m r)
  4. -> ConduitT i (v e) m r
#

Generally speaking, yielding values from inside a Conduit requires some allocation for constructors. This can introduce an overhead, similar to the overhead needed to represent a list of values instead of a vector. This overhead is even more severe when talking about unboxed values.

This combinator allows you to overcome this overhead, and efficiently fill up vectors. It takes two parameters. The first is the size of each mutable vector to be allocated. The second is a function. The function takes an argument which will yield the next value into a mutable vector.

Under the surface, this function uses a number of tricks to get high performance. For more information on both usage and implementation, please see: https://www.fpcomplete.com/user/snoyberg/library-documentation/vectorbuilder

Monadic lifting

5 declarations
classclass Monad m => MonadIO (m :: Type -> Type) where
#

Monads in which IO computations may be embedded. Any monad built by applying a sequence of monad transformers to the IO monad will be an instance of this class.

Instances should satisfy the following laws, which state that liftIO is a transformer of monads:

Methods

  • liftIO :: IO a -> m a

    Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

    Example
    import Control.Monad.Trans.State -- from the "transformers" library
    
    printState :: Show s => StateT s IO ()
    printState = do
      state <- get
      liftIO $ print state

    Had we omitted liftIO, we would have ended up with this error:

    • Couldn't match type ‘IO’ with ‘StateT s IO’
     Expected type: StateT s IO ()
       Actual type: IO ()

    The important part here is the mismatch between StateT s IO () and IO ().

    Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

    > evalStateT printState "hello"
    "hello"
    
    > evalStateT printState 3
    3
    
Instances22MonadIO, …
classclass (forall (m :: Type -> Type). Monad m => Monad (t m)) => MonadTrans (t :: (Type -> Type) -> Type -> Type) where
#

The class of monad transformers. For any monad m, the result t m should also be a monad, and lift should be a monad transformation from m to t m, i.e. it should satisfy the following laws:

Since 0.6.0.0 and for GHC 8.6 and later, the requirement that t m be a Monad is enforced by the implication constraint forall m. Monad m => Monad (t m) enabled by the QuantifiedConstraints extension.

Ambiguity error with GHC 9.0 to 9.2.2

These versions of GHC have a bug (https://gitlab.haskell.org/ghc/ghc/-/issues/20582) which causes constraints like

(MonadTrans t, forall m. Monad m => Monad (t m)) => ...

to be reported as ambiguous. For transformers 0.6 and later, this can be fixed by removing the second constraint, which is implied by the first.

Methods

  • lift :: Monad m => m a -> t m a

    Lift a computation from the argument monad to the constructed monad.

Instances19MonadTrans, …
classclass Monad m => MonadThrow (m :: Type -> Type) where
#

A class for monads in which exceptions may be thrown.

Instances should obey the following law:

throwM e >> x = throwM e

In other words, throwing an exception short-circuits the rest of the monadic computation.

Methods

  • throwM :: (HasCallStack, Exception e) => e -> m a

    Throw an exception. Note that this throws when this action is run in the monad m, not when it is applied. It is a generalization of Control.Exception's throwIO.

    Should satisfy the law:

    throwM e >> f = throwM e
Instances22MonadThrow, …
classclass MonadIO m => MonadUnliftIO (m :: Type -> Type) where
#

Monads which allow their actions to be run in IO.

While MonadIO allows an IO action to be lifted into another monad, this class captures the opposite concept: allowing you to capture the monadic context. Note that, in order to meet the laws given below, the intuition is that a monad must have no monadic state, but may have monadic context. This essentially limits MonadUnliftIO to ReaderT and IdentityT transformers on top of IO.

Laws. For any function run provided by withRunInIO, it must meet the monad transformer laws as reformulated for MonadUnliftIO:

  • run . return = return
  • run (m >>= f) = run m >>= run . f

Instances of MonadUnliftIO must also satisfy the following laws:

Identity law

withRunInIO (\run -> run m) = m

Inverse law

withRunInIO (\_ -> m) = liftIO m

As an example of an invalid instance, a naive implementation of MonadUnliftIO (StateT s m) might be

withRunInIO inner =
  StateT $ \s ->
    withRunInIO $ \run ->
      inner (run . flip evalStateT s)

This breaks the identity law because the inner run m would throw away any state changes in m.

Methods

  • withRunInIO :: ((forall a. m a -> IO a) -> IO b) -> m b

    Convenience function for capturing the monadic context and running an IO action with a runner function. The runner function is used to run a monadic action m in IO.

Instances4MonadUnliftIO
classclass Monad m => PrimMonad (m :: Type -> Type) where
#

Class of monads which can perform primitive state-transformer actions.

Associated types

Methods

Instances21PrimMonad, …

ResourceT

3 declarations
classclass MonadIO m => MonadResource (m :: Type -> Type) where
#

A Monad which allows for safe resource allocation. In theory, any monad transformer stack which includes a ResourceT can be an instance of MonadResource.

Note: runResourceT has a requirement for a MonadUnliftIO m monad, which allows control operations to be lifted. A MonadResource does not have this requirement. This means that transformers such as ContT can be an instance of MonadResource. However, the ContT wrapper will need to be unwrapped before calling runResourceT.

Since 0.3.0

Instances14MonadResource, …
newtypenewtype ResourceT (m :: Type -> Type) a
#

The Resource transformer. This transformer keeps track of all registered actions, and calls them upon exit (via runResourceT). Actions may be registered via register, or resources may be allocated atomically via allocate. allocate corresponds closely to bracket.

Releasing may be performed before exit via the release function. This is a highly recommended optimization, as it will ensure that scarce resources are freed early. Note that calling release will deregister the action, so that a release action will only ever be called once.

Since 0.3.0

Instances22MonadTrans, MonadRWS, MonadError, MonadReader, MonadState, MonadWriter, …
valuerunResourceT :: MonadUnliftIO m => ResourceT m a -> m a
#

Unwrap a ResourceT transformer, and call all registered release actions.

Note that there is some reference counting involved due to resourceForkIO. If multiple threads are sharing the same collection of resources, only the last call to runResourceT will deallocate the resources.

NOTE Since version 1.2.0, this function will throw a ResourceCleanupException if any of the cleanup functions throw an exception.

Acquire

7 declarations
newtypenewtype Acquire a
#

A method for acquiring a scarce resource, providing the means of freeing it when no longer needed. This data type provides Functor/Applicative/Monad instances for composing different resources together. You can allocate these resources using either the bracket pattern (via with) or using ResourceT (via allocateAcquire).

This concept was originally introduced by Gabriel Gonzalez and described at: http://www.haskellforall.com/2013/06/the-resource-applicative.html. The implementation in this package is slightly different, due to taking a different approach to async exception safety.

Instances4Monad, Functor, Applicative, MonadIO
valuemkAcquire
  1. :: IO a

    acquire the resource

  2. -> (a -> IO ())

    free the resource

  3. -> Acquire a
#

Create an Acquire value using the given allocate and free functions.

To acquire and free the resource in an arbitrary monad with MonadUnliftIO, do the following:

acquire <- withRunInIO $ \runInIO ->
  return $ mkAcquire (runInIO create) (runInIO . free)

Note that this is only safe if the Acquire is run and freed within the same monadic scope it was created in.

valuemkAcquireType
  1. :: IO a

    acquire the resource

  2. -> (a -> ReleaseType -> IO ())

    free the resource

  3. -> Acquire a
#

Same as mkAcquire, but the cleanup function will be informed of how cleanup was initiated. This allows you to distinguish, for example, between normal and exceptional exits.

To acquire and free the resource in an arbitrary monad with MonadUnliftIO, do the following:

acquire <- withRunInIO $ \runInIO ->
  return $ mkAcquireType (runInIO create) (\a -> runInIO . free a)

Note that this is only safe if the Acquire is run and freed within the same monadic scope it was created in.

Pure pipelines

1 declaration
newtypenewtype Identity a
#

Identity functor and monad. (a non-strict monad)

Examples
Example1 expression
fmap (+1) (Identity 0)Identity 1
Example1 expression
Identity [1, 2, 3] <> Identity [4, 5, 6]Identity [1,2,3,4,5,6]
>>> do
      x <- Identity 10
      y <- Identity (x + 5)
      pure (x + y)
Identity 25

Constructors

Instances55Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …