38

A container having 2 or more elements could be called a "plural container". A container with no elements could be called an "empty container".

But what is the terminology for a container whose current size is exactly 1 element?

I would use the term "unary" but that refers to operators that take exactly 1 argument.

The reason I would like a term for this is that I am writing a boolean method to check if two data structures are both of size exactly one and I want a good name for the method. Currently I have:

def length_is_1(self, x, y):
    return len(x) == 1 and len(y) == 1
22
  • 3
    degenerate container? Commented Aug 25, 2020 at 14:42
  • 1
    If it were only designed to hold one element or none, you could use the word full. But if you have a list that is capable of holding zero or many, you can describe the item itself as "lone", but I'm not sure there is a specific word that describes the status of a general purpose container as "currently containing only one item". I think if you were determined, you'd have to adapt a term such as "singular", "simple" or "primal", perhaps even "scant", but I don't think there's an existing term that meets the requirement head-on.
    – Steve
    Commented Aug 25, 2020 at 15:35
  • 3
    In SQL we speak of a "Singleton" Selection, and there is an answer for this.
    – mckenzm
    Commented Aug 26, 2020 at 3:00
  • 11
    Frame challenge: Why are you writing a function to check that two argument lists have exactly one element? This seems quite overspecific. There's no abstraction in that function. Either this function represents something that can be termed in the context of its intended use, or it should not be a function at all, imho. Commented Aug 26, 2020 at 10:35
  • 8
    Method names: HasSingleItem, ContainsSingleItem, NotPluralOrEmpty. Inspired by linq and string methods names in C#.
    – Sinatr
    Commented Aug 26, 2020 at 14:23

10 Answers 10

64

Do not come up with a new word. Your name is perfectly fine: It is unambiguous and specific and consequently leaves no doubt to the reader what you are talking about.

By contrast, singleton, unary, 1-tuple or any other term borrowed from mathematics or software engineering carries with it a baggage of preconceptions which are confusing. A list with a single element in it is emphatically not a singleton. It has nothing to do with unary operators, and a list is clearly not a tuple in C++. It is a list with one element in it, not more, not less.

That is sometimes a perceived downside of a simple approach in programming: It seems unsophisticated, and hence of a lesser value. Look at Duff's device! Marvel at the ingenuity of boost.lambda! But then listen to Jim Radigan, who leads the VC++ compiler team:

One of the other things that happen when we go to check code into the compiler is we do peer code review. So if you survive that, it’s probably ok, it’s not too complex. But if you try to check in meta-programming constructs with 4-5 different include files and virtual methods that wind up taking you places you can’t see unless you’re in a debugger – no one is going to let you check that in.

That your peer is able to understand your code right away because it is plain and simple and calls things what they are is not a sign that you didn't realize your full programming potential. It is a sign of excellence. Do not look for a Latin word.

14
  • 11
    "Do not come up with a new word" – I 100% agree. The Java API calls this a singleton list, and Java is one of the most widely used programming languages. So, don't come up with a new word, just stick with what Java already uses. Commented Aug 26, 2020 at 9:26
  • 8
    @JörgWMittag The Java API calls that a singleton if only exactly one element can be in the list. That "Highlander paradigm" is not at play in the OP's case. Calling a general list which just happens to have one element a singleton is plain misleading. Commented Aug 26, 2020 at 9:42
  • 2
    @LukasKörfer "Empty list" is a general term in computer science, denoting what it says. The contraction used to name Collections.EmptyList is derived from this general term. By contrast, at least to me singleton in software engineering is a specif term used by the gang of four to name the singleton pattern. Yes, it has usages outside this specific pattern, and even outside software, but if in the context of software somebody tells me "this is a singleton" I have a very specific idea of what they mean, and it is not a general list which just happens to have a single element in it. Commented Aug 26, 2020 at 11:47
  • 2
    @Lukas I find the immutability essential: Singleton implies there can be only one. Commented Aug 26, 2020 at 11:56
  • 6
    In a mutable (programming) world we can only describe the current state. When you call a list an empty list, you would only describe that one moment where it is actually empty. Once you added an item it is not empty anymore. Nevertheless we call lists (regarding to their current state) empty lists. In the same way we might call lists with only one item (regarding to their current state) singleton lists. Commented Aug 26, 2020 at 12:00
