-- |Description: Internal
module Exon.Combinators where

import Prelude hiding (intercalate)

-- |Monoidally combine all elements in the list, appending the separator between each pair of elements.
intercalate ::
  Monoid a =>
  Foldable t =>
  a ->
  t a ->
  a
intercalate :: forall a (t :: * -> *). (Monoid a, Foldable t) => a -> t a -> a
intercalate a
sep =
  forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Maybe a -> a -> Maybe a
f forall a. Maybe a
Nothing
  where
    f :: Maybe a -> a -> Maybe a
f Maybe a
Nothing a
a =
      forall a. a -> Maybe a
Just a
a
    f (Just a
z) a
a =
      forall a. a -> Maybe a
Just (a
z forall a. Semigroup a => a -> a -> a
<> a
sep forall a. Semigroup a => a -> a -> a
<> a
a)