Data.Tuple.Compat

base-compat-0.12.1: A compatibility layer for base

Safe Haskell | Safe
Language | Haskell2010

Data.Tuple.Compat

Synopsis

- fst :: (a, b) -> a
- snd :: (a, b) -> b
- curry :: ((a, b) -> c) -> a -> b -> c
- uncurry :: (a -> b -> c) -> (a, b) -> c
- swap :: (a, b) -> (b, a)
- data Solo a = Solo a

# Documentation

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

Extract the first component of a pair.

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

Extract the second component of a pair.

curry :: ((a, b) -> c) -> a -> b -> c #

`curry` converts an uncurried function to a curried function.

#### Examples

Expand

    >>> curry fst 1 2
    1

uncurry :: (a -> b -> c) -> (a, b) -> c #

`uncurry` converts a curried function to a function on pairs.

#### Examples

Expand

    >>> uncurry (+) (1,2)
    3

    >>> uncurry ($) (show, 1)
    "1"

    >>> map (uncurry max) [(1,2), (3,4), (6,8)]
    [2,4,8]

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

Swap the components of a pair.

data Solo a #

`Solo` is the canonical lifted 1-tuple, just like `(,)` is the canonical lifted 2-tuple (pair) and `(,,)` is the canonical lifted 3-tuple (triple).

The most important feature of `Solo` is that it is possible to force its "outside" (usually by pattern matching) without forcing its "inside", because it is defined as a datatype rather than a newtype. One situation where this can be useful is when writing a function to extract a value from a data structure. Suppose you write an implementation of arrays and offer only this function to index into them:

    index :: Array a -> Int -> a

Now imagine that someone wants to extract a value from an array and store it in a lazy-valued finite map/dictionary:

    insert "hello" (arr index 12) m

This can actually lead to a space leak. The value is not actually extracted from the array until that value (now buried in a map) is forced. That means the entire array may be kept live by just that value! Often, the solution is to use a strict map, or to force the value before storing it, but for some purposes that's undesirable.

One common solution is to include an indexing function that can produce its result in an arbitrary `Applicative` context:

    indexA :: Applicative f => Array a -> Int -> f a

When using `indexA` in a pure context, `Solo` serves as a handy `Applicative` functor to hold the result. You could write a non-leaky version of the above example thus:

    case arr indexA 12 of
      Solo a -> insert "hello" a m

While such simple extraction functions are the most common uses for unary tuples, they can also be useful for fine-grained control of strict-spined data structure traversals, and for unifying the implementations of lazy and strict mapping functions.

Constructors

Solo a |

#### Instances

Instances details

Foldable Solo | Since: base-4.15
Instance details Defined in Data.Foldable Methods fold :: Monoid m => Solo m -> m # foldMap :: Monoid m => (a -> m) -> Solo a -> m # foldMap' :: Monoid m => (a -> m) -> Solo a -> m # foldr :: (a -> b -> b) -> b -> Solo a -> b # foldr' :: (a -> b -> b) -> b -> Solo a -> b # foldl :: (b -> a -> b) -> b -> Solo a -> b # foldl' :: (b -> a -> b) -> b -> Solo a -> b # foldr1 :: (a -> a -> a) -> Solo a -> a # foldl1 :: (a -> a -> a) -> Solo a -> a # toList :: Solo a -> [a] # null :: Solo a -> Bool # length :: Solo a -> Int # elem :: Eq a => a -> Solo a -> Bool # maximum :: Ord a => Solo a -> a # minimum :: Ord a => Solo a -> a # sum :: Num a => Solo a -> a # product :: Num a => Solo a -> a #
Traversable Solo | Since: base-4.15
Instance details Defined in Data.Traversable Methods traverse :: Applicative f => (a -> f b) -> Solo a -> f (Solo b) # sequenceA :: Applicative f => Solo (f a) -> f (Solo a) # mapM :: Monad m => (a -> m b) -> Solo a -> m (Solo b) # sequence :: Monad m => Solo (m a) -> m (Solo a) #
Applicative Solo | Since: base-4.15
Instance details Defined in GHC.Base Methods pure :: a -> Solo a # (<*>) :: Solo (a -> b) -> Solo a -> Solo b # liftA2 :: (a -> b -> c) -> Solo a -> Solo b -> Solo c # (*>) :: Solo a -> Solo b -> Solo b # (<*) :: Solo a -> Solo b -> Solo a #
Functor Solo | Since: base-4.15
Instance details Defined in GHC.Base Methods fmap :: (a -> b) -> Solo a -> Solo b # (<$) :: a -> Solo b -> Solo a #
Monad Solo | Since: base-4.15
Instance details Defined in GHC.Base Methods (>>=) :: Solo a -> (a -> Solo b) -> Solo b # (>>) :: Solo a -> Solo b -> Solo b # return :: a -> Solo a #
Monoid a => Monoid (a) | Since: base-4.15
Instance details Defined in GHC.Base Methods mempty :: (a) # mappend :: (a) -> (a) -> (a) # mconcat :: [(a)] -> (a) #
Semigroup a => Semigroup (a) | Since: base-4.15
Instance details Defined in GHC.Base Methods (<>) :: (a) -> (a) -> (a) # sconcat :: NonEmpty (a) -> (a) # stimes :: Integral b => b -> (a) -> (a) #
Bounded a => Bounded (a) |
Instance details Defined in GHC.Enum Methods minBound :: (a) # maxBound :: (a) #
Enum a => Enum (a) |
Instance details Defined in GHC.Enum Methods succ :: (a) -> (a) # pred :: (a) -> (a) # toEnum :: Int -> (a) # fromEnum :: (a) -> Int # enumFrom :: (a) -> [(a)] # enumFromThen :: (a) -> (a) -> [(a)] # enumFromTo :: (a) -> (a) -> [(a)] # enumFromThenTo :: (a) -> (a) -> (a) -> [(a)] #
Read a => Read (a) | Since: base-4.15
Instance details Defined in GHC.Read Methods readsPrec :: Int -> ReadS (a) # readList :: ReadS [(a)] # readPrec :: ReadPrec (a) # readListPrec :: ReadPrec [(a)] #
Show a => Show (a) | Since: base-4.15
Instance details Defined in GHC.Show Methods showsPrec :: Int -> (a) -> ShowS # show :: (a) -> String # showList :: [(a)] -> ShowS #
Eq a => Eq (a) |
Instance details Defined in GHC.Classes Methods (==) :: (a) -> (a) -> Bool # (/=) :: (a) -> (a) -> Bool #
Ord a => Ord (a) |
Instance details Defined in GHC.Classes Methods compare :: (a) -> (a) -> Ordering # (<) :: (a) -> (a) -> Bool # (<=) :: (a) -> (a) -> Bool # (>) :: (a) -> (a) -> Bool # (>=) :: (a) -> (a) -> Bool # max :: (a) -> (a) -> (a) # min :: (a) -> (a) -> (a) #