tuple.h

Tuples: (x, y, z).

A tuple is represented as (x1, ..., xN). Tuples are a convenient way to deal with product types. For example:

[examples/rectangle.c]

// Computes the area of a rectangle.

#include <metalang99.h>

#define rect(width, height) ML99_tuple(width, height)
#define rectWidth           ML99_tupleGet(0)
#define rectHeight          ML99_tupleGet(1)

#define rectArea(rect) ML99_mul(rectWidth(rect), rectHeight(rect))

/*
 *                15
 * +------------------------------+
 * |                              |
 * |                              |
 * |                              | 7
 * |                              |
 * |                              |
 * +------------------------------+
 */
#define RECTANGLE rect(v(15), v(7))

ML99_ASSERT_EQ(rectArea(RECTANGLE), v(15 * 7));

int main(void) {}

Note

Tuples are more time and space-efficient than lists, but export less functionality; if a needed function is missed, invoking ML99_list and then manipulating with the resulting Cons-list might be helpful.

Defines

ML99_tuple(...)

Transforms a sequence of arguments into (...).

Examples

#include <metalang99/tuple.h>

// (1, 2, 3)
ML99_tuple(v(1, 2, 3))

ML99_tupleEval(...)

Transforms a sequence of arguments into (v(...)).

Examples

#include <metalang99/tuple.h>

// (v(1, 2, 3))
ML99_tupleEval(v(1, 2, 3))

Deprecated:

I have seen no single use case over time. Please, open an issue if you need this function.

ML99_untuple(x)

Untuples the tuple x, leaving the result unevaluated.

If x is not a tuple, it emits a fatal error.

Examples

#include <metalang99/tuple.h>

// 1, 2, 3
ML99_untuple(v((1, 2, 3)))

ML99_untupleChecked(x)

The same as ML99_untuple.

Deprecated:

Use ML99_untuple instead.

ML99_untupleEval(x)

Untuples the tuple x and evaluates the result.

Examples

#include <metalang99/tuple.h>

// 1, 2, 3
ML99_untupleEval(v((v(1, 2, 3))))

Deprecated:

For the same reason as ML99_tupleEval.

ML99_isTuple(x)

Tests whether x is inside parentheses or not.

The preconditions are the same as of ML99_isUntuple.

Examples

#include <metalang99/tuple.h>

// 0
ML99_isTuple(v(123))

// 1
ML99_isTuple(v((123)))

ML99_isUntuple(x)

The inverse of ML99_isTuple.

x must be either of these forms:

  • (...) (reported as non-untupled)

  • (...) (...) ... (reported as untupled)

  • anything else not beginning with (...) (reported as untupled)

For example (respectively):

  • (~, ~, ~) (non-untupled)

  • (~, ~, ~) (~, ~, ~) or (~, ~, ~) (~, ~, ~) abc (untupled)

  • 123 or 123 (~, ~, ~) (untupled)

Examples

#include <metalang99/tuple.h>

// 1
ML99_isUntuple(v(123))

// 0
ML99_isUntuple(v((123)))

// 1
ML99_isUntuple(v((123) (456) (789)))

ML99_tupleCount(x)

Computes the count of items in the tuple x.

At most 63 items can be contained in x.

Examples

#include <metalang99/tuple.h>

// 3
ML99_tupleCount(v((~, ~, ~)))

// 1
ML99_tupleCount(v(()))

ML99_tupleIsSingle(x)

Tells if the tuple x contains only one item or not.

Examples

#include <metalang99/tuple.h>

// 1
ML99_tupleIsSingle(v((~)))

// 0
ML99_tupleIsSingle(v((~, ~, ~)))

ML99_tupleGet(i)

Expands to a metafunction extracting the i -indexed element of a tuple.

i can range from 0 to 7, inclusively.

Examples

#include <metalang99/tuple.h>

// 2
ML99_tupleGet(1)(v((1, 2, 3)))

ML99_tupleTail(x)

Extracts the tuple’s tail.

x must contain at least two elements.

Examples

#include <metalang99/tuple.h>

// 2, 3
ML99_tupleTail(v((1, 2, 3)))

ML99_tupleAppend(x, ...)

Appends provided variadic arguments to the tuple x.

Examples

#include <metalang99/tuple.h>

// (1, 2, 3)
ML99_tupleAppend(ML99_tuple(v(1)), v(2, 3))

ML99_tuplePrepend(x, ...)

Prepends provided variadic arguments to the tuple x.

Examples

#include <metalang99/tuple.h>

// (1, 2, 3)
ML99_tuplePrepend(ML99_tuple(v(3)), v(1, 2))

ML99_tupleForEach(f, x)

A shortcut for ML99_variadicsForEach(f, ML99_untuple(x)).

ML99_tupleForEachI(f, x)

A shortcut for ML99_variadicsForEachI(f, ML99_untuple(x)).

ML99_assertIsTuple(x)

Emits a fatal error if x is not a tuple, otherwise results in emptiness.

Examples

#include <metalang99/tuple.h>

#define F_IMPL(x) ML99_TERMS(ML99_assertIsTuple(v(x)), ML99_untuple(v(x)))

// 1, 2, 3
ML99_call(F, v((1, 2, 3)))

// A compile-time tuple mismatch error.
ML99_call(F, v(123))

ML99_TUPLE(...)
ML99_UNTUPLE(x)
ML99_IS_TUPLE(x)
ML99_IS_UNTUPLE(x)
ML99_TUPLE_COUNT(x)
ML99_TUPLE_IS_SINGLE(x)
ML99_TUPLE_GET(i)
ML99_TUPLE_TAIL(x)
ML99_TUPLE_APPEND(x, ...)
ML99_TUPLE_PREPEND(x, ...)