66

In Haskell, I can easily map a list:

map (\x -> 2*x) [1,2]

gives me [2,4]. Is there any "mapTuple" function which would work like that?

mapTuple (\x -> 2*x) (1,2)

with the result being (2,4).

quant_dev
  • 5,983
  • 1
  • 31
  • 53

13 Answers13

86

Here's a rather short point-free solution:

import Control.Monad (join)
import Control.Arrow ((***))

mapTuple = join (***)
Harrison Grodin
  • 1,967
  • 2
  • 17
  • 27
Rotsor
  • 13,009
  • 5
  • 40
  • 57
  • What is the effect of join when used on functions? – Riccardo T. Mar 15 '12 at 16:53
  • 14
    @Riccardo - `join` takes a function of two arguments with the same type, `a->a->b`, and creates a new function with one argument `a -> b`, passing that argument to both positions of the original function. This is because the Monad instance for functions is identical to the `Reader` monad, giving `join` type `(a -> a -> b) -> a -> b`. I find it a little easier to work out when the arrows aren't written infix, e.g. `join :: (a ->) ( (a ->) b) -> (a ->) b`. – John L Mar 15 '12 at 17:01
49

Searching at Hoogle gives no exact matches for (a -> b) -> (a, a) -> (b, b), which is the type you require, but it is pretty easy to do yourself:

mapTuple :: (a -> b) -> (a, a) -> (b, b)
mapTuple f (a1, a2) = (f a1, f a2)

Note, you will have to define a new function for 3-tuples, 4-tuples etc - although such a need might be a sign, that you are not using tuples like they were intended: In general, tuples hold values of different types, so wanting to apply a single function to all values is not very common.

Boris
  • 4,884
  • 4
  • 41
  • 71
  • 8
    `Control.Arrow` *is* in the standard libs, and `Control.Bifunctor` istn't too far away... – Landei Mar 15 '12 at 15:33
  • @Landei: Yes, but the OP asked about mapping a single function over a tuple. – Boris Mar 15 '12 at 15:44
  • 1
    @Landei: I removed the mention of the standard libraries, since I don't know much about which modules are in them, or are going to be soon, and just mentioned hoogle, which was where I searched. – Boris Mar 15 '12 at 15:53
  • 3
    You can always use the Applicative trick to feed an argument twice in a function: `(bimap id id) (*2) (3,5)` – Landei Mar 16 '12 at 08:02
31

You could use Bifunctor:

import Control.Monad  (join)
import Data.Bifunctor (bimap)

join bimap (2*) (1,2)

This works not only for pairs, but for a number of other types as well, e.g. for Either.

Bifunctor is in base as of version 4.8. Previously it was provided by the bifunctors package.

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
Landei
  • 52,346
  • 12
  • 89
  • 188
  • The `join` trick for the `(->)` monad instance is awesome. Thanks! – Profpatsch Nov 15 '16 at 12:16
  • Nice! That beats what I found in another context, where I just wanted to modify the second value, i.e. import`Data.Graph.Inductive.Query.Monad` and then `(*2) >< (*2)` is the function you're after. (I'm puzzled that this isn't in Data.Tuple, but perhaps that's just an accident of history.) – James Jones Feb 27 '17 at 17:58
23

You can use arrows from module Control.Arrow to compose functions that work on tuples.

Prelude Control.Arrow> let f = (*2) *** (*2)
Prelude Control.Arrow> f (1,2)
(2,4)
Prelude Control.Arrow> let f' = (*2) *** (*3)
Prelude Control.Arrow> f (2,2)
(4,4)
Prelude Control.Arrow> f' (2,2)
(4,6)

Your mapTuple then becomes

mapTuple f = f *** f

If with your question you asked for a function that maps over tuples of arbitrary arity, then I'm afraid you can't because they would have different types (e.g. the tuple types (a,b) and (a,b,c) are totally different and unrelated).

Riccardo T.
  • 8,569
  • 5
  • 32
  • 74
22

You can also use lens to map tuples:

import Control.Lens
mapPair = over both

Or you can map over tuples with upto 10 elements:

mapNtuple f = traverseOf each (return . f)
Sebastian Wagner
  • 1,655
  • 2
  • 16
  • 24
  • 4
    `over each` seems to work too: `(over each) (+1) (1,2,3,4,5,6,7,8,9) == (2,3,4,5,6,7,8,9,10)` – Wizek Dec 16 '16 at 16:55
12

Here is another way:

mapPair :: (a -> b) -> (a, a) -> (b, b) -- this is the inferred type
mapPair f = uncurry ((,) `on` f)

