Tuples

Contains modules for working with tuples of different sizes.

Usage: either import the whole module and use the TupleN modules, or import the specific TupleN module directly.

import Tuples "mo:new-base/Tuples"; // imports all TupleN modules
import Nat "mo:new-base/Nat";
import Text "mo:new-base/Text";

Tuples.Tuple2.swap((1, "hello"));
Tuples.Tuple3.toText((1, "hello", 3), Nat.toText, Text.toText, Nat.toText);
import { Tuple2, Tuple3 } "mo:new-base/Tuples";
import Nat "mo:new-base/Nat";
import Text "mo:new-base/Text";

Tuple2.swap((1, "hello"));
Tuple3.toText((1, "hello", 3), Nat.toText, Text.toText, Nat.toText);

module Tuple2

public func swap<A, B>() : (B, A)

Swaps the elements of a tuple.

import Tuples "mo:new-base/Tuple";
assert Tuple.Tuple2.swap((1, "hello")) == ("hello", 1);

public func toText<A, B>(
  t : (A, B),
  toTextA : A -> Text,
  toTextB : B -> Text
) : Text

Creates a textual representation of a tuple for debugging purposes.

import Tuple "mo:new-base/Tuple";
import Nat "mo:new-base/Nat";
assert Tuple.toText((1, "hello"), Nat.toText, func (x: Text): Text = x) == "(1, hello)";

public func equal<A, B>(
  t1 : (A, B),
  t2 : (A, B),
  aEqual : (A, A) -> Bool,
  bEqual : (B, B) -> Bool
) : Bool

Compares two tuples for equality.

import Tuple "mo:new-base/Tuple";
import Nat "mo:new-base/Nat";
import Text "mo:new-base/Text";
assert Tuple.equal((1, "hello"), (1, "hello"), Nat.equal, Text.equal);

public func compare<A, B>(
  t1 : (A, B),
  t2 : (A, B),
  aCompare : (A, A) -> Types.Order,
  bCompare : (B, B) -> Types.Order
) : Types.Order

Compares two tuples lexicographically.

import Tuple "mo:new-base/Tuple";
import Nat "mo:new-base/Nat";
import Text "mo:new-base/Text";
assert Tuple.compare((1, "hello"), (1, "world"), Nat.compare, Text.compare) == #less;
assert Tuple.compare((1, "hello"), (2, "hello"), Nat.compare, Text.compare) == #less;
assert Tuple.compare((1, "hello"), (1, "hello"), Nat.compare, Text.compare) == #equal;
assert Tuple.compare((2, "hello"), (1, "hello"), Nat.compare, Text.compare) == #greater;
assert Tuple.compare((1, "world"), (1, "hello"), Nat.compare, Text.compare) == #greater;

public func makeToText<A, B>(toTextA : A -> Text, toTextB : B -> Text) : ((A, B)) -> Text

Creates a toText function for a tuple given toText functions for its elements.

public func makeEqual<A, B>(aEqual : (A, A) -> Bool, bEqual : (B, B) -> Bool) : ((A, B), (A, B)) -> Bool

Creates an equal function for a tuple given equal functions for its elements.

public func makeCompare<A, B>(aCompare : (A, A) -> Types.Order, bCompare : (B, B) -> Types.Order) : ((A, B), (A, B)) -> Types.Order

Creates a compare function for a tuple given compare functions for its elements.

module Tuple3

public func toText<A, B, C>(
  t : (A, B, C),
  toTextA : A -> Text,
  toTextB : B -> Text,
  toTextC : C -> Text
) : Text

Creates a textual representation of a 3-tuple for debugging purposes.

public func equal<A, B, C>(
  t1 : (A, B, C),
  t2 : (A, B, C),
  aEqual : (A, A) -> Bool,
  bEqual : (B, B) -> Bool,
  cEqual : (C, C) -> Bool
) : Bool

Compares two 3-tuples for equality.

public func compare<A, B, C>(
  t1 : (A, B, C),
  t2 : (A, B, C),
  aCompare : (A, A) -> Types.Order,
  bCompare : (B, B) -> Types.Order,
  cCompare : (C, C) -> Types.Order
) : Types.Order

Compares two 3-tuples lexicographically.

public func makeToText<A, B, C>(
  toTextA : A -> Text,
  toTextB : B -> Text,
  toTextC : C -> Text
) : ((A, B, C)) -> Text

Creates a toText function for a 3-tuple given toText functions for its elements.

public func makeEqual<A, B, C>(
  aEqual : (A, A) -> Bool,
  bEqual : (B, B) -> Bool,
  cEqual : (C, C) -> Bool
) : ((A, B, C), (A, B, C)) -> Bool

Creates an equal function for a 3-tuple given equal functions for its elements.

public func makeCompare<A, B, C>(
  aCompare : (A, A) -> Types.Order,
  bCompare : (B, B) -> Types.Order,
  cCompare : (C, C) -> Types.Order
) : ((A, B, C), (A, B, C)) -> Types.Order

Creates a compare function for a 3-tuple given compare functions for its elements.

module Tuple4

public func toText<A, B, C, D>(
  t : (A, B, C, D),
  toTextA : A -> Text,
  toTextB : B -> Text,
  toTextC : C -> Text,
  toTextD : D -> Text
) : Text

Creates a textual representation of a 4-tuple for debugging purposes.

public func equal<A, B, C, D>(
  t1 : (A, B, C, D),
  t2 : (A, B, C, D),
  aEqual : (A, A) -> Bool,
  bEqual : (B, B) -> Bool,
  cEqual : (C, C) -> Bool,
  dEqual : (D, D) -> Bool
) : Bool

Compares two 4-tuples for equality.

public func compare<A, B, C, D>(
  t1 : (A, B, C, D),
  t2 : (A, B, C, D),
  aCompare : (A, A) -> Types.Order,
  bCompare : (B, B) -> Types.Order,
  cCompare : (C, C) -> Types.Order,
  dCompare : (D, D) -> Types.Order
) : Types.Order

Compares two 4-tuples lexicographically.

public func makeToText<A, B, C, D>(
  toTextA : A -> Text,
  toTextB : B -> Text,
  toTextC : C -> Text,
  toTextD : D -> Text
) : ((A, B, C, D)) -> Text

Creates a toText function for a 4-tuple given toText functions for its elements.

public func makeEqual<A, B, C, D>(
  aEqual : (A, A) -> Bool,
  bEqual : (B, B) -> Bool,
  cEqual : (C, C) -> Bool,
  dEqual : (D, D) -> Bool
) : ((A, B, C, D), (A, B, C, D)) -> Bool

Creates an equal function for a 4-tuple given equal functions for its elements.

public func makeCompare<A, B, C, D>(
  aCompare : (A, A) -> Types.Order,
  bCompare : (B, B) -> Types.Order,
  cCompare : (C, C) -> Types.Order,
  dCompare : (D, D) -> Types.Order
) : ((A, B, C, D), (A, B, C, D)) -> Types.Order

Creates a compare function for a 4-tuple given compare functions for its elements.