Jump to content

Ellipsis (computer programming)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Beland (talk | contribs) at 17:58, 21 October 2016 (new section: Parent directory, and adjust intro). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, ellipsis notation (.. or ...) is used to denote ranges, an unspecified number of arguments, or a parent directory. 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 Ada, 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 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] In Perl6 an actual Unicode (U+2026) ellipsis (…) character is used to serve as a type of marker in a 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!");
}

Delphi / Turbo Pascal / Free Pascal:

var FilteredChars: set of [#0..#32,#127,'a'..'z'];
var CheckedItems: set of [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 and 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.

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

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:

int printf( const char* format, ... );[3]

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.[4]

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

Java

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

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

PHP

PHP 5.6 supports[7] 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:

function variadic_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, particularly in numpy, an ellipsis is used for slicing an arbitrary number of dimensions for a high-dimensional array:[8]

>>> 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,[9] 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 ];

References

  1. ^ perlop - perldoc.perl.org
  2. ^ Exegesis 7: Formats - perl6[self-published source?]
  3. ^ http://www.cplusplus.com/reference/cstdio/printf/
  4. ^ Variadic Macros - Using the GNU Compiler Collection (GCC)
  5. ^ Working draft changes for C99 preprocessor synchronization - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm
  6. ^ "Working Draft, Standard for Programming Language C++" (PDF).: 14.5.3 Variadic templates
  7. ^ https://wiki.php.net/rfc/variadics
  8. ^ http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
  9. ^ Mathworks.com