Ellipsis (computer programming)

Last updated

In computer programming, ellipsis notation (.. or ...) is used to denote ranges, an unspecified number of arguments, or a parent directory. Most programming languages require the ellipsis to be written as a series of periods; a single (Unicode) ellipsis character cannot be used.

Contents

Ranges

In some programming languages (including Ada, Perl, Ruby, Apache Groovy, Kotlin, Haskell, and Pascal), a shortened two-dot ellipsis is used to represent a range of values given two endpoints; for example, to iterate through a list of integers between 1 and 100 inclusive in Perl:

foreach (1..100)

In Ruby the ... operator denotes a half-open range, i.e. that includes the start value but not the end value.

In Rust the ..= operator denotes an inclusive range for cases in matches and the .. operator represents a range not including the end value.

Perl and Ruby overload the ".." operator in scalar context as a flip-flop operator - a stateful bistable Boolean test, roughly equivalent to "true while x but not yet y", similarly to the "," operator in sed and AWK. [1]

GNU C compatible compilers have an extension to the C and C++ language to allow case ranges in switch statements:

switch(u){case0...0x7F:putchar(c);break;case0x80...0x7FF:putchar(0xC0+c>>6);putchar(0x80+c&0x3f);break;case0x800...0xFFFF:putchar(0xE0+c>>12);putchar(0x80+(c>>6)&0x3f);putchar(0x80+(c>>12));break;default:error("not supported!");}

Additionally, GNU C allows a similar range syntax for designated initializers, available in the C language only:

intarray[10]={[0...5]=1};

Delphi / Turbo Pascal / Free Pascal:

