Is there an existing function for applying a function to each member of a tuple?

I want to apply a function to both members of a homogeneous tuple, as a result we get another tuple. Following my previous question

+3
source share
1 answer

I don’t think there is any standard library function that does this.

My personal preference is to avoid too many user statements (they make the code shorter, but they make reading difficult for those who have not seen the definition before). Applying a function to both elements of a tuple is logically close to the operation with maps in lists (which applies the function to all elements of the list), so I would probably define Tuple2.map:

module Tuple2 = 
  let map f (a, b) = (f a, f b)

:

let nums = (1, 2)
nums |> Tuple2.map (fun x -> x + 1)
+5

All Articles