package tablecloth-native

  1. Overview
  2. Docs
val create : 'a -> 'b -> 'a * 'b

Create a 2-tuple.

Tuple2.create 3 4 = (3, 4)

let zip (xs : 'a list) (ys : 'b list) : ('a * 'b) list = List.map2 ~f:Tuple2.create xs ys

val first : ('a * 'b) -> 'a

Extract the first value from a tuple.

Tuple2.first (3, 4) = 3

Tuple2.first ("john", "doe") = "john"

val second : ('a * 'b) -> 'b

Extract the second value from a tuple.

Tuple2.second (3, 4) = 4

Tuple2.second ("john", "doe") = "doe"

val mapFirst : f:('a -> 'x) -> ('a * 'b) -> 'x * 'b

Transform the first value in a tuple.

Tuple2.mapFirst ~f:String.reverse ("stressed", 16) = ("desserts", 16)

Tuple2.mapFirst ~f:String.length ("stressed", 16) = (8, 16)

val map_first : f:('a -> 'x) -> ('a * 'b) -> 'x * 'b
val mapSecond : f:('b -> 'y) -> ('a * 'b) -> 'a * 'y

Transform the second value in a tuple.

Tuple2.mapSecond ~f:sqrt ("stressed", 16.) = ("stressed", 4.)

Tuple2.mapSecond ~f:(~-) ("stressed", 16) = ("stressed", -16)

val map_second : f:('b -> 'y) -> ('a * 'b) -> 'a * 'y
val mapEach : f:('a -> 'x) -> g:('b -> 'y) -> ('a * 'b) -> 'x * 'y

Transform each value of a tuple.

Tuple2.mapEach ~f:String.reverse ~g:sqrt ("stressed", 16.) = ("desserts", 4.)

Tuple2.mapEach ~f:String.length ~g:(~-) ("stressed", 16) = (8, -16)

val map_each : f:('a -> 'x) -> g:('b -> 'y) -> ('a * 'b) -> 'x * 'y
val mapAll : f:('a -> 'b) -> ('a * 'a) -> 'b * 'b

Transform all the values of a tuple using the same function. mapAll can only be used on tuples which have the same type for each value.

Tuple2.mapAll ~f:succ (3, 4, 5) = (4, 5, 6)

Tuple2.mapAll ~f:String.length ("was", "stressed") = (3, 8)

val map_all : f:('a -> 'b) -> ('a * 'a) -> 'b * 'b
val swap : ('a * 'b) -> 'b * 'a

Switches the first and second values of a tuple.

Tuple2.swap (3, 4) = (4, 3)

Tuple2.swap ("stressed", 16) = (16, "stressed")

val curry : (('a * 'b) -> 'c) -> 'a -> 'b -> 'c

curry f takes a function f which takes a single argument of a tuple 'a * 'b and returns a function which takes two arguments that can be partially applied.

let squareArea (width, height) = width * height

let curriedArea : float -> float -> float = curry squareArea

let heights = [3, 4, 5]

List.map widths ~f:(curriedArea 4) = [12; 16; 20]

val uncurry : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c

uncurry f takes a function f which takes two arguments and returns a function which takes a single argument of a 2-tuple

let sum (a : int) (b: int) : int = a + b

let uncurriedSum : (int * int) -> int = uncurry add

uncurriedSum (3, 4) = 7

val toList : ('a * 'a) -> 'a list

Turns a tuple into a list of length two. This function can only be used on tuples which have the same type for each value.

Tuple2.toList (3, 4) = [3; 4]

Tuple2.toList ("was", "stressed") = ["was"; "stressed"]

val to_list : ('a * 'a) -> 'a list