-- |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 =
  Maybe a -> a
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Maybe a -> a) -> (t a -> Maybe a) -> t a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> a -> Maybe a) -> Maybe a -> t a -> Maybe a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Maybe a -> a -> Maybe a
f Maybe a
forall a. Maybe a
Nothing
  where
    f :: Maybe a -> a -> Maybe a
f Maybe a
Nothing a
a =
      a -> Maybe a
forall a. a -> Maybe a
Just a
a
    f (Just a
z) a
a =
      a -> Maybe a
forall a. a -> Maybe a
Just (a
z a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
sep a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a)