module Configuration.Utils.Internal
(
lens
, over
, set
, view
, Lens'
, Lens
, Iso'
, Iso
, iso
, (&)
, (<&>)
, sshow
, exceptT
, errorT
, fmapL
) where
import Control.Applicative (Const(..))
import Control.Monad
import Control.Monad.Reader.Class
import Control.Monad.Except
import Data.Functor.Identity
import Data.Monoid.Unicode
import Data.Profunctor
import Data.Profunctor.Unsafe
import Data.String
import qualified Data.Text as T
import Prelude.Unicode
infixl 1 &, <&>
type Lens σ τ α β = Functor φ ⇒ (α → φ β) → σ → φ τ
type Lens' σ α = Lens σ σ α α
lens ∷ (σ → α) → (σ → β → τ) → Lens σ τ α β
lens getter setter lGetter s = setter s `fmap` lGetter (getter s)
over ∷ ((α → Identity β) → σ → Identity τ) → (α → β) → σ → τ
over s f = runIdentity . s (Identity . f)
set ∷ ((α → Identity β) → σ → Identity τ) → β → σ → τ
set s a = runIdentity . s (const $ Identity a)
view ∷ MonadReader σ μ ⇒ ((α → Const α α) → σ → Const α σ) → μ α
view l = asks (getConst #. l Const)
type Iso σ τ α β = (Profunctor π, Functor φ) ⇒ π α (φ β) → π σ (φ τ)
type Iso' σ α = Iso σ σ α α
iso ∷ (σ → α) → (β → τ) → Iso σ τ α β
iso f g = dimap f (fmap g)
(&) ∷ α → (α → β) → β
(&) = flip ($)
(<&>) ∷ Functor φ ⇒ φ α → (α → β) → φ β
(<&>) = flip fmap
sshow
∷ (Show α, IsString τ)
⇒ α
→ τ
sshow = fromString ∘ show
exceptT
∷ Monad μ
⇒ (ε → μ β)
→ (α → μ β)
→ ExceptT ε μ α
→ μ β
exceptT a b = runExceptT >=> either a b
errorT
∷ Monad μ
⇒ ExceptT T.Text μ α
→ μ α
errorT = exceptT (\e → error ∘ T.unpack $ "Error: " ⊕ e) return
fmapL ∷ (α → β) → Either α γ → Either β γ
fmapL f = either (Left ∘ f) Right