Relude.Extra.Tuple

relude-1.2.2.0: Safe, performant, user-friendly and lightweight Haskell Standard Library

Copyright | (c) 2018-2023 Kowainik
License | MIT
Maintainer | Kowainik <xrom.xkov@gmail.com>
Stability | Stable
Portability | Portable
Safe Haskell | Safe
Language | Haskell2010

Relude.Extra.Tuple

Description

Contains utility functions for working with tuples.

Since: 0.4.0

Synopsis

- dup :: a -> (a, a)
- toFst :: (a -> b) -> a -> (b, a)
- toSnd :: (a -> b) -> a -> (a, b)
- fmapToFst :: Functor f => (a -> b) -> f a -> f (b, a)
- fmapToSnd :: Functor f => (a -> b) -> f a -> f (a, b)
- mapToFst :: (a -> b) -> a -> (b, a)
- mapToSnd :: (a -> b) -> a -> (a, b)
- traverseToFst :: Functor t => (a -> t b) -> a -> t (b, a)
- traverseToSnd :: Functor t => (a -> t b) -> a -> t (a, b)
- traverseBoth :: Applicative t => (a -> t b) -> (a, a) -> t (b, b)

# Documentation

dup :: a -> (a, a) #

Creates a tuple by pairing something with itself.

    >>> dup "foo"
    ("foo","foo")
    >>> dup ()
    ((),())

Since: 0.6.0.0

toFst :: (a -> b) -> a -> (b, a) #

Apply a function, with the result in the fst slot, and the value in the other.

A dual to `toSnd`.

    >>> toFst length [3, 1, 0, 2]
    (4,[3,1,0,2])
    >>> toFst (+5) 10
    (15,10)

Since: 0.7.0.0

toSnd :: (a -> b) -> a -> (a, b) #

Apply a function, with the result in the snd slot, and the value in the other.

A dual to `toFst`.

    >>> toSnd length [3, 1, 0, 2]
    ([3,1,0,2],4)
    >>> toSnd (+5) 10
    (10,15)

Since: 0.7.0.0

fmapToFst :: Functor f => (a -> b) -> f a -> f (b, a) #

Like `fmap`, but also keep the original value in the snd position.

A dual to `fmapToSnd`.

    >>> fmapToFst show [3, 10, 2]
    [("3",3),("10",10),("2",2)]

Since: 0.7.0.0

fmapToSnd :: Functor f => (a -> b) -> f a -> f (a, b) #

Like `fmap`, but also keep the original value in the fst position.

A dual to `fmapToFst`.

    >>> fmapToSnd show [3, 10, 2]
    [(3,"3"),(10,"10"),(2,"2")]

Since: 0.7.0.0

mapToFst :: (a -> b) -> a -> (b, a) #

Deprecated: Use `toFst` from `Tuple` instead

Apply a function, with the result in the fst slot, and the value in the other.

A dual to `mapToSnd`

    >>> mapToFst (+1) 10
    (11,10)

Since: 0.4.0

mapToSnd :: (a -> b) -> a -> (a, b) #

Deprecated: Use `toSnd` from `Tuple` instead

Apply a function, with the result in the second slot, and the value in the other.

A dual to `mapToFst`.

    >>> mapToSnd (+1) 10
    (10,11)

Since: 0.4.0

traverseToFst :: Functor t => (a -> t b) -> a -> t (b, a) #

Apply a function that returns a value inside of a functor, with the output in the first slot, the input in the second, and the entire tuple inside the functor.

A dual to `traverseToSnd`

    >>> traverseToFst (Just . (+1)) 10
    Just (11,10)
    >>> traverseToFst (const Nothing) 10
    Nothing

Since: 0.5.0

traverseToSnd :: Functor t => (a -> t b) -> a -> t (a, b) #

Apply a function that returns a value inside of a functor, with the output in the second slot, the input in the first, and the entire tuple inside the functor.

A dual to `traverseToFst`.

    >>> traverseToSnd (Just . (+1)) 10
    Just (10,11)
    >>> traverseToSnd (const Nothing) 10
    Nothing

Since: 0.5.0

traverseBoth :: Applicative t => (a -> t b) -> (a, a) -> t (b, b) #

Maps a function that returns a value inside of an applicative functor over both elements of a tuple, with the entire tuple inside the applicative functor.

    >>> traverseBoth (Just . ("Hello " <>)) ("Alice", "Bob")
    Just ("Hello Alice","Hello Bob")
    >>> traverseBoth (const Nothing) ("Alice", "Bob")
    Nothing