module Lens.Family.Stock (
choosing
, alongside
, beside
, _1, _2
, chosen
, ix
, at, intAt
, at', intAt'
, contains, intContains
, both
, _Left, _Right
, _Just, _Nothing
, ignored
, mapped
, AlongsideLeft, AlongsideRight
, LensLike, LensLike'
, Applicative, Identical
) where
import Control.Arrow (first, second)
import Control.Applicative (Applicative, pure, (<$>), (<*>))
import Lens.Family (LensLike, LensLike')
import Lens.Family.Unchecked (lens, setting, Identical)
import Lens.Family.Phantom (Phantom, coerce)
import qualified Data.Map as Map
import qualified Data.IntMap as IntMap
import qualified Data.Map.Strict as Map'
import qualified Data.IntMap.Strict as IntMap'
import qualified Data.Set as Set
import qualified Data.IntSet as IntSet
choosing :: Functor f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (Either a b) (Either a' b') c c'
choosing la _ f (Left a) = Left `fmap` la f a
choosing _ lb f (Right b) = Right `fmap` lb f b
_1 :: Functor f => LensLike f (a, b) (a', b) a a'
_1 f (a, b) = (\a' -> (a', b)) `fmap` f a
_2 :: Functor f => LensLike f (a, b) (a, b') b b'
_2 f (a, b) = (\b' -> (a, b')) `fmap` f b
chosen :: Functor f => LensLike f (Either a a) (Either b b) a b
chosen = choosing id id
ix :: (Eq k, Functor f) => k -> LensLike' f (k -> v) v
ix k f g = (\v' x -> if (k == x) then v' else g x) `fmap` f (g k)
at :: (Ord k, Functor f) => k -> LensLike' f (Map.Map k v) (Maybe v)
at = flip Map.alterF
intAt :: Functor f => Int -> LensLike' f (IntMap.IntMap v) (Maybe v)
intAt = flip IntMap.alterF
at' :: (Ord k, Functor f) => k -> LensLike' f (Map.Map k v) (Maybe v)
at' = flip Map'.alterF
intAt' :: Functor f => Int -> LensLike' f (IntMap.IntMap v) (Maybe v)
intAt' = flip IntMap'.alterF
contains :: (Ord k, Functor f) => k -> LensLike' f (Set.Set k) Bool
contains k = lens (Set.member k) (\m nv -> if nv then Set.insert k m else Set.delete k m)
intContains :: Functor f => Int -> LensLike' f IntSet.IntSet Bool
intContains k = lens (IntSet.member k) (\m nv -> if nv then IntSet.insert k m else IntSet.delete k m)
_Left :: Applicative f => LensLike f (Either a b) (Either a' b) a a'
_Left f (Left a) = Left <$> f a
_Left _ (Right b) = pure (Right b)
_Right :: Applicative f => LensLike f (Either a b) (Either a b') b b'
_Right f (Right b) = Right <$> f b
_Right _ (Left a) = pure (Left a)
_Just :: Applicative f => LensLike f (Maybe a) (Maybe a') a a'
_Just f (Just a) = Just <$> f a
_Just _ Nothing = pure Nothing
_Nothing :: Applicative f => LensLike' f (Maybe a) ()
_Nothing f Nothing = const Nothing <$> f ()
_Nothing _ j = pure j
both :: Applicative f => LensLike f (a,a) (b,b) a b
both f (x,y) = (,) <$> f x <*> f y
beside :: Applicative f => LensLike f a a' c c' -> LensLike f b b' c c' -> LensLike f (a,b) (a',b') c c'
beside la lb f (x,y) = (,) <$> la f x <*> lb f y
ignored :: Applicative f => null -> a -> f a
ignored _ = pure
mapped :: (Identical f, Functor g) => LensLike f (g a) (g a') a a'
mapped = setting fmap
newtype AlongsideLeft f b a = AlongsideLeft (f (a, b))
instance Functor f => Functor (AlongsideLeft f a) where
fmap f (AlongsideLeft x) = AlongsideLeft (fmap (first f) x)
instance Phantom f => Phantom (AlongsideLeft f a) where
coerce (AlongsideLeft x) = AlongsideLeft (coerce x)
newtype AlongsideRight f a b = AlongsideRight (f (a, b))
instance Functor f => Functor (AlongsideRight f a) where
fmap f (AlongsideRight x) = AlongsideRight (fmap (second f) x)
instance Phantom f => Phantom (AlongsideRight f a) where
coerce (AlongsideRight x) = AlongsideRight (coerce x)
alongside :: Functor f => LensLike (AlongsideLeft f b2') a1 a1' b1 b1'
-> LensLike (AlongsideRight f a1') a2 a2' b2 b2'
-> LensLike f (a1, a2) (a1', a2') (b1, b2) (b1', b2')
alongside l1 l2 f (a1, a2) = fa1'a2'
where
AlongsideRight fa1'a2' = l2 f2 a2
f2 b2 = AlongsideRight fa1'b2'
where
AlongsideLeft fa1'b2' = l1 f1 a1
f1 b1 = AlongsideLeft (f (b1, b2))