package tablecloth-native
Library
Module
Module type
Parameter
Class
Class type
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
Extract the first value from a tuple.
Tuple2.first (3, 4) = 3
Tuple2.first ("john", "doe") = "john"
Extract the second value from a tuple.
Tuple2.second (3, 4) = 4
Tuple2.second ("john", "doe") = "doe"
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)
Transform the second value in a tuple.
Tuple2.mapSecond ~f:sqrt ("stressed", 16.) = ("stressed", 4.)
Tuple2.mapSecond ~f:(~-) ("stressed", 16) = ("stressed", -16)
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)
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)
Switches the first and second values of a tuple.
Tuple2.swap (3, 4) = (4, 3)
Tuple2.swap ("stressed", 16) = (16, "stressed")
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]
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
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"]