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 s t a b = ∀ f . Functor f ⇒ (a → f b) → s → f t
type Lens' s a = Lens s s a a
lens ∷ (s → a) → (s → b → t) → Lens s t a b
lens getter setter lGetter s = setter s `fmap` lGetter (getter s)
over ∷ ((a → Identity b) → s → Identity t) → (a → b) → s → t
over s f = runIdentity . s (Identity . f)
set ∷ ((a → Identity b) → s → Identity t) → b → s → t
set s a = runIdentity . s (const $ Identity a)
view ∷ MonadReader r m ⇒ ((a → Const a a) → r → Const a r) → m a
view l = asks (getConst #. l Const)
type Iso s t a b = ∀ p f . (Profunctor p, Functor f) ⇒ p a (f b) → p s (f t)
type Iso' s a = Iso s s a a
iso ∷ (s → a) → (b → t) → Iso s t a b
iso f g = dimap f (fmap g)
(&) ∷ a → (a → b) → b
(&) = flip ($)
(<&>) ∷ Functor f ⇒ f a → (a → b) → f b
(<&>) = flip fmap
sshow
∷ (Show a, IsString s)
⇒ a
→ s
sshow = fromString ∘ show
exceptT
∷ Monad m
⇒ (e → m b)
→ (a → m b)
→ ExceptT e m a
→ m b
exceptT a b = runExceptT >=> either a b
errorT
∷ Monad m
⇒ ExceptT T.Text m a
→ m a
errorT = exceptT (\e → error ∘ T.unpack $ "Error: " ⊕ e) return
fmapL ∷ (a → b) → Either a c → Either b c
fmapL f = either (Left ∘ f) Right