Distinct ordinal numbers to avoid off-by-one errors
Denominative introduces a distinction between the types of cardinal and ordinal numbers with the clear and ambitious goal of avoiding off-by-one errors, without compromising performance.
- provides an
Ordinal
type representing ordinal numbers Ordinal
s should be used for values which zero-indexed or one-indexed cardinal numbers could be ambiguousOrdinal
s are distinct from cardinalInt
s- conversions between
Ordinal
s andInt
s may be made only by specifying zero- or one-indexing - introduces a distinct namespace for the first ten ordinals
Denominative introduces a new type, Ordinal
, which represents an ordinal number. Unlike cardinal numbers
(still represented by Int
s and Long
s) an Ordinal
has a first element, called Prim
, which unambiguously
refers to the first element of any sequence without the need to specify if the sequence is zero- or one-indexed.
(There is, absolutely fundamentally, no concept of a "zeroth" Ordinal
.)
Ordinal
is represented internally by an Int
, so shares the performance characteristics of using Int
s, but
is a distinct type. Thus, an Int
such as 1
, 138
or -12
, cannot be used where an Ordinal
is expected,
and an Ordinal
cannot be used where an Int
is expected.
Conversions between Int
s and Ordinal
s may only
The first ten Ordinal
numbers have names, which arise from the first part of the sequence that begins,
"primary", "secondary", "tertiary", etc.:
Prim
Sec
Ter
Quat
Quin
Sen
Sept
Oct
Non
Den
However, in practice, only Prim
and Sec
are likely to find regular use.
Given a sequence of elements, it's often useful to be able to refer to the last or second-to-last elements. This
is possible with the ult
(short for "ultimate") and pen
(short for "penultimate") extension methods that are
available on any countable value, and which return the Ordinal
referring to these elements. A countable
value typically means a Seq
or one of its subtypes, but is actually an instance of any type that implements
the Countable
typeclass.
The ante
extension method refers to the ordinal before pen
, that is, the third-to-last (or antepenultimate)
Ordinal
index.
Certain arithmetic operations are possible between Ordinal
values and Int
s, but many operations that exist
for cardinal numbers (such as multiplication and division) do not make sense for ordinal numbers.
Here are some valid operations.
A cardinal number may be added to an Ordinal
:
val ordinal: Ordinal = Ter + 3 // Sen
val ordinal2: Ordinal = 3 + Quin // Oct
A cardinal number may be subtracted from an Ordinal
:
val ordinal: Ordinal = Den - 7 // Ter
One Ordinal
may be subtracted from another:
val cardinal: Int = Non - Sept // 2
A range of Ordinal
s is represented by an Interval
. In all cases, this is a closed or "inclusive" interval,
and is specified by its first Ordinal
and its (included) final Ordinal
. For example,
val interval: Interval = Ter ~ Sen
would represent the elements 2, 3, 5, 8 of the Fibonacci sequence, 1, 1, 2, 3, 5, 8, 13, 21, etc.
If, on the other hand, we had a finite sequence, xs
, 2, 4, 6, 8, 10, 12, we could refer to all but the first
and last elements by writing Sec to xs.pen
. This would produce an interval representing the ordinals,
Sec
, Ter
, Quat
, Quin
; omitting Prim
and Sen
(which would be xs.ult
).
The size of an Interval
is a cardinal number, thus an Int
. We can get an Interval
s size with the size
method.
An Interval
is most useful as a way of specifying a range of Ordinal
values because we want to perform some
operation iteratively using each of the values. Two methods are provided: foreach
and foldLeft
, which
behave exactly as their familiar counterparts in Scala's standard collections library.
Denominative is classified as embryotic. For reference, Soundness projects are categorized into one of the following five stability levels:
- embryonic: for experimental or demonstrative purposes only, without any guarantees of longevity
- fledgling: of proven utility, seeking contributions, but liable to significant redesigns
- maturescent: major design decisions broady settled, seeking probatory adoption and refinement
- dependable: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version
1.0.0
or later - adamantine: proven, reliable and production-ready, with no further breaking changes ever anticipated
Projects at any stability level, even embryonic projects, can still be used, as long as caution is taken to avoid a mismatch between the project's stability level and the required stability and maintainability of your own project.
Denominative is designed to be small. Its entire source code currently consists of 151 lines of code.
Denominative will ultimately be built by Fury, when it is published. In the meantime, two possibilities are offered, however they are acknowledged to be fragile, inadequately tested, and unsuitable for anything more than experimentation. They are provided only for the necessity of providing some answer to the question, "how can I try Denominative?".
-
Copy the sources into your own project
Read the
fury
file in the repository root to understand Denominative's build structure, dependencies and source location; the file format should be short and quite intuitive. Copy the sources into a source directory in your own project, then repeat (recursively) for each of the dependencies.The sources are compiled against the latest nightly release of Scala 3. There should be no problem to compile the project together with all of its dependencies in a single compilation.
-
Build with Wrath
Wrath is a bootstrapping script for building Denominative and other projects in the absence of a fully-featured build tool. It is designed to read the
fury
file in the project directory, and produce a collection of JAR files which can be added to a classpath, by compiling the project and all of its dependencies, including the Scala compiler itself.Download the latest version of
wrath
, make it executable, and add it to your path, for example by copying it to/usr/local/bin/
.Clone this repository inside an empty directory, so that the build can safely make clones of repositories it depends on as peers of
denominative
. Runwrath -F
in the repository root. This will download and compile the latest version of Scala, as well as all of Denominative's dependencies.If the build was successful, the compiled JAR files can be found in the
.wrath/dist
directory.
Contributors to Denominative are welcome and encouraged. New contributors may like to look for issues marked beginner.
We suggest that all contributors read the Contributing Guide to make the process of contributing to Denominative easier.
Please do not contact project maintainers privately with questions unless there is a good reason to keep them private. While it can be tempting to repsond to such questions, private answers cannot be shared with a wider audience, and it can result in duplication of effort.
Denominative was designed and developed by Jon Pretty, and commercial support and training on all aspects of Scala 3 is available from Propensive OÜ.
To denominate is to assign a name to, which is denominative. Denominative assigns new names to the ordinal numbers in order to distinguish them from the cardinals.
In general, Soundness project names are always chosen with some rationale, however it is usually frivolous. Each name is chosen for more for its uniqueness and intrigue than its concision or catchiness, and there is no bias towards names with positive or "nice" meanings—since many of the libraries perform some quite unpleasant tasks.
Names should be English words, though many are obscure or archaic, and it should be noted how willingly English adopts foreign words. Names are generally of Greek or Latin origin, and have often arrived in English via a romance language.
The logo shows an on/off symbol, itself comprised of a 0 (off) and a 1 (on), alluding to the ideo of being "off by one".
Denominative is copyright © 2024 Jon Pretty & Propensive OÜ, and is made available under the Apache 2.0 License.