Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
MCSP.Data.Pair
Contents
Description
Operations for working with a pair of elements of the same type.
Synopsis
- type Pair a = (a, a)
- pattern Pair :: a -> a -> Pair a
- left :: Pair a -> a
- right :: Pair a -> a
- pattern First :: a -> (a, b)
- pattern Second :: b -> (a, b)
- pattern (::|) :: Pair a -> [a] -> [a]
- fst :: (a, b) -> a
- uncurry :: (a -> b -> c) -> (a, b) -> c
- snd :: (a, b) -> b
- swap :: (a, b) -> (b, a)
- (***) :: (a -> a') -> (b -> b') -> (a, b) -> (a', b')
- first :: (a -> a') -> (a, b) -> (a', b)
- second :: (b -> b') -> (a, b) -> (a, b')
- (&&&) :: (a -> b) -> (a -> c) -> a -> (b, c)
- both :: (a -> b) -> (a, a) -> (b, b)
- dupe :: a -> (a, a)
- firstM :: Functor m => (a -> m a') -> (a, b) -> m (a', b)
- secondM :: Functor m => (b -> m b') -> (a, b) -> m (a, b')
- bothM :: Applicative m => (a -> m b) -> Pair a -> m (Pair b)
- zipM :: Applicative m => Pair (m a) -> m (Pair a)
- zip :: [a] -> [b] -> [(a, b)]
- unzip :: Functor f => f (a, b) -> (f a, f b)
- cartesian :: [a] -> [b] -> [(a, b)]
- liftP :: (a -> b -> c) -> Pair a -> Pair b -> Pair c
- transpose :: ((a, b), (c, d)) -> ((a, c), (b, d))
- ($:) :: (a -> b -> c) -> (a, b) -> c
Data Types
pattern First :: a -> (a, b) Source #
Matches the first element of a pair.
>>>
case ('x', 10) of First v -> v
'x'
pattern Second :: b -> (a, b) Source #
Matches the second element of a pair.
>>>
case ('x', 10) of Second v -> v
10
pattern (::|) :: Pair a -> [a] -> [a] Source #
Extracts the first two elements in a list as a pair.
>>>
case [1, 2, 3] of (p ::| _) -> p
(1,2)
Operations
uncurry :: (a -> b -> c) -> (a, b) -> c #
uncurry
converts a curried function to a function on pairs.
Examples
>>>
uncurry (+) (1,2)
3
>>>
uncurry ($) (show, 1)
"1"
>>>
map (uncurry max) [(1,2), (3,4), (6,8)]
[2,4,8]
bothM :: Applicative m => (a -> m b) -> Pair a -> m (Pair b) Source #
Apply an action to both components of a pair.
>>>
import Data.List.NonEmpty (nonEmpty)
>>>
bothM nonEmpty ([], [1])
Nothing
>>>
bothM nonEmpty ([1], [2])
Just (1 :| [],2 :| [])
zipM :: Applicative m => Pair (m a) -> m (Pair a) Source #
Extract a pair elements from a pair of actions.
>>>
import Data.Maybe (Maybe (..))
>>>
zipM (Just 1, Nothing)
Nothing
>>>
zipM (Just 1, Just 2)
Just (1,2)
>>>
import Data.Int (Int)
>>>
zipM @[] ([1, 2, 3], [4, 5])
[(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)]
zip :: [a] -> [b] -> [(a, b)] #
\(\mathcal{O}(\min(m,n))\). zip
takes two lists and returns a list of
corresponding pairs.
>>>
zip [1, 2] ['a', 'b']
[(1,'a'),(2,'b')]
If one input list is shorter than the other, excess elements of the longer list are discarded, even if one of the lists is infinite:
>>>
zip [1] ['a', 'b']
[(1,'a')]>>>
zip [1, 2] ['a']
[(1,'a')]>>>
zip [] [1..]
[]>>>
zip [1..] []
[]
zip
is right-lazy:
>>>
zip [] undefined
[]>>>
zip undefined []
*** Exception: Prelude.undefined ...
zip
is capable of list fusion, but it is restricted to its
first list argument and its resulting list.
cartesian :: [a] -> [b] -> [(a, b)] Source #
Cartesian product of the elements of two lists.
>>>
cartesian [1, 2, 3] "ab"
[(1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b')]
liftP :: (a -> b -> c) -> Pair a -> Pair b -> Pair c Source #
Apply a binary operator in both elements of a pair.
>>>
import GHC.Num ((+))
>>>
liftP (+) (10, 20) (3, 4)
(13,24)