30

There is as far as I know, no widely accepted term in SW engineering to described a list with exactly one element

In mathematics, and more precisely in the set theory, a set with only one element is called a singleton. Unfortunately, in SW engineering, the term is so heavily associated with a design pattern, that using it for another purpose might create a confusion.

Anyway, a list is ordered, so a set is not a list. An ordered list of items is called a sequence in mathematics. And a sequence with exactly one element is called a 1-tuple. Unfortunately, a tuple corresponds to specific data structures, so this term, even converted to letters-only, would also be very ambiguous.

Finally, if mathematics don’t help, let’s look at literature! The contrary of plural is singular. According to the Meriam-Webster dictionary, it means “of, or relating to, one person, thing or instance”. So a singular list should express exactly what you want. This term is even reinforced by its ambiguity: “singular” is associated in other contexts with strangeness, and a list with only one element is indeed a strange list (as πάντα ῥεῖ already pointed out in the comments. But I’d nevertheless prefer to explain it a comment at first use ;-)

12
  • 16
    I'm not sure how representative the Java SE API is for the entire world of software engineering, but they have been calling it a singleton list since 2000. Commented Aug 25, 2020 at 15:56
  • 1
    May be worth adding that “singular” is also the obvious contrast to “plural” as suggested in the question for 2-or-more-element lists.
    – KRyan
    Commented Aug 26, 2020 at 1:52
  • 2
    Regarding the GoF pattern, in Java "singleton" still does mean that (though more usually in practice a container-managed singleton), but singleton <Collection> is commonplace. Commented Aug 26, 2020 at 4:00
  • 7
    To me, "a singular list" sounds like it means there's only one list being considered. The dictionary definitions for the word also include "Being only one of a larger population" and "of or relating to a separate person or thing / ... a single instance or to something considered by itself". Is there something wrong with just "a one-element list"? (Also, context probably matters in that if it's possible to define the terms used which gives some leeway.)
    – ilkkachu
    Commented Aug 26, 2020 at 4:41
  • 1
    @JörgWMittag: But a "singleton list" in the Java API is an immutable list of length one. I think it would be rather surprising for an API to use the term "singleton" to mean only that a collection currently has exactly one element.
    – ruakh
    Commented Aug 26, 2020 at 7:26
20

The term you are looking for is singleton.

In mathematics, a set of cardinality 1 is called a singleton set or just singleton. A Tuple of length one is sometimes called a singleton as well.

By analogy, collections of size 1 are typically also called singleton collections in programming, see for example the java.util.Collections.singleton, java.util.Collections.singletonList, and java.util.Collections.singletonMap methods added in Java 1.3 or this GitHub discussion titled Singleton list types (single item list), or this Python tutorial: "A list with a single object is sometimes referred to as a singleton list".

