Tuples.
NOTE: API changes are expected in a future release.
Modules are provided for tuples with 2, 3, 4, and 5 elements. Each provides the following categories of functions.
Creation. Functions make
take n
arguments and build a n
-tuple.
Projection. Functions first
, second
, third
, fourth
, and fifth
extract a single element. Also, multiple elements can be extracted. For example, Tuple3.get13
returns the first and third elements of a 3-tuple. All possible combinations are provided.
Note there are no get
functions in Tuple2 because first
and second
already cover all possibilities. However, swap
is provided, which can be thought of as projecting items in a different order.
Mapping. Apply a function to one or all elements of a tuple. Functions map1
, map2
, etc. map a given function to the first, second, etc. element of a tuple. All elements can be mapped using map
or mapn
. For example, Tuple3.map
f g h
will apply f
, g
, and h
to the three elements, respectively, of a 3-tuple. Function mapn
is similar but applies the same function to all elements, which thus requires the elements to be of the same type.
Currying. Every tuple has a curry
and uncurry
function, which allow converting between functions that take n
arguments to ones that take a single n
-tuple argument.
Enumeration. Every n
-tuple can be converted to an enum with n
elements using its enum
function, and can be constructed from an enum using of_enum
. Tuples satisfy BatEnum.Enumerable
.
Printing. Function print
prints a tuple given a method for printing each of its elements. The simpler printn
function can be used when all elements are of the same type.
Comparison. Every tuple has a compare
function, which can optionally be customized by specifying methods for comparing each element. Pervasives.compare
is used by default.
Pairs. Some of the functions here are also exposed in Pervasives
, as documented below.