Ellipsis (computer programming): Difference between revisions

Content deleted Content added
Gedge67 (talk | contribs)
m adding refs for the perl/perl6 operator
Zambyte (talk | contribs)
m Scheme: Added reference title
Tags: Mobile edit Mobile app edit Android app edit App section source
 
(24 intermediate revisions by 17 users not shown)
Line 1:
{{Short description|Notation for ranges or parent directory}}
{{refimprovemore citations needed|date=September 2014}}
In [[computer programming]], '''ellipsis''' notation (.. or ...) is used to denote [[range (computer programming)|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.
{{use dmy dates|date=June 2022|cs1-dates=y}}
In [[computer programming]], '''ellipsis''' notation (.. or ...) is used to denote [[range (computer programming)|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 language]]s (including [[Ada (programming language)|Ada]], [[Perl]], [[Ruby (programming language)|Ruby]], [[Groovy (programming language)|Apache Groovy]], [[Kotlin (programming language)|Kotlin]], [[Haskell (programming language)|Haskell]], and [[Pascal (programming language)|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 [[integer]]s between 1 and 100 inclusive in Perl:
 
:<code>foreach (1..100)</code>
Line 9 ⟶ 11:
In Ruby the <code>...</code> operator denotes a half-open range, i.e. that includes the start value but not the end value.
 
In [[Rust (programming language)|Rust]] the <code>..=</code> operator denotes an inclusive range for cases in matches and the <code>..</code> operator represents a range not including the end value.
 
Perl and Ruby [[Operator overloading|overload]] the ".." operator in scalar context as a [[flip-flop operator]] - a [[stateful]] [[bistability|bistable]] [[Boolean datatypedata type|Boolean]] test, roughly equivalent to "true while ''x'' but not yet ''y''", similarly to the "," operator in [[Sed (programming language)|sed]] and [[AWK programming language|AWK]].<ref>[http://perldoc.perl.org/perlop.html#Range-Operators-operator%2c-range-range-..-... perlop - perldoc.perl.org<!-- Bot generated title -->]</ref> In Perl6 an actual [[Unicode]] (U+2026) ellipsis (…) character is used to serve as a type of marker in a format string.<ref>[http://dev.perl.org/perl6/doc/design/exe/E07.html#And_mark_what_way_I_make... Exegesis 7: Formats - perl6<!-- Bot generated title -->]{{Self-published inline|date=January 2011}}</ref>
 
The [[GNU Compiler Collection|GNU C]] hascompatible compilers have an extension to the C and C++ language to allow ''case ranges'' in [[switch statement]]s:
<sourcesyntaxhighlight lang="C">
switch(u) {
case 0 ... 0x7F : putchar(c); break;
Line 21 ⟶ 23:
default: error("not supported!");
}
</syntaxhighlight>
</source>
Additionally, GNU C allows a similar range syntax for [[C syntax#Designated initializers|designated initializers]], available in the C language only:
<syntaxhighlight lang="C">
int array[10] = { [0...5] = 1 };
</syntaxhighlight>
 
Delphi / Turbo Pascal / Free Pascal:
<sourcesyntaxhighlight lang="delphi">
var FilteredChars: set of [#0..#32,#127,'a'..'z'];
var CheckedItems: set of [4,10..38,241,58];
</syntaxhighlight>
</source>
 
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]]).
Line 36 ⟶ 42:
 
== Incomplete code ==
{{anchor|yadayada}} <!-- ref from Perl -->
In Perl<ref>{{Cite web |url=https://perldoc.perl.org/perlsyn.html#The-Ellipsis-Statement |title=Perlsyn - Perl syntax - Perldoc Browser}}</ref> and [[Perl6Raku (programming language)|Raku]]<ref>{{Cite web |url=https://doc.perl6.org/language/operators#listop_... |title=Operators }}</ref> 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 <code>A ::= INTEGER (0..127, ..., 256..511)</code> an ellipsis is used to separate the extension root from extension additions. The definition of type A in version 1 system of the form <code>A ::= INTEGER (0..127, ...)</code> and the definition of type A in version 2 system of the form <code>A ::= INTEGER (0..127, ..., 256..511)</code> 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: {{code|2=asn1|1=B ::= SEQUENCE { a INTEGER, b INTEGER, ..., c INTEGER } }} <!-- Is the s.n. ellipsis defined as a "..." trigraph or something else? This paragraph is a matter of syntax, not text *representation*, and therefore is misplaced. --Incnis Mrsi -->
 
== Variable number of parameters ==
Line 45 ⟶ 54:
In the [[C (programming language)|C programming language]], an ellipsis is used to represent a [[variadic function|variable number of parameters]] to a [[function (programming)|function]]. For example:
 
:{{code|2=c|int printf( const char* format, ... );}}<ref>{{Cite web |url=http://www.cplusplus.com/reference/cstdio/printf/ |title=Printf - C++ Reference }}</ref>
 
The above function in C could then be called with different types and numbers of parameters such as:
Line 55 ⟶ 64:
[[C99]] introduced macros with a [[variadic macro|variable number of arguments]].<ref>[https://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html Variadic Macros - Using the GNU Compiler Collection (GCC)]</ref>
 
[[C++11]] included the C99 preprocessor,<ref>Working draft changes for C99 preprocessor synchronization - http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm</ref> and also introduced templates with a [[variadic template|variable number of arguments]].<ref>{{cite web | title=Working Draft, Standard for Programming Language C++ | url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf}}: 14.5.3 Variadic templates</ref>
 
===Java===
Line 64 ⟶ 73:
===PHP===
 
PHP 5.6 supports<ref>{{Cite web |url=https://wiki.php.net/rfc/variadics |title=PHP: RFC:variadics}}</ref> use of ellipsis to define an explicitly [[variadic function]], where <code>...</code> before an argument in a function definition means that arguments from that point on will be collected into an array. For example:
 
<sourcesyntaxhighlight lang="php">
function variadic_function($a, $b, ...$other)
{
return $other;
}
 
var_dump(variadic_function(1, 2, 3, 4, 5));
</syntaxhighlight>
</source>
 
Produces this output:
<sourcesyntaxhighlight lang="php">
array(3) {
[0]=>
Line 84 ⟶ 94:
int(5)
}
</syntaxhighlight>
</source>
 
== Scheme ==
The <code>syntax-rules</code> hygienic macro system originally introduced in [[Scheme (programming language)|R4RS]] uses <code>...</code> to specify that the proceeding pattern may be matched zero or more times. For example, the following code could be used to implement the standard <code>let*</code> form, recursively in terms of itself, and the more primitive <code>let</code>:
 
<syntaxhighlight lang="scheme">
(define-syntax let*
(syntax-rules ()
((let* () body1 body2 ...)
(let () body1 body2 ...))
((let* ((name1 val1) (name2 val2) ...) body1 body2 ...)
(let ((name1 val1))
(let* ((name2 val2) ...) body1 body2 ...)))))
</syntaxhighlight>
 
SRFI 46<ref>{{Cite web |url=https://srfi.schemers.org/srfi-46/srfi-46.html |title=SRFI 46}}</ref> was proposed to extend <code>syntax-rules</code> to allow the user to specify an ellipsis identifier. This is useful for disambiguating when ellipsis are use in nested macro definitions. This feature was later included in R7RS.
 
== Multiple dimensions ==
In [[Python (programming language)|Python]], particularly in [[numpy]], anthe ellipsis is useda fornullary slicingexpression anthat arbitraryrepresents numberthe of dimensions for a high-dimensional array:<refcode>http://docs.scipy.org/doc/numpy/reference/arrays.indexing.htmlEllipsis</refcode> singleton.
 
It's used particularly in [[NumPy]], where an ellipsis is used for slicing an arbitrary number of dimensions for a high-dimensional array:<ref>{{Cite web |url=http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html |title=Indexing routines — NumPy v1.22 Manual}}</ref>
<source lang="numpy">
 
<syntaxhighlight lang="pycon">
>>> import numpy as np
>>> t = np.random.rand(2, 3, 4, 5)
Line 96 ⟶ 123:
>>> t[0, ...].shape # select 1st element from first dimension, copy rest
(3, 4, 5)
</syntaxhighlight>
</source>
 
== Other semantics ==
 
In [[MATLAB]], a three-character ellipsis is used to indicate [[line continuation]],<ref>[http://www.mathworks.com/help/techdoc/matlab_env/f0-5789.html#f0-5857 Mathworks.com]</ref> making the sequence of lines
=== MATLAB ===
In [[MATLAB]], a three-character ellipsis is used to indicate [[line continuation]],<ref>[{{Cite web |url=http://www.mathworks.com/help/techdoc/matlab_env/f0-5789.html#f0-5857 |title=Mathworks.com] |access-date=2011-04-14 |archive-date=2011-06-29 |archive-url=https://web.archive.org/web/20110629105840/http://www.mathworks.com/help/techdoc/matlab_env/f0-5789.html#f0-5857 |url-status=dead }}</ref> making the sequence of lines
:<code>x = [ 1 2 3 ...<br />4 5 6 ];</code>
semantically equivalent to the single line
:<code>x = [ 1 2 3 4 5 6 ];</code>
 
In Raku an actual [[Unicode]] (U+2026) ellipsis (…) character is used to serve as a type of marker in a format string.<ref>{{cite web |title=Exegesis 7: Formats |editor-first=Larry |editor-last=Wall |editor-link=Larry Wall |author-first=Damian |author-last=Conway |author-link=Damian Conway |date=2006-05-29 |orig-date=2004-02-26 |version=2 |number=7 |website=dev.perl.org |url=http://dev.perl.org/perl6/doc/design/exe/E07.html#And_mark_what_way_I_make... |url-status=dead |archive-url=https://web.archive.org/web/20110615080653/http://dev.perl.org/perl6/doc/design/exe/E07.html#And_mark_what_way_I_make... |archive-date=2011-06-15}}</ref>
 
=== PHP ===
 
Since [[PHP]] 8.1, a nullary ellipsis may be used to create a [[closure (computer programming)|closure]] from a callable or an object method:<ref>{{Cite web |title=PHP 8.1.0 Release Announcement |url=https://www.php.net/releases/8.1/en.php#first_class_callable_syntax |access-date=2023-03-29 |website=php.net}}</ref>
 
<syntaxhighlight lang="php">
// 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(...);
</syntaxhighlight>
 
=== Python ===
 
In Python, the ellipsis can also be used as the first parameter within the <code>collections.abc.Callable</code> type annotation to denote any number of arguments:<ref>{{Cite web |title=typing — Support for type hints § typing.Callable |url=https://docs.python.org/3/library/typing.html#typing.Callable |access-date=2023-03-29 |website=Python 3.11.2 Documentation}}</ref>
<!-- typing.Callable is deprecated in favor of collections.abc.Callable, but documentation has not yet been moved! -->
 
<syntaxhighlight lang="python">
from collections.abc import Callable
from typing import TypeAlias, Any
 
VarFunction: TypeAlias = Callable[..., Any]
</syntaxhighlight>
 
==References==