module Lens.Family.Stock (
_1, _2
, chosen
, ix
, at, intAt
, at', intAt'
, contains, intContains
, left, right
, just, nothing
, both
, bend, lend
, cod
, both'
, bend', lend'
, both_
, bend_, lend_
, left_, right_
, just_, nothing_
, ignored
, mapped
, alongside
, backwards
, beside, beside', beside_
, choosing
, from
, AlongsideLeft, AlongsideRight
, FromF, FromG
, AdapterLike, AdapterLike'
, LensLike, LensLike'
, GrateLike, GrateLike'
, Identical, Backwards
, FiniteBits
, lft, rgt
, some, none
, lft_, rgt_
, some_, none_
) where
import Control.Arrow (first, second)
import Control.Applicative.Backwards (Backwards(..))
import Control.Applicative (liftA2)
import Data.Bits (FiniteBits, (.|.), bit, finiteBitSize, testBit, zeroBits)
import qualified Data.IntMap as IntMap
import qualified Data.IntMap.Strict as IntMap'
import qualified Data.IntSet as IntSet
import qualified Data.Map as Map
import qualified Data.Map.Strict as Map'
import Data.Proxy (asProxyTypeOf)
import qualified Data.Set as Set
import Lens.Family
import Lens.Family.Phantom
import Lens.Family.Unchecked
choosing :: Functor f => LensLike f s0 t0 a b -> LensLike f s1 t1 a b -> LensLike f (Either s0 s1) (Either t0 t1) a b
choosing la _ f (Left a) = Left <$> la f a
choosing _ lb f (Right b) = Right <$> lb f b
_1 :: Functor f => LensLike f (a, r) (b, r) a b
_1 f (a, r) = (\b -> (b, r)) <$> f a
_2 :: Functor f => LensLike f (r, a) (r, b) a b
_2 f (r, a) = (\b -> (r, b)) <$> f a
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) <$> 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)
cod :: Functor g => GrateLike g (r -> a) (r -> b) a b
cod f h r = f $ ($ r) <$> h
left :: (Applicative f, Traversable g) => AdapterLike f g (Either a r) (Either b r) a b
left f = either (pure . Right) (fmap Left . f) . traverse switch
where
switch = either Right Left
left_ :: Applicative f => LensLike f (Either a r) (Either b r) a b
left_ = under left
right :: (Applicative f, Traversable g) => AdapterLike f g (Either r a) (Either r b) a b
right f = either (pure . Left) (fmap Right . f) . sequenceA
right_ :: Applicative f => LensLike f (Either r a) (Either r b) a b
right_ = under right
just :: (Applicative f, Traversable g) => AdapterLike f g (Maybe a) (Maybe b) a b
just f = maybe (pure Nothing) (fmap Just . f) . sequenceA
just_ :: Applicative f => LensLike f (Maybe a) (Maybe b) a b
just_ = under just
nothing :: (Applicative f, Traversable g) => AdapterLike' f g (Maybe a) ()
nothing = prism (maybe (Right ()) (Left . Just)) (const Nothing)
nothing_ :: Applicative f => LensLike' f (Maybe a) ()
nothing_ = under nothing
both :: (Applicative f, Functor g) => AdapterLike f g (a,a) (b,b) a b
both = beside id id
both' :: Functor g => GrateLike g (a,a) (b,b) a b
both' = beside' id id
both_ :: Applicative f => LensLike f (a,a) (b,b) a b
both_ = beside_ id id
lend :: (FiniteBits b, Applicative f, Functor g) => AdapterLike' f g b Bool
lend f s = foldr (liftA2 (.|.)) (pure zeroBits) [mask i <$> f (flip testBit i <$> s) | i <- [0..finiteBitSize b-1]]
where
mask i True = bit i
mask _ False = zeroBits
b = b `asProxyTypeOf` s
lend' :: (FiniteBits b, Functor g) => GrateLike' g b Bool
lend' = over lend
lend_ :: (FiniteBits b, Applicative f) => LensLike' f b Bool
lend_ = under lend
bend :: (FiniteBits b, Applicative f, Functor g) => AdapterLike' f g b Bool
bend = backwards lend
bend' :: (FiniteBits b, Functor g) => GrateLike' g b Bool
bend' = over bend
bend_ :: (FiniteBits b, Applicative f) => LensLike' f b Bool
bend_ = under bend
beside :: (Applicative f, Functor g) => AdapterLike f g s0 t0 a b -> AdapterLike f g s1 t1 a b -> AdapterLike f g (s0, s1) (t0, t1) a b
beside la lb f s = (,) <$> la f (fst <$> s) <*> lb f (snd <$> s)
beside' :: Functor g => GrateLike g s0 t0 a b -> GrateLike g s1 t1 a b -> GrateLike g (s0, s1) (t0, t1) a b
beside' la lb = over $ beside (setting la) (setting lb)
beside_ :: Applicative f => LensLike f s0 t0 a b -> LensLike f s1 t1 a b -> LensLike f (s0, s1) (t0, t1) a b
beside_ la lb = under $ beside (resetting la) (resetting lb)
ignored :: Applicative f => null -> s -> f s
ignored _ = pure
mapped :: (Identical f, Functor h) => LensLike f (h a) (h b) a b
mapped = setting fmap
backwards :: LensLike (Backwards f) s t a b -> LensLike f s t a b
backwards l f = forwards . l (Backwards . f)
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 b1) s0 t0 a0 b0
-> LensLike (AlongsideRight f t0) s1 t1 a1 b1
-> LensLike f (s0, s1) (t0, t1) (a0, a1) (b0, b1)
alongside l0 l1 f (s0, s1) = ft0t1
where
AlongsideRight ft0t1 = l1 f1 s1
f1 a1 = AlongsideRight ft0a1
where
AlongsideLeft ft0a1 = l0 f0 s0
f0 a0 = AlongsideLeft (f (a0, a1))
newtype FromF i j g x = FromF ((g x -> j) -> i)
instance Functor g => Functor (FromF i j g) where
fmap f (FromF h) = FromF $ \k -> h (k . fmap f)
instance Phantom g => Phantom (FromF i j g) where
coerce (FromF h) = FromF $ \k -> h (k . coerce)
newtype FromG e f x = FromG (e -> f x)
instance Functor f => Functor (FromG e f) where
fmap f (FromG h) = FromG $ fmap f . h
instance Phantom g => Phantom (FromG e g) where
coerce (FromG h) = FromG $ coerce . h
from :: (Functor f, Functor g)
=> AdapterLike (FromF (g s -> f t) (f b) g) (FromG (f b) f) b a t s
-> AdapterLike f g s t a b
from l = l'
where
FromF l' = l (\(FromG h1) -> FromF $ (.) h1) (FromG id)
{-# DEPRECATED lft "Renamed as 'left'." #-}
lft :: (Applicative f, Traversable g) => AdapterLike f g (Either a r) (Either b r) a b
lft = left
{-# DEPRECATED lft_ "Renamed as 'left_'." #-}
lft_ :: Applicative f => LensLike f (Either a r) (Either b r) a b
lft_ = left_
{-# DEPRECATED rgt "Renamed as 'right'." #-}
rgt :: (Applicative f, Traversable g) => AdapterLike f g (Either r a) (Either r b) a b
rgt = right
{-# DEPRECATED rgt_ "Renamed as 'right_'." #-}
rgt_ :: Applicative f => LensLike f (Either r a) (Either r b) a b
rgt_ = right_
{-# DEPRECATED some "Renamed as 'just'." #-}
some :: (Applicative f, Traversable g) => AdapterLike f g (Maybe a) (Maybe b) a b
some = just
{-# DEPRECATED some_ "Renamed as 'just_'." #-}
some_ :: Applicative f => LensLike f (Maybe a) (Maybe b) a b
some_ = just_
{-# DEPRECATED none "Renamed as 'nothing'." #-}
none :: (Applicative f, Traversable g) => AdapterLike' f g (Maybe a) ()
none = nothing
{-# DEPRECATED none_ "Renamed as 'nothing_'." #-}
none_ :: Applicative f => LensLike' f (Maybe a) ()
none_ = nothing_