You need Data.Function imported for on function.

ciuncan
  • 992
  • 2
  • 10
  • 23
12

To add another solution to this colourful set... You can also map over arbitrary n-tuples using Scrap-Your-Boilerplate generic programming. For example:

import Data.Data
import Data.Generics.Aliases

double :: Int -> Int
double = (*2)

tuple :: (Int, Int, Int, Int)
tuple = gmapT (mkT double) (1,2,3,4)

Note that the explicit type annotations are important, as SYB selects the fields by type. If one makes one tuple element type Float, for example, it wouldn't be doubled anymore.

Ry-
  • 199,309
  • 51
  • 404
  • 420
Peter Wortmann
  • 2,234
  • 13
  • 14
  • 1
    I understand vaguely that a lot of the original SYB libraries have been replaced by GHC Generics with some support in base/compiler. (But maybe you and that wikipedia page use "SYB" ad a term for generics in general, and not just the specific original SYB papers... ) – misterbee Mar 22 '12 at 05:53
  • 3
    I might be wrong, but given that GHC's [`Data.Data` documentation](http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Data.html) cites SYB, I'd wager that it's a direct descendant. – Peter Wortmann Mar 22 '12 at 11:43
9

The extra package provides the both function in the Data.Tuple.Extra module. From the docs:

Apply a single function to both components of a pair.

> both succ (1,2) == (2,3)

both :: (a -> b) -> (a, a) -> (b, b)
Neil Mitchell
  • 8,573
  • 1
  • 24
  • 76
9

Yes, for tuples of 2 items, you can use first and second to map the contents of a tuple (Don't worry about the type signature; a b c can be read as b -> c in this situation). For larger tuples, you should consider using a data structure and lenses instead.

dflemstr
  • 25,186
  • 5
  • 66
  • 102
6

You can also use Applicatives which have additional benefit of giving you possibility to apply different functions for each tuple element:

import Control.Applicative

mapTuple :: (a -> a') -> (b -> b') -> (a, b) -> (a', b')
mapTuple f g = (,) <$>  f . fst <*> g . snd

Inline version:

(\f -> (,) <$>  f . fst <*> f . snd) (*2) (3, 4)

or with different map functions and without lambda:

(,) <$> (*2) . fst <*> (*7) . snd $ (3, 4)

Other possibility would be to use Arrows:

import Control.Arrow

(+2) . fst &&& (+2) . snd $ (2, 3)
Piotr Kukielka
  • 3,622
  • 3
  • 29
  • 40
4

I just added a package tuples-homogenous-h98 to Hackage that solves this problem. It adds newtype wrappers for tuples and defines Functor, Applicative, Foldable and Traversable instances for them. Using the package you can do things like:

untuple2 . fmap (2 *) . Tuple2 $ (1, 2)

or zip tuples like:

Tuple2 ((+ 1), (*2)) <*> Tuple2 (1, 10)
BenJacob
  • 827
  • 8
  • 25
Petr
  • 60,177
  • 8
  • 136
  • 295
4

The uniplate package provides the descend function in the Data.Generics.Uniplate.Data module. This function will apply the function everywhere the types match, so can be applied to lists, tuples, Either, or most other data types. Some examples:

descend (\x -> 2*x) (1,2) == (2,4)
descend (\x -> 2*x) (1,"test",Just 2) == (2,"test",Just 4)
descend (\x -> 2*x) (1,2,3,4,5) == (2,4,6,8,10)
descend (\x -> 2*x) [1,2,3,4,5] == [2,4,6,8,10]
Neil Mitchell
  • 8,573
  • 1
  • 24
  • 76
3

Yes, you would do:

map (\x -> (fst x *2, snd x *2)) [(1,2)]

fst grabs the first data entry in a tuple, and snd grabs the second; so, the line of code says "take a tuple, and return another tuple with the first and second items double the previous."

Riccardo T.
  • 8,569
  • 5
  • 32
  • 74
Marshall Conover
  • 765
  • 4
  • 22
  • 1
    Your code does not work, but as a side note remember that you can pattern match at the arguments to a lambda function: `\(x,y) -> (x*2,y*2)` – danr Mar 15 '12 at 15:20
  • @danr - I was missing a parenthesis, thanks for pointing it out :P. I wasn't aware of pattern matching though, thanks! – Marshall Conover Mar 15 '12 at 15:23
  • 2
    I don't see any reason to use `map` on a list with a single element: the point here is the function in the parentheses in fact. – awllower Aug 24 '16 at 07:51