Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

Haskell: map one element of a tuple

I'm new to Haskell, and wondering what would be an idiomatic way to convert a tuple (a,b) to (c,b). In other words, only transform the first value, leaving the second unchanged.

I can, of course, use fst and snd, but if I can map over lists, why not tuples?

Upvotes: 1

Views: 1790

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152707

Any of these will do:

base:Data.Bifunctor.first
base:Control.Arrow.first
extra:Data.Tuple.Extra.first
(lens:Control.Lens._1 lens:Control.Lens.%~)

And there are many more.

Upvotes: 9

Related Questions