Skip to content

Commit d173b5f

Browse files
vrom911chshersh
authored andcommitted
[#122] Add separate combinator (#167)
* [#122] Add separate combinator Resolves #122 * Fix example, fix windows doctest
1 parent b9fc9ad commit d173b5f

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

co-log-core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
`co-log-core` uses [PVP Versioning][1].
44
The change log is available [on GitHub][2].
55

6+
## Unreleased
7+
8+
* [#122](https://github.com/kowainik/co-log/issues/122):
9+
Add the `separate` combinator.
10+
611
## 0.2.0.0 — May 5, 2019
712

813
* [#85](https://github.com/kowainik/co-log/issues/85):

co-log-core/co-log-core.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.4
22
name: co-log-core
3-
version: 0.2.0.0
3+
version: 0.2.1.0
44
synopsis: Composable Contravariant Comonadic Logging Library
55
description:
66
This package provides core types and functions to work with the @LogAction@ data type which is both simple and powerful.

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ module Colog.Core.Action
5353
, (<<=)
5454
, duplicate
5555
, multiplicate
56+
, separate
5657

5758
-- * Higher-order combinators
5859
, hoistLogAction
5960
) where
6061

6162
import Control.Monad (when, (<=<), (>=>))
6263
import Data.Coerce (coerce)
63-
import Data.Foldable (fold, for_)
64+
import Data.Foldable (fold, for_, traverse_)
6465
import Data.List.NonEmpty (NonEmpty (..))
6566
import Data.Monoid (Monoid (..))
6667
import Data.Semigroup (Semigroup (..), stimesMonoid)
@@ -619,6 +620,34 @@ multiplicate (LogAction l) = LogAction $ \msgs -> l (fold msgs)
619620
{-# SPECIALIZE multiplicate :: Monoid msg => LogAction m msg -> LogAction m [msg] #-}
620621
{-# SPECIALIZE multiplicate :: Monoid msg => LogAction m msg -> LogAction m (NonEmpty msg) #-}
621622

623+
{- | Like 'multiplicate' but instead of logging a batch of messages it logs each
624+
of them separately.
625+
626+
>>> :{
627+
let logger :: LogAction IO Int
628+
logger = logPrint
629+
in separate logger <& [1..5]
630+
:}
631+
1
632+
2
633+
3
634+
4
635+
5
636+
637+
@since 0.2.1.0
638+
-}
639+
separate
640+
:: forall f msg m .
641+
(Traversable f, Applicative m)
642+
=> LogAction m msg
643+
-> LogAction m (f msg)
644+
separate (LogAction action) = LogAction (traverse_ action)
645+
{-# INLINE separate #-}
646+
{-# SPECIALIZE separate :: Applicative m => LogAction m msg -> LogAction m [msg] #-}
647+
{-# SPECIALIZE separate :: Applicative m => LogAction m msg -> LogAction m (NonEmpty msg) #-}
648+
{-# SPECIALIZE separate :: LogAction IO msg -> LogAction IO [msg] #-}
649+
{-# SPECIALIZE separate :: LogAction IO msg -> LogAction IO (NonEmpty msg) #-}
650+
622651
{- | Allows changing the internal monadic action.
623652
624653
Let's say we have a pure logger action using 'PureLogger'

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE CPP #-}
2+
13
{- |
24
Copyright: (c) 2018-2020 Kowainik
35
SPDX-License-Identifier: MPL-2.0
@@ -76,9 +78,13 @@ opening file each time we need to write to it.
7678
7779
Opens file in 'AppendMode'.
7880
81+
#ifndef mingw32_HOST_OS
82+
7983
>>> logger action = action <& "foo"
8084
>>> withLogStringFile "/dev/stdout" logger
8185
foo
86+
87+
#endif
8288
-}
8389
withLogStringFile :: MonadIO m => FilePath -> (LogAction m String -> IO r) -> IO r
8490
withLogStringFile path action = withFile path AppendMode $ action . logStringHandle

co-log-core/test/Doctests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Test.DocTest (doctest)
77

88
main :: IO ()
99
main = do
10-
sourceFiles <- glob "co-log-core/src/**/*.hs"
10+
sourceFiles <- glob "src/**/*.hs"
1111
doctest
1212
$ "-XInstanceSigs"
1313
: "-XScopedTypeVariables"

0 commit comments

Comments
 (0)