Many languages seem to have structures that are very similar called iterable or enumerable. These are structures that can be iterated or enumerated over, which seem to me extremely similar things. Are these words synonymous or is there a subtle semantic difference between iterable and enumerable which justifies the choice of term?
4 Answers
IMHO this depends on the context, sometimes they are synonymous, sometimes they are not. For example, in C# you have a data type "IEnumerable", which classifies iterators, but you have also the "enum" declaration which is for symbolic constants, not specificially for iterations. In other programming languages (or other contexts) the situation may be similar or not.
If you mean the two words just as english verbs and not specifically as programming language keywords, then
- iterating means to "loop over all elements of a set, one by one"
- enumerating means to "give each element of a set an ordinal number, one by one"
And since iterating is needed for enumerating, and enumerating implies some kind of iteration, those two process descriptions can typically be interchanged.
-
3+1 why do you mention enums though? The enum as a structure would be ortoghonal to enumarability? Though I guess you could say that the nature of an enum would be "giving each constant in a set an ordinal number, one by one"... :) Commented Nov 9, 2013 at 16:40
-
@MarjanVenema: well, the keyword "enum" comes from enum- erate, I guess? Commented Nov 9, 2013 at 16:55
-
D'oh, ya, I'd say so. I got my chicken and egg the wrong way round :-) Commented Nov 9, 2013 at 17:24
-
3
-
3
As others have said, the precise semantics depend on the programming language that uses the terms, so I'll give a purely linguistic appraisal.
"Iterable" is a rather new-coined word, obviously referring to "iterating" and the "iterators" that many languages have built in. Therefore, such types almost certainly support an iterator, but not necessarily anything else, and not necessarily anything beyond the most basic iterator functionality: processing every item once. Reversing, deleting, measuring distances etc. may or may not be supported.
"Enumerable" refers to enumerating things, which can mean the same as iterating, but only if the language doesn't already use "iterable" for that purpose. If a language has both, "enumerable" almost certainly means something else, probably something more powerful. Most likely, it will support the notion of connecting each element with a unique numeric index, and probably it will allow random access (e.g. retrieving the third element before the first and second ones).
That's about all the meaning you can reasonably infer from just the words. In any concrete situation, refer to the standard library API docs.
I want to focus on the strict definition of both terms
Iterable
is to iterate things, and get access to the element one by one.
I think that the term Enumerable
was originated from the turning machine. It's about the ability to list out the element one by one in a proper order. The things can be listed out one by one has to be countable
, each one has a unique correspondence index. Given an element, you can get a unique index. Given an index, you can only find one possible element associated with that index.
In other words, Enumerable
implies the ability to generate the elements . Some programming language, for example, Haskell
has implemented this idea. There is a Enum type class, and Char
is one the instance.
Prelude> fromEnum True
1
Prelude> fromEnum False
0
Prelude> toEnum 1 :: Bool
True
Prelude> fromEnum 'a'
97
Prelude> enumFromTo 'a' (toEnum 122 :: Char)
"abcdefghijklmnopqrstuvwxyz"
enumeration is counting, iteration is a replaying
a second enumeration will yield the same number, an iteration may be a variation
fractals are built by iterations of a function, repeating the function on the result of the last iteration, each iteration has a different value