varFilteredChars:setof[#0..#32,#127,'a'..'z'];varCheckedItems:setof[4,10..38,241,58];

In the Unified Modeling Language (UML), a two-character ellipsis is used to indicate variable cardinality of an association. For example, a cardinality of 1..* means that the number of elements aggregated in an association can range from 1 to infinity (a usage equivalent to Kleene plus).

Parent directory

On Windows and Unix-like operating systems, ".." is used to access the parent directory in a path.

Incomplete code

In Perl [2] and Raku [3] the 3-character ellipsis is also known as the "yada yada yada" operator and, similarly to its linguistic meaning, serves as a "stand-in" for code to be inserted later.

Python3 also allows the 3-character ellipsis to be used as an expressive place-holder for code to be inserted later.

In Abstract Syntax Notation One (ASN.1), the ellipsis is used as an extension marker to indicate the possibility of type extensions in future revisions of a protocol specification. In a type constraint expression like A ::= INTEGER (0..127, ..., 256..511) an ellipsis is used to separate the extension root from extension additions. The definition of type A in version 1 system of the form A ::= INTEGER (0..127, ...) and the definition of type A in version 2 system of the form A ::= INTEGER (0..127, ..., 256..511) constitute an extension series of the same type A in different versions of the same specification. The ellipsis can also be used in compound type definitions to separate the set of fields belonging to the extension root from the set of fields constituting extension additions. Here is an example: B::=SEQUENCE{aINTEGER,bINTEGER,...,cINTEGER}

Variable number of parameters

C and C++

In the C programming language, an ellipsis is used to represent a variable number of parameters to a function. For example:

intprintf(constchar*format,...); [4]

The above function in C could then be called with different types and numbers of parameters such as:

printf("numbers %i %i %i",5,10,15);

and

printf("input string %s, %f","another string",0.5);

C99 introduced macros with a variable number of arguments. [5]

C++11 included the C99 preprocessor, [6] and also introduced templates with a variable number of arguments. [7]

Java

As of version 1.5, Java has adopted this "varargs" functionality. For example:

publicintfunc(intnum,String...strings)

PHP

PHP 5.6 supports [8] use of ellipsis to define an explicitly variadic function, where ... before an argument in a function definition means that arguments from that point on will be collected into an array. For example:

functionvariadic_function($a,$b,...$other){return$other;}var_dump(variadic_function(1,2,3,4,5));

Produces this output:

array(3){[0]=>int(3)[1]=>int(4)[2]=>int(5)}

Multiple dimensions

In Python, the ellipsis is a nullary expression that represents the Ellipsis singleton.

It's used particularly in NumPy, where an ellipsis is used for slicing an arbitrary number of dimensions for a high-dimensional array: [9]

>>> importnumpyasnp>>> t=np.random.rand(2,3,4,5)>>> t[...,0].shape# select 1st element from last dimension, copy rest(2, 3, 4)>>> t[0,...].shape# select 1st element from first dimension, copy rest(3, 4, 5)

Other semantics

MATLAB

In MATLAB, a three-character ellipsis is used to indicate line continuation, [10] making the sequence of lines

x = [ 1 2 3 ...
4 5 6 ];

semantically equivalent to the single line

x = [ 1 2 3 4 5 6 ];

In Raku an actual Unicode (U+2026) ellipsis (…) character is used to serve as a type of marker in a format string. [11]

PHP

Since PHP 8.1, a nullary ellipsis may be used to create a closure from a callable or an object method: [12]

// old style: PHP 8.0 and older$foo=[$this,'foo'];$fn=Closure::fromCallable('strlen');// new style: PHP 8.1 and newer$foo=$this->foo(...);$fn=strlen(...);

Python

In Python, the ellipsis can also be used as the first parameter within the collections.abc.Callable type annotation to denote any number of arguments: [13]

fromcollections.abcimportCallablefromtypingimportTypeAlias,AnyVarFunction:TypeAlias=Callable[...,Any]

Related Research Articles

C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems, device drivers, and protocol stacks, but its use in application software has been decreasing. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems.

The C preprocessor is the macro preprocessor for several computer programming languages, such as C, Objective-C, C++, and a variety of Fortran languages. The preprocessor provides inclusion of header files, macro expansions, conditional compilation, and line control.

<span class="mw-page-title-main">C syntax</span> Set of rules defining correctly structured programs

The syntax of the C programming language is the set of rules governing writing of software in C. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

printf is a C standard library function that formats text and writes it to standard output.

<span class="mw-page-title-main">Foreach loop</span> Control flow statement for traversing items in a collection

In computer programming, foreach loop is a control flow statement for traversing items in a collection. foreach is usually used in place of a standard for loop statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal.

In computer programming, the ternary conditional operator is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, ternary if, or inline if. An expression a ? b : c evaluates to b if the value of a is true, and otherwise to c. One can read it aloud as "if a then b otherwise c". The form a ? b : c is by far and large the most common, but alternative syntaxes do exist; for example, Raku uses the syntax a ?? b !! c to avoid confusion with the infix operators ? and !, whereas in Visual Basic .NET, it instead takes the form If(a, b, c).

A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments.

In computer programming, a function prototype is a declaration of a function that specifies the function's name and type signature, but omits the function body. While a function definition specifies how the function does what it does, a function prototype merely specifies its interface, i.e. what data types go in and come out of it. The term "function prototype" is particularly used in the context of the programming languages C and C++ where placing forward declarations of functions in header files allows for splitting a program into translation units, i.e. into parts that a compiler can separately translate into object files, to be combined by a linker into an executable or a library. The function declaration precedes the function definition, giving details of name, return type, and storage class along with other relevant attributes.

In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.

The computer programming languages C and Pascal have similar times of origin, influences, and purposes. Both were used to design their own compilers early in their lifetimes. The original Pascal definition appeared in 1969 and a first compiler in 1970. The first version of C appeared in 1972.

scanf, short for scan formatted, is a C standard library function that reads and parses text from standard input.

sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.

The C and C++ programming languages are closely related but have many significant differences. C++ began as a fork of an early, pre-standardized C, and was designed to be mostly source-and-link compatible with C compilers of the time. Due to this, development tools for the two languages are often integrated into a single product, with the programmer able to specify C or C++ as their source language.

C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by the publication year of the specification, though it was formerly named C++0x because it was expected to be published before 2010.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

stdarg.h is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the header cstdarg.

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

The syntax and semantics of PHP, a programming language, form a set of rules that define how a PHP program can be written and interpreted.

In computer programming, variadic templates are templates that take a variable number of arguments.

C23 is the informal name for ISO/IEC 9899:2024, the next standard for the C programming language, which will replace C17. It was started in 2016 informally as C2x, and expected to be published in 2024. The most recent publicly available working draft of C23 was released on April 1, 2023. The first WG14 meeting for the C2x draft was held in October 2019, virtual remote meetings were held in 2020 due to the COVID-19 pandemic, then various teleconference meetings continued to occur through 2024.

References

  1. perlop - perldoc.perl.org
  2. "Perlsyn - Perl syntax - Perldoc Browser".
  3. "Operators".
  4. "Printf - C++ Reference".
  5. Variadic Macros - Using the GNU Compiler Collection (GCC)
  6. Working draft changes for C99 preprocessor synchronization - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm
  7. "Working Draft, Standard for Programming Language C++" (PDF).: 14.5.3 Variadic templates
  8. "PHP: RFC:variadics".
  9. "Indexing routines — NumPy v1.22 Manual".
  10. "Mathworks.com". Archived from the original on 2011-06-29. Retrieved 2011-04-14.
  11. Conway, Damian (2006-05-29) [2004-02-26]. Wall, Larry (ed.). "Exegesis 7: Formats". dev.perl.org. 2. Archived from the original on 2011-06-15.
  12. "PHP 8.1.0 Release Announcement". php.net. Retrieved 2023-03-29.
  13. "typing — Support for type hints § typing.Callable". Python 3.11.2 Documentation. Retrieved 2023-03-29.