dwyl / learn-elixir

:droplet: Learn the Elixir programming language to build functional, fast, scalable and maintainable web applications!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What does 'enumerable' mean?

Cleop opened this issue · comments

commented

Lists are enumerable, What does enumerable mean?

Is the meaning of enumerable that you can refer to a value by numerical reference (by counting from left to right)?

Say I have a list: ["a", "b", "c"], the numerical reference/ index of a would be 0, b = 1, c = 2?

I've looked at dictionary definitions as well as this forum question: http://softwareengineering.stackexchange.com/questions/199592/what-does-enumerable-mean and can't tell if it's more complex than my original assumption.

commented

I don't think that my original understanding is correct because:

Tuples are not enumerable yet the README then goes on to illustrate an example of calling a tuple value by index which goes against my initial understanding for what could demonstrate an enumerable value.

Hello @Cleop, while you are waiting for other replies, and not pretending to know much about Elixir, but using some googlfu and general knowledge, I think the following discussion might prove helpful Tuples aren't Enumerable? Now, the key passage in the discussion for me is: "Tuples don't support the Enumerable protocol? They sure seem enumerable when you can ask elem {:a, :b, :c}, 1 and get :b."

So there seems to be two concepts that are perhaps being conflated within the concept of "enumerable", one of which is being able to be indexed, and the other is supporting the Enumerable protocol in Elixir. As you have seen, tuples do appear to be able to be indexed, so by elimination what is probably meant is that tuples do not support the Enumerable Protocol.

So, I think that "tuples are not enumerable" imply that tuples is not a type that supports the Enumerable protocol, which implies you cannot call certain functions on tuples, that you can call on those collection types that do support the protocol, such as lists.

Also useful in the discussion linked above is the following passage:

"Tuples also aren't meant to be iterated over, don't get confused by the
fact that you could using elem/2 and size/1. Tuples are for storing
multiple pieces of information together, which does not imply that they
are intended for storing a collection."

Anyways, that is my best guess, based on a little research. Hopefully, something is useful here for you, and if I have made some errors, others will come along to throw some light.

commented

Thanks @YvesMuyaBenda, seems like a potentially confusing area. I will try to clean up this area in the README so it is clearer.

commented

So to clarify:

Enumerable values at the most basic level are values that can be 'counted by one-to-one correspondence with the set of all positive integers'. In other words, values that can be referenced and obtained by index.

In Elixir both list and tuple values can be referenced by index.

A second feature of enumerable values is that the values can be iterated over ie. using functions like map or each. In Elixir these kinds of functions are found in the Enumerable Protocol, Enum and Stream modules. Lists can use both of these modules as outlined in the Enumerable Protocol. Whilst tuples may be considered 'enumerable' as you can refer to them by index, you may also not consider them enumerable because they are not included in the Enumerable Protocol and do not work with the Enum and Stream modules.

@Cleop Your writing is so smooth and concise for tech writing, with an excellent use of bold emphasis! What you wrote is my current understanding of the parts of the concept "enumerable" and how they relate. The key thing is that the discussion takes place within the specific context of Elixir, which you highlighted in your second paragraph, with the prepositional phrase "In Elixir". Good stuff!