# Pair handling combinators

Arie Peterson ariep at xs4all.nl
Sun Oct 22 11:02:31 EDT 2006

```Wolfram wrote:

|  >
|  > Control.Combinators? Such general beasts as mapFst, mapSnd
|  > and >< from Data.Graph.Inductive.Query.Monad could go in
|  > there, as could other pair handling combinators like (f <&>
|  > g) x = (f x, g x)
|
| Pair handling combinators could go into Data.Tuple.

Many of those combinators can be expressed conveniently using functions
from Control.Arrow.

| > swap (x,y) = (y,x)

> swap = flip (,)
:p

| > pupd f g (x,y) = (f x, g y)

> pupd = (***)

| > mapPair f (x,y) = (f x, f y)

> mapPair = join (***)

| > mapTriple f (x,y,z) = (f x, f y, f z)

Not this one. I sometimes use nested pairs instead of triples to allow use
of the basic arrow combinators.

| > pupd1 f (x,y) = (f x, y)

> pupd1 = first

| > pupd2 g (x,y) = (x, g y)

> pupd2 = second

| > keep1 f p@(x,y) = (x, f p)

> keep1 = (fst &&&)

| > keep2 f p@(x,y) = (f p, y)

> keep2 = (&&& snd)

I'm not opposing introduction of these special tuple combinators, but it
may be good to reuse existing code.

Regards,

Arie

```