Skip to content

Commit 97a8486

Browse files
jiribenesvrom911
authored andcommitted
[#138] Add higher-order transformation function (#154)
* [#138] Add higher-order transformation function * Add example and inline pragma to 'hoistLogAction'
1 parent 491e731 commit 97a8486

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

co-log-core/src/Colog/Core/Action.hs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
{-# LANGUAGE CPP #-}
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE Rank2Types #-}
23

34
{- |
45
Copyright: (c) 2018-2019 Kowainik
@@ -48,6 +49,9 @@ module Colog.Core.Action
4849
, (<<=)
4950
, duplicate
5051
, multiplicate
52+
53+
-- * Higher-order combinators
54+
, hoistLogAction
5155
) where
5256

5357
import Control.Monad (when, (>=>))
@@ -575,3 +579,28 @@ multiplicate (LogAction l) = LogAction $ \msgs -> l (fold msgs)
575579
{-# INLINE multiplicate #-}
576580
{-# SPECIALIZE multiplicate :: Monoid msg => LogAction m msg -> LogAction m [msg] #-}
577581
{-# SPECIALIZE multiplicate :: Monoid msg => LogAction m msg -> LogAction m (NonEmpty msg) #-}
582+
583+
{- | Allows changing the internal monadic action.
584+
585+
Let's say we have a pure logger action using 'PureLogger'
586+
and we want to log all messages into 'IO' instead.
587+
588+
If we provide the following function:
589+
590+
@
591+
performPureLogsInIO :: PureLogger a -> IO a
592+
@
593+
594+
then we can convert a logger action that uses a pure monad
595+
to a one that performs the logging in the 'IO' monad using:
596+
597+
@
598+
hoistLogAction performPureLogsInIO :: LogAction (PureLogger a) a -> LogAction IO a
599+
@
600+
-}
601+
hoistLogAction
602+
:: (forall x. m x -> n x)
603+
-> LogAction m a
604+
-> LogAction n a
605+
hoistLogAction f (LogAction l) = LogAction (f . l)
606+
{-# INLINE hoistLogAction #-}

0 commit comments

Comments
 (0)