When it comes to lists there are good, short and nice sounding names for most parts of list
- Head
- Tail
- Last – somewhat commonly used
Currently I am doing a lots of TypeScript type manipulation and often I need to get all the starting elements of list/tuple except for the last element, e.g. part [1,2,3] of [1,2,3,4] and I don’t really have good nice name for this, is there anything that is agreed upon for is less common case like in Head/Tail instance? I currently name it as Initial but don’t really like it
2
Smalltalk has names which I (as a Smalltalk programmer) consider pretty ok:
first
and last
return the first or last element of a sequenceable collection (array, list, etc).
allButFirst
and allButLast
return the sub-collection without the first or last element.
Many languages use .. (or 🙂 to denote slices, and I find Python’s solution most appealing. There, the indexes in a slice a[start:end]
, and start and end can be omitted or negative. All but first elements of a list then becomes a[1:]
, and all but last a[:-1]
, which is both terse and versatile and avoids the naming issue altogether.
1
There is no particular standard or even informal standard for this, so you’ll have to establish a term at least for your project.
To be understood as conveying a similar concept to those more well-known terms, it should be a short, common everyday word. “Body” unfortunately would imply leaving out the head and the tail, so it’s out. “Start” (to me) sounds more like the meaning of “head”, i.e. containing only one element. I think in your situation I’d go with front, which at least conveys a meaning of containing several elements rather than just one.
1
Like @Killian I don’t think there’s an established word for this, when referring to something which is all-but-the-last.
Perhaps the “fore”, or something derived from it as a prefix and strongly implying a multitude or an area (rather than a single item), like “foregroup” or “forepart“. These are likely to be the most self-evident words if other people encounter them.
Or if you wanted to stay with the animal metaphors like head and tail, then perhaps “nose” or “beak“, or even “snout“.
The is also “trunk” – which besides being long and at the front of the elephant, has a strong connotation from trees of “the main thing” or “the major part”.
I can’t think of many objects which are regarded as having a long piece conventionally at the front as well as a short piece in the rear. Perhaps a whip, which has a “flail” and a handle – which would conveniently be a nice rhyming counterpart with “tail”, and memorised as a “front tail”.
1