Last active
December 2, 2022 14:11
-
-
Save ploeh/6d8050e121a5175fabb1d08ef5266cd7 to your computer and use it in GitHub Desktop.
Helpful functions for working with pairs in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
I got the extend functions from Jérémie Chassaing: https://twitter.com/thinkb4coding/status/775427149243224064
This one is also useful if a
and b
are of the same type:
let map f (a, b) = f a, f b
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
Can you add an example of where the extend functions are really useful? The rest I've already got lots of places I can think of where I would have used them.