mcsp-algorithms-0.1.0: Algorithms for Minimum Common String Partition (MCSP) in Haskell.
Safe HaskellSafe-Inferred
LanguageGHC2021

MCSP.Data.Pair

Description

Operations for working with a pair of elements of the same type.

Synopsis

Data Types

type Pair a = (a, a) Source #

A pair of elements of the same type a.

pattern Pair :: a -> a -> Pair a Source #

A pair of elements of the same type a.

left :: Pair a -> a Source #

right :: Pair a -> a Source #

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

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

Extract the first component of a pair.

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]

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

Extract the second component of a pair.

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

Swap the components of a pair.

(***) :: (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) 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.

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

The unzip function is the inverse of the zip function.

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)

transpose :: ((a, b), (c, d)) -> ((a, c), (b, d)) Source #

Transpose elements in a pair of pairs like in a square matrix.

>>> transpose (('a', 1), ('b', 2))
(('a','b'),(1,2))

($:) :: (a -> b -> c) -> (a, b) -> c infixr 4 Source #

Spread a pair of values as arguments to a function.

Infix version of uncurry.

>>> import GHC.Num ((+))
>>> f x y = x + y
>>> f $: (1, 2)
3