Language of Sequences in Programming Languages
In mathematics, a sequence is an enumerated collection of objects in which repetitions are allowed and order matters. Wikipedia
The mathematical concept of sequences naturally occurs in all useful programming languages. I wanted to review how different programming languages describe the concept.
Syntactically, it’s usually seen in two ways:
for
loops. They require:
- A starting statement
- An ending condition
- An “after iteration” statement
foreach
loops. They require:
- An object to loop through
Both:
- Take a sequence
- Perform the iteration
Usually, these concepts are described using the words:
- loop, enumerable, list, array, iterable, sequence, collection, vector
- loop, enumerate, iterate, go through
Which words should I use?
Word Usage by Programming Languages
Investigating further, I wanted to see how these concepts were described by popular programming languages and sources. Here are the quotes of select sources:
In the Python reference:
The
for
statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object:
In the Python tutorial:
The
for
statement in Python differs a bit from what you may be used to in C or Pascal. […] Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):
In the ECMAScript Specification:
Information about for
is under the header of
“iteration statements”.
In Rust by Example:
The for in construct can be used to iterate through an Iterator. One of the easiest ways to create an iterator is to use the range notation a…b. This yields values from a (inclusive) to b (exclusive) in steps of one.
In the Ruby documentation:
The
for
loop consists of for followed by a variable to contain the iteration argument followed by in and the value to iterate over using each. The do is optional:
In cppreference.com:
Conditionally executes a statement repeatedly, where the statement does not need to manage the loop condition.
In K&R,
Information about for
is under the header of “3.5
Loops - While and For”.
Chosen Words
I learned that iterate is used much more frequently than I previously thought. I also realized that I’ve underused the word sequence.
In the context of programming, I’ll share the words that I like the most.
When speaking about a sequence:
- Prefer sequence
- Prefer list, It’s quicker to say, but only if context already exists (word is overloaded in English)
- Avoid iterable unless talking about Python’s iterable object
- Avoid vector, array, iterable, or enumerable unless talking about the use of those things in a programming language
- Avoid loop unless talking about the syntax of iterating through lists in programming languages. The noun form is overloaded in English
When speaking about the action of iterating:
- Prefer iterate
- Prefer loop. It’s quicker to say, but only if context already exists (word is overloaded in English)
- Avoid enumerate and go through. They are vague