Skip to content

Instantly share code, notes, and snippets.

@ploeh
Last active December 2, 2022 14:11
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ploeh/6d8050e121a5175fabb1d08ef5266cd7 to your computer and use it in GitHub Desktop.
Save ploeh/6d8050e121a5175fabb1d08ef5266cd7 to your computer and use it in GitHub Desktop.
Helpful functions for working with pairs in F#
module Tuple2
let replicate x = x, x
let curry f x y = f (x, y)
let uncurry f (x, y) = f x y
let swap (x, y) = (y, x)
let mapFst f (x, y) = f x, y
let mapSnd f (x, y) = x, f y
let extendFst f (x,y) = f (x,y), y
let extendSnd f (x,y) = x, f(x,y)
let optionOfFst f (x, y) =
match f x with
| Some x' -> Some (x', y)
| None -> None
let optionOfSnd f (x, y) =
match f y with
| Some y' -> Some (x, y')
| None -> None
@brianberns
Copy link

This one is also useful if a and b are of the same type:

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

@ImaginaryDevelopment
Copy link

I typically call that one mapBoth @brianberns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment