aboutsummaryrefslogtreecommitdiffstats
path: root/src/Test/VeriFuzz/Internal/Shared.hs
blob: bf96509d981b40757dc4ef50b266b1b86586bd00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{-|
Module      : Test.VeriFuzz.Internal.Shared
Description : Shared high level code used in the other modules internally.
Copyright   : (c) 2018-2019, Yann Herklotz Grave
License     : BSD-3
Maintainer  : ymherklotz [at] gmail [dot] com
Stability   : experimental
Portability : POSIX

Shared high level code used in the other modules internally.
-}

module Test.VeriFuzz.Internal.Shared where

import           Data.Maybe (fromMaybe)

-- | Fold up a list of Monoids using mappend and mempty as the first
-- element.
fromList :: (Foldable t, Monoid a) => t a -> a
fromList = foldl mappend mempty

-- | Combine the Monoid elements of a list and insert the seperation symbol in
-- between each element except the last one.
sep :: (Monoid a) => a -> [a] -> a
sep el l = fromMaybe mempty $
  (fromList . fmap (<>el) <$> safe init l) <> safe last l

-- | Alternative sep which returns the pattern if the list is empty.
sep_ :: (Monoid a) => a -> [a] -> a
sep_ el l
  | null l = mempty
  | otherwise = el <> sep el l

-- | Converts unsafe list functions in the Prelude to a safe version.
safe :: ([a] -> b) -> [a] -> Maybe b
safe _ [] = Nothing
safe f l  = Just $ f l