Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
6

What are Tuples? [closed]

Asked by
Discern 1007 Moderation Voter
9 years ago

This could be considered as a follow-up to my last question on FilteringEnabled.

I have been looking at RemoteEvents/RemoteFunctions, and I think I get the basic concept of it.

One thing though, I have no clue what a Tuple is. I have looked at the wiki's page about Tuples, and I was confused.

Why do you need 3 periods? What do the periods represent? How do I use these?

And most of all, What do they even do?

Thanks in advance.

Locked by YellowoTide and BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
5
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

In less specific terms, "tuples" are a general extension of "pairs": ordered sequences of things with known length.


Tuples are a really weird and a really nice feature of Lua.

The simplest way that tuples show up is multiple assignment:

apple, banana = 5, 13

print(apple) -- 5
print(banana) -- 13

Lua matches up each thing on the left with a thing on the right.

This goes deeper than that though. We can return multiple things from a function:

function gets()
    return 7, 17
end

apple, banance = gets()
print(apple) -- 7
print(banana) -- 17

Though doing anything to the result will make it only keep the first one:

apple, banana = gets() + 1
print(apple) -- 8
print(banana) -- nil

We can use a few, too:

apple, banana, cat, dog = gets(), gets()
print(apple, banana, cat, dog)
-- 7, 7, 17, nil

Here, they start where the function started on the right, and the ones to the right "cover up" the ones to the left. Hence apple is the first result of the first gets(), banana is the first result of the second gets() and cat is the second result of the second gets().

We can also use these in definitions of lists:

local t = {1, gets()}
print(t[1], t[2], t[3])
-- 1, 7, 17

These are basically just conveniences. There is an interesting function: unpack.

local list = {4, 5, 6}
local x, a, b, c = 1, unpack(list)
print(x, a, b, c)
-- 1, 4, 5, 6

This essentially "un-does" the operation of putting tuples inside of a table. ie.

list = { unpack(other) }

Will have the same contents as list other.


Consider a function that adds two numbers:

function sum(a, b)
    return a + b
end

What if I want to add more than two numbers?

print( sum(1, sum(2, 3)) )
-- 6

This isn't very nice; what if we add a third parameter?

function sum(a, b, c)
    return a + b + c
end

print( sum(1, 2, 3) )
-- 6

print( sum(1, 2) )
-- error

So now we have to change this to support c being option:

function sum(a, b, c)
    return a + b + (c or 0)
end

What if we wanted to add 4 numbers? Now this starts to get messy. One option is that we instead take a list of numbers:

function sum(list)
    local s = 0
    for _, el in pairs(list) do
        s = s + el
    end
    return s
end

But now we can't say sum(1, 2), we have to say sum({1, 2}). What is the solution? Tuples!

function sum(...)
    local list = {...}
    for _, el in pairs(list) do
        s = s + el
    end
    return s
end

Basically, ... is a variable -- except it is a tuple, rather than only one value.

Thus list = {...} will make a list filled with the same arguments that were passed to sum!

For good measure, here's a way to use ... that doesn't involve immediately dumping it into a table:

-- Recursive sum!
function sum(first, ...)
    if not first then
        return 0 -- sum() == 0
    end
    return first + sum(...)
end
Ad
Log in to vote
0
Answered by 9 years ago

A Tuple is an ordered list of elements.

This would be considered a Tuple as it is a list of elements.

(1,2,3,4,5,6,"Cow") 

A function that takes a Tuple is a function that accepts multiple arguments.

Instance.new("Part",workspace)

This code takes the Tuple: `"Part",workspace'.

You can use ... to take an unknown amount of parameters into a function.

function Kill(...)
    local Player1,Player2,Player3,Player4 = ...
end

In the function above ... acts as a Tuple. So if I call the above function like this:

Kill("Bob","Joe",Frank)

The function would look like this:

function Kill(...)
    local Player1,Player2,Player3,Player4 = "Bob", "Joe",Frank
end

Player1, Player2, and Player3 would be assigned to "Bob", "Joe" and Frank"Player4' would be nil

Well what if I inputed more than 4 arguments into the above function? In the above example any additional arguments would be ignored unless they are assigned to a variable.

This can be fixed by using a table:

function Kill(...)
    local Players = {...}
end

Kill(Frank,Bob,Joe)

This would look like:

function Kill(...)
    local Players = {Frank,Bob,Joe}
end

Kill(Frank,Bob,Joe)