{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections #-}
module Relude.Extra.Tuple
( dup
, toFst
, toSnd
, fmapToFst
, fmapToSnd
, mapToFst
, mapToSnd
, traverseToFst
, traverseToSnd
, traverseBoth
) where
import Relude.Function ((.))
import Relude.Functor (Functor (..), (<$>))
import Relude.Applicative (Applicative (..))
dup :: a -> (a, a)
dup :: forall a. a -> (a, a)
dup a
a = (a
a, a
a)
{-# INLINE dup #-}
toFst :: (a -> b) -> a -> (b, a)
toFst :: forall a b. (a -> b) -> a -> (b, a)
toFst a -> b
f a
a = (a -> b
f a
a, a
a)
{-# INLINE toFst #-}
toSnd :: (a -> b) -> a -> (a, b)
toSnd :: forall a b. (a -> b) -> a -> (a, b)
toSnd a -> b
f a
a = (a
a, a -> b
f a
a)
{-# INLINE toSnd #-}
fmapToFst :: Functor f => (a -> b) -> f a -> f (b, a)
fmapToFst :: forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f (b, a)
fmapToFst = (a -> (b, a)) -> f a -> f (b, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> (b, a)) -> f a -> f (b, a))
-> ((a -> b) -> a -> (b, a)) -> (a -> b) -> f a -> f (b, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> a -> (b, a)
forall a b. (a -> b) -> a -> (b, a)
toFst
{-# INLINE fmapToFst #-}
fmapToSnd :: Functor f => (a -> b) -> f a -> f (a, b)
fmapToSnd :: forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f (a, b)
fmapToSnd = (a -> (a, b)) -> f a -> f (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> (a, b)) -> f a -> f (a, b))
-> ((a -> b) -> a -> (a, b)) -> (a -> b) -> f a -> f (a, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> a -> (a, b)
forall a b. (a -> b) -> a -> (a, b)
toSnd
{-# INLINE fmapToSnd #-}
mapToFst :: (a -> b) -> a -> (b, a)
mapToFst :: forall a b. (a -> b) -> a -> (b, a)
mapToFst = (a -> b) -> a -> (b, a)
forall a b. (a -> b) -> a -> (b, a)
toFst
{-# INLINE mapToFst #-}
{-# DEPRECATED mapToFst "Use 'toFst' from 'Relude.Extra.Tuple' instead" #-}
mapToSnd :: (a -> b) -> a -> (a, b)
mapToSnd :: forall a b. (a -> b) -> a -> (a, b)
mapToSnd = (a -> b) -> a -> (a, b)
forall a b. (a -> b) -> a -> (a, b)
toSnd
{-# INLINE mapToSnd #-}
{-# DEPRECATED mapToSnd "Use 'toSnd' from 'Relude.Extra.Tuple' instead" #-}
traverseToFst :: Functor t => (a -> t b) -> a -> t (b, a)
traverseToFst :: forall (t :: * -> *) a b. Functor t => (a -> t b) -> a -> t (b, a)
traverseToFst a -> t b
f a
a = (,a
a) (b -> (b, a)) -> t b -> t (b, a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> t b
f a
a
{-# INLINE traverseToFst #-}
traverseToSnd :: Functor t => (a -> t b) -> a -> t (a, b)
traverseToSnd :: forall (t :: * -> *) a b. Functor t => (a -> t b) -> a -> t (a, b)
traverseToSnd a -> t b
f a
a = (a
a,) (b -> (a, b)) -> t b -> t (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> t b
f a
a
{-# INLINE traverseToSnd #-}
traverseBoth :: Applicative t => (a -> t b) -> (a, a) -> t (b, b)
traverseBoth :: forall (t :: * -> *) a b.
Applicative t =>
(a -> t b) -> (a, a) -> t (b, b)
traverseBoth a -> t b
f (a
a1, a
a2) = (,) (b -> b -> (b, b)) -> t b -> t (b -> (b, b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> t b
f a
a1 t (b -> (b, b)) -> t b -> t (b, b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> t b
f a
a2
{-# INLINE traverseBoth #-}