Jump to content

Ellipsis (computer programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Basel Maven (talk | contribs) at 17:16, 14 April 2011 (This creates a new article by moving here from Ellipsis portions related to computer languages and operators). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

In programming

In programming, ellipsis notation (.. or ...) is mostly used for two usages: Either to denote ranges or to denote a variable or unspecified number of arguments. Most programming languages other than Perl6 require the ellipsis to be written as a series of periods; a single (Unicode) ellipsis character cannot be used.

Ranges

In some programming languages (including Perl, Ruby, Groovy, 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 Perl and Ruby, the ... operator denotes a half-open range, i.e. that includes the start value but not the end value.

Perl overloads the ".." operator in scalar context as a stateful bistable Boolean test, roughly equivalent to "true while x but not yet y".[1] In Perl6, the 3-character ellipsis is also known as the "yadda yadda yadda" operator and, similarly to its linguistic meaning, serves as a "stand-in" for code to be inserted later. In addition, an actual Unicode ellipsis character is used to serve as a type of marker in a perl6 format string.[2]

The GNU Compiler Collection has an extension to the C and C++ language to allow case ranges in switch statements:

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

Variable number of parameters

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

void func(const char* str, ...)

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

func("input string", 5, 10, 15);

and

func("input string", "another string", 0.5);

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

public int func(int num, String... strings)

C99 introduced macros with a variable number of arguments.

C++0x introduces templates with a variable number of arguments.

Multiple dimensions

In Python, particularly in numpy, an ellipsis is used for slicing an arbitrary number of dimensions for a high-dimensional array:

>>> import numpy as np
>>> 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

In MATLAB, a three-character ellipsis is used to indicate line continuation,[3] 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 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).