I know roughly 20 of the easiest cards like the back of my hand now, I've committed them to memory and I'm starting to wonder about how they fit into the bigger picture that is elixir. One GAME 2 question is:

Use a function to determine the length of a tuple

I know that I can use the tuple_size function for that, but what is a tuple used for and why is determining it's length part of the fundamentals? I have purposefully focussed on learning this set of cards to add known entities of code to my tool belt, now I am curious on how to apply them. Thinking ahead to building a pack of Ruby cards, it is sinking in just how important the content is for the beginner decks are. My known Elixir world is very small right now but it is up to me to expand it, so when I get curious I'm going to follow that thread and document what I learn. Time to take my head out of the clouds and get back to tuples...

The key question is: What is a tuple and why would I want to use it? It looks like a Ruby hash to me because of the curly braces around it, and the items seemed to be associated in pairs, but unlike in Ruby the pairs are not pointing to each other in a key and value format.

After some digging I found this wonderful explanation by Alexei Sholik, being quoted on a stackoverflow answer:

[...] tuples are poor man's records. In other words, a tuple represents a single piece of data, a single value, albeit aggregate. You cannot take away an element from a tuple without changing the semantic meaning of that particular tuple value.

This is contrary to lists and other collections that store many [...] independent values. Taking a value away from list simply reduces the length of the list. It doesn't affect semantic meaning of anything.

This doesn't mention the key and value structure of a Ruby hash but tuples seem to serve a similar use, representing a piece of data, not a collection. To complete the picture we need to find tuples in action and application. Onward to GitHub!

deps: [

  {:ok, "0.1.0", path: "deps/ok"},

  {:unknown, "0.1.0", git: "deps/unknown"}


Here we have a subset of an array describing a project, and the tuples are storing a status in the first position, which seems to be an Elixir idiom, and then other information after. I was shocked to see key value syntax path: "deps/ok", but evaluating this in a REPL turns them into an array:

> { :ok, path: "here.pdf", foot_path: "there.pdf" }

{:ok, [path: "here.pdf", foot_path: "there.pdf"]}

Going on a hunch I throw in an atom as the last item in the tuple:

> { :ok, path: "here.pdf", foot_path: "there.pdf", :stones }

** (SyntaxError) iex:22: syntax error before: stones

Kaboom! I was expecting this. In Ruby the keyword or hash arguments always come at the end, so switching the position of :stones to precede these pairs will make this a valid tuple:

> { :ok, :stones, path: "here.pdf", foot_path: "there.pdf" }

{:ok, :stones, [path: "here.pdf", foot_path: "there.pdf"]}

Whoop! So that must mean that this is syntactic sugar and I could use an array in a middle position if I really wanted to:

> { :ok, [path: "here.pdf", foot_path: "there.pdf"], :stones}

{:ok, [path: "here.pdf", foot_path: "there.pdf"], :stones}

Bingo! I feel like I have a grasp on these tuple thingys, but this exercise has emphasised that I need to think more on contextualising my knowledge as I am building it.

Now for the moment of truth. What will tuple_size return when we use the syntactic sugar for those arrays:

> tuple_size { :ok, :stones, path: "here.pdf", foot_path: "there.pdf" }


Doesn't that look a bit weird? I know why the answer is 3, as I'm sure you do now as well, but visually this could catch someone out.

Who knew a dive into the tuple_size function was going to be so interesting! I am amazed that there is still half the deck of Elixir flashcards that I have not looked at since day 1, because I am following the threads of my own curiosity just on the "easy" cards. Staying motivated is much easier when the learning is delight driven.

Day 6 looks at reimplementing a couple of elixir functions.

< Previous