Module functional.tuple

Tuple container.

An interned, immutable, nil-preserving tuple object.

Like Lua strings, tuples with the same elements can be quickly compared with a straight-forward == comparison.

The immutability guarantees only work if you don't change the contents of tables after adding them to a tuple. Don't do that!

Objects

Tuple Immutable Tuple container.

Metamethods

__call ([i=1[, j=self.n]]) Unpack tuple values between index i and j, inclusive.
prototype:__len () Return the length of this tuple.
prototype:__newindex (k, v) Prevent mutation of tup.
prototype:__tostring () Return a string representation of tup


Objects

Tuple
Immutable Tuple container.

Fields:

  • _type string object name (default "Tuple")
  • n int number of tuple elements

Usage:

     local Tuple = require "functional.tuple"
     function count (...)
       argtuple = Tuple (...)
       return argtuple.n
     end
     count () --> 0
     count (nil) --> 1
     count (false) --> 1
     count (false, nil, true, nil) --> 4

Metamethods

__call ([i=1[, j=self.n]])
Unpack tuple values between index i and j, inclusive.

Parameters:

  • i int first index to unpack (default 1)
  • j int last index to unpack (default self.n)

Returns:

    ... values at indices i through j, inclusive

Usage:

     tup = Tuple (1, 3, 2, 5)
     --> 3, 2, 5
     tup (2)
prototype:__len ()
Return the length of this tuple.

Returns:

    int number of elements in tup

Usage:

     -- Only works on Lua 5.2 or newer:
     #Tuple (nil, 2, nil) --> 3
     -- For compatibility with Lua 5.1, use functional.operator.len
     len (Tuple (nil, 2, nil)
prototype:__newindex (k, v)
Prevent mutation of tup.

Parameters:

  • k tuple key
  • v tuple value

Raises:

cannot change immutable tuple object
prototype:__tostring ()
Return a string representation of tup

Returns:

    string representation of tup

Usage:

     -- 'Tuple ("nil", nil, false)'
     print (Tuple ("nil", nil, false))
generated by LDoc 1.4.3 Last updated 2016-01-31 18:35:45