The Software Design Pattern that ensures that a class can only be instantiated once is called the Singleton Design Pattern, [a type that can only be inhabited by one value is called a singleton type.

So, a good name for your method would be is_singleton_list, or, since it is an instance method of the collection, just is_singleton.

17
  • 6
    To be clear, is a "singleton list" one that can only contain one item, or is it one that does contain just one for the time being, whilst being capable of containing any number?
    – Steve
    Commented Aug 25, 2020 at 15:44
  • 7
    Since I am a fan of functional programming, there is no difference to me, since a list is immutable and only ever contains what it contains right now :-D So, a singleton list is a list that contains one element, period. However, for the case of a mutable list, I would argue that it depends on how many elements the list contains, not how many it could contain. (A list that can only contain at most one item wold be isomorphic to Option, a list that can only contain exactly one item is isomorphic to the item.) Commented Aug 25, 2020 at 15:49
  • In all fairness, and even if not totally convinced that it might not create some confusion, this is a well argumented answer !
    – Christophe
    Commented Aug 25, 2020 at 16:18
  • 1
    Haskell does use singleton for various structures as well
    – Bergi
    Commented Aug 26, 2020 at 0:30
  • 6
    With all due respect, the term they are looking for is surely not singleton. At least ever since 1994, the term singleton in software engineering refers to a "Highlander" design pattern: "There can be only one." This does not mean that tonight he is the only man at the bar; it means there can be only one overall, period. This is not mathematics; this is Sparta. Commented Aug 26, 2020 at 8:55
3

There is no standard terminology for this. Even 'plural container' doesn't see any use; if I'd encounter that term in the wild, I would assume it's a group of plural words (["tests", "programs"]). Therefore, even if you would find a fitting term (e.g. 'trivial'), you should not use it to name methods if you want other people to understand from the name alone what it does.

The current name is reasonably clear, but perhaps you need a more generic method. I'm not sure about Python naming conventions, but I'm used to start methods with a verb:

def check_length(self, x, y, length):
    return len(x) == length and len(y) == length
1
  • Upvote for the first half alone! Glorfindel would assume a "plural container" is collection of plural items, while I'd assume it's some kind of special container that stores multiple sets of items. It's definitely not clear what that would mean, possibly because it's so redundant as to be confusing. I'm relieved to see that this question is the highest result when googling "plural container" programming
    – A C
    Commented Sep 25, 2020 at 0:42
3

Plural container is wrong to begin with. There is nothing plural about the container, it will remain to be one container no matter how many elements it contains.

Your method could be named GetNaiveCount(). The result type could be an enum NaiveCount: None, One, Many. I would make it a property NaiveCount though if the language supports that.

2
  • Thank you! A container is a container, irrespective it’s length. But I wonder: is GetNaiveCount a Container method or rather a method for numbers? Commented Aug 26, 2020 at 10:46
  • @HartmutBraun Here GetNaiveCount is supposed to be a member of the container. Commented Aug 26, 2020 at 12:06
2

in set theory this can be called a singleton or a unit set, see also https://en.wikipedia.org/wiki/Singleton_(mathematics)

1

From a practical programmer's point of view, it is just a list. It has the interface and the behaviour of a list. It's type is list. The fact that it currently holds only one element doesn't change that, and that state may change soon anyhow.

Personally I will instantly understand if you tell me you have "a list of one (element)", but I will ask you to clarify if you use "singleton list" or one of the other special terms presented here, because most of them are already (over)loaded with a certain meaning. It may make sense within a special domain to define a term for it, to differentiate between several specializations that need to be treated differently and make communication less ambiguous, but then your peers will hopefully understand the subtleties (if not it's your issue to introduce it to them).

But don't make things more complex than they need to be.

-1

In mathematics, and more precisely in the set theory, a set with only one element is called a singleton. Since lists are sets, then a list with one element can be called a singleton list.

Yes, lists are sets... or at least they can be encoded in set theory:

s is a list of type X iff there exists n in Nat such that s:[1,n] --> X

Then, s is a function and so is a set of ordered pairs. In this case the ordered pairs in s are of the form (k,x) where k in [1,n] and x in X.

In this way, the list [hello,world] is encoded as the set {(1,hellow),(2,world)}. And the singleton list [hello] becomes the singleton set {(1,hello)}. Clearly, the empty list [] is the empty set {}.

The first time I saw this encoding was in the Z formal notation (for formal specification) designed by Jean-Raymond Abrial.

6
  • what do these dollar signs mean? $X$, $s$ etc
    – gnat
    Commented Sep 23, 2020 at 14:49
  • I tried to write math formulas in Latex which is supposed to be supported here, but for some reason it doesn't. I gave up after 45'. It should look like here. Commented Sep 24, 2020 at 19:16
  • 1
    @MaximilianoCristiá Only some sites have mathjax enabled due to overhead. Commented Sep 25, 2020 at 15:30
  • @Deduplicator Thanks! I didn't know that. Changed to plain text. Commented Sep 27, 2020 at 1:46
  • 1
    What is the list, its name or its contents? {x is a natural number, x < 10 ^ x > 8} is an unnamed set, you are just describing its contents. Semantically if you add an element to a list, it's another list; it can't be the same list because in that case your program hasn't moved from one state to another, when it did. Commented Sep 29, 2020 at 12:43
-2

If it can hold zero or one element, a common name is optional, for example C++ has std::optional.

2
  • 2
    It's not about what it can hold, it's about the current holdings: in my case it's about two such containers. So what word describes: "these lists both have length 1"? Commented Aug 27, 2020 at 8:25
  • This answer would only apply to the Maybe<T> monad/type.
    – julealgon
    Commented Sep 25, 2020 at 20:23
-2

The distinction is only meaningful in languages with Run-Time Type Information, or can have types implicitly contain other types, such as any or Object. At runtime in a function whose type is already decided, a 1-tuple of an int and an int don't have type tags, the set of operations applicable to each is identical.

A list is usually a container of dynamic length, so its size is only meaningful if you use dependent typing or contract programming, because the length is dynamic and not truly a property of the type in the mathematical sense.

The tuple syntax (1, 2) in Haskell is of type (Int, Int)

(1) is the same thing (Int) which is the same type as Int. In other words, the type of a 1-tuple of a type is the same type as that type. If you think about it, this makes sense in a language where variables can only be of one type: The same set of operations apply to a 1-tuple of a thing as to a thing itself, because the type is predetermined outside the function call.

6
  • 1
    I'm not the downvoter, but your argument doesn't strictly make sense. A 1-tuple is not necessarily the same as a scalar - at least, the equivalence cannot be sustained if the language permits multi-dimensional arrays, where an array does not just have a length, but also a rank, and a length-1-rank-1 array (a tuple) is different from a length-1-rank-0 array (a scalar).
    – Steve
    Commented Aug 26, 2020 at 16:41
  • @Steve Rank and length of a tuple is a type-level construct. In this context, dynamically typed means any language with RTTI. For an instantiated function, the types have already been decided what to run. When types are removed, a length-1 rank-0 tuple is the same as a length-1 tuple and a scalar. Commented Aug 27, 2020 at 20:09
  • A length-1-rank-0 array is a scalar. But that's not the same structurally as a length-1-rank-1 array. The value-carrying capacity may be identical, but the mechanics of the storage are not identical. And if the storage is not identical, the operations cannot possibly be identical. It's the introduction of rank (i.e. nesting level, or multiple axes) which forces the distinction, whereas the distinction does not exist when only length is considered (i.e. in a flat model with only sequences of values). It's a crucial oversight of most mathematical treatments as applied to computing.
    – Steve
    Commented Aug 28, 2020 at 0:31
  • @Steve I don't see what you're saying. If the length and rank are part of the type, and the value-carrying capacity is part of the type, and the type has already been resolved prior to the function call, then clearly the distinction is meaningless. (Otherwise the function effectively takes a Either<Length 1 rank 0, Length 1 rank 1, int>. Either is the same as Maybe.) Commented Aug 28, 2020 at 17:53
  • As I say, the value-carrying capacity (of a scalar type vs a length-1-rank-1 array) may be the same, but the storage of the value is structured differently. The length-1-rank-1 array contains a scalar. They cannot possibly be the same type, in any type system that admits nested arrays. There may be an implicit conversion to and from a scalar, but that only confirms the existence of the type distinction in principle. The distinction also exists in natural language and ordinary understandings - a bowling ball bag containing a bowling ball, is not the same item as just the bowling ball.
    – Steve
    Commented Aug 28, 2020 at 18:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.