Ellipsis (computer programming)
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. RangesIn 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:
In Ruby the In Rust the 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) {
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!");
}
Additionally, GNU C allows a similar range syntax for designated initializers, available in the C language only: int array[10] = { [0...5] = 1 };
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 directoryOn Windows and Unix-like operating systems, ".." is used to access the parent directory in a path. Incomplete codeIn 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 Variable number of parametersC and C++In the C programming language, an ellipsis is used to represent a variable number of parameters to a function. For example:
The above function in C could then be called with different types and numbers of parameters such as:
and
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] JavaAs of version 1.5, Java has adopted this "varargs" functionality. For example:
PHPPHP 5.6 supports[8] use of ellipsis to define an explicitly variadic function, where 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)
}
SchemeThe (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 ...)))))
SRFI 46[9] was proposed to extend Multiple dimensionsIn Python, the ellipsis is a nullary expression that represents the It's used particularly in NumPy, where an ellipsis is used for slicing an arbitrary number of dimensions for a high-dimensional array:[10] >>> 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 semanticsMATLABIn MATLAB, a three-character ellipsis is used to indicate line continuation,[11] making the sequence of lines
semantically equivalent to the single line
In Raku an actual Unicode (U+2026) ellipsis (…) character is used to serve as a type of marker in a format string.[12] PHPSince PHP 8.1, a nullary ellipsis may be used to create a closure from a callable or an object method:[13] // 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(...);
PythonIn Python, the ellipsis can also be used as the first parameter within the from collections.abc import Callable
from typing import TypeAlias, Any
VarFunction: TypeAlias = Callable[..., Any]
References
|
Portal di Ensiklopedia Dunia