Range (computer programming): Difference between revisions

Content deleted Content added
Fix broken link
mNo edit summary
 
(29 intermediate revisions by 16 users not shown)
Line 1:
{{RefimproveMore citations needed|date=December 2006}}
In [[computer science]], the term '''range''' may refer to one of three things:
# The possible values that may be stored in a [[variable (programming)|variable]].
Line 5:
# An alternative to [[iterator]].
 
== Range of a variable ==
The range of a variable is given as the set of possible values that that variable can hold. In the case of an integer, the variable definition is restricted to whole numbers only, and the range will cover every number within its range (including the maximum and minimum). For example, the range of a [[signedness|signed]] [[16-bit]] [[Integer (computer science)|integer]] variable is all the integers from −32,768 to +32,767.
 
== Range of an array ==
{{Main|Array data type#Indexing notation}}
 
When an array is numerically indexed, its range is the upper and lower bound of the array. Depending on the environment, a warning, a [[fatal errorexception]], or unpredictable behavior will occur if the program attempts to access an array element that is outside the range. In some [[programming languages]], such as [[C (programming language)|C]], arrays have a fixed lower bound (zero) and will contain data at each position up to the upper bound (so an array with 5 elements will have a range of 0 to 4). In others, such as [[PHP]], an array may have holes where no element is defined, and therefore an array with a range of 0 to 4 will have ''up to'' 5 elements (and a minimum of 2).
 
== Range as an alternative to iterator ==
Another meaning of ''range'' in computer science is an alternative to [[iterator]]. When used in this sense, range is defined as "a pair of begin/end iterators packed together".<ref name="itersmustgo">{{Cite web
| author = [[Andrei Alexandrescu]]
| author-link = Andrei Alexandrescu
| title = Iterators Must Go
| url = https://accu.org/content/conf2009/AndreiAlexandrescu_iterators-must-go.pdf
| date = 6 May 2009
| accessdateaccess-date = 29 July 2014
| publisher = BoostCon 2009
}}</ref> It is argued <ref name="itersmustgo" /> that "Ranges are a superior abstraction" (compared to iterators) for several reasons, including better safety.
 
In particular, such ranges are supported in [[C++20]],<ref>[https://en.cppreference.com/w/cpp/ranges cppreference]</ref> [[Boost C++ Libraries]]<ref>[http://www.boost.org/libs/range/index.html Boost.Range documentation]</ref> and the [[D (programming language)|D]] standard library.<ref>[http://dlang.org/phobos/std_range.html D Phobos Runtime Library std.range module]</ref>
 
== Range as a data type ==
==See also==
[[File:Range UML class.svg|thumb|A generic class representing a range, it contains a start property and a end property]]
*[[Interval (mathematics)|Interval]]
A data type for ranges can be implemented using [[Generic programming|generics]].
 
Example in [[C Sharp (programming language)|C#]].
<syntaxhighlight lang="csharp">
public record Range<T>(T Start, T End) where T : IComparable;
</syntaxhighlight>
 
Example in [[Kotlin (programming language)|Kotlin]].
<syntaxhighlight lang="kotlin">
data class Range<T: Comparable<T>>(val start: T, val end: T)
</syntaxhighlight>
 
Example in [[PHP]].
<syntaxhighlight lang="php">
readonly class Range<T> {
public function __construct(
public T $start,
public T $end,
) {}
}
</syntaxhighlight>
 
Example in [[Python (programming language)|Python]].
<syntaxhighlight lang="python">
from dataclasses import dataclass
 
@dataclass
class Range[T]:
start: T
end: T
</syntaxhighlight>
 
[[Rust (programming language)|Rust]] has a built-in range struct in the standard library in {{Mono|std::ops::Range}}.<ref>{{cite web |title=Range in std::ops - Rust |url=https://doc.rust-lang.org/std/ops/struct.Range.html |website=doc.rust-lang.org |access-date=17 October 2024}}</ref>
 
== Range as a operator ==
[[Rust (programming language)|Rust]] has the {{Mono|..}} and {{Mono|..{{=}}}} operators.
<syntaxhighlight lang="rust">
let heartwarming = "heartwarming!".to_string();
let warm = &heartwarming[5..9];
</syntaxhighlight>
 
[[Zig (programming language)|Zig]] also has the {{Mono|..}} operator.
<syntaxhighlight lang="zig">
// To iterate over consecutive integers, use the range syntax.
var sum: usize = 0;
for (0..5) |i| {
sum += i;
}
</syntaxhighlight>
 
As does [[C Sharp (programming language)|C#]],<ref>{{cite web|last1=BillWagner|access-date=2025-02-22|title=Explore ranges of data using indices and ranges - C#|url=https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes|date=14 November 2023|website=learn.microsoft.com}}</ref>
<syntaxhighlight lang="csharp">
string items[] = ["one","two","three","four"];
string first_three_items[] = items[0..2];
</syntaxhighlight>
[[F Sharp (programming_language)|F#]],<ref>{{cite web|access-date=2025-02-22|title=Range Operator|url=https://camilotk.github.io/fsharp-by-example/chapters/range-operator/|date=17 February 2023|website=F# by example}}</ref>
<syntaxhighlight lang="fsharp">
[1..4]
// Outputs: [1; 2; 3; 4]
</syntaxhighlight>
[[Kotlin (programming language)|Kotlin]],<ref>{{cite web|access-date=2025-02-22|title=Ranges and progressions - Kotlin|url=https://kotlinlang.org/docs/ranges.html#progression|website=Kotlin Help}}</ref>
<syntaxhighlight lang="kotlin">
for (i in 1..5) print(i)
</syntaxhighlight>
and [[Perl]].<ref>{{cite web|access-date=2025-02-22|title=perlop - Perl expressions: operators, precedence, string literals - Perldoc Browser|url=https://perldoc.perl.org/perlop#Range-Operators|website=perldoc.perl.org}}</ref>
<syntaxhighlight lang="perl">
for( 1..5) { print }
</syntaxhighlight>
 
[[Python (programming language)|Python]] and [[PHP (programming language)|PHP]] does not have any range operator but they do have a {{Code|range}} function.<ref>{{cite web |title=Built-in Functions |url=https://docs.python.org/3/library/functions.html#func-range |website=Python documentation |publisher=Python Software Foundation |access-date=17 December 2024 |language=en}}</ref><ref>{{cite web |title=PHP: range - Manual |url=https://www.php.net/manual/en/function.range.php |publisher=The PHP Documentation Group |access-date=17 December 2024}}</ref>
 
== See also ==
* [[Interval (mathematics)|Interval]]
 
== References ==
{{reflistReflist}}
 
{{DEFAULTSORT:Range (Computer Science)}}
[[Category:ProgrammingArticles constructswith example C Sharp code]]
[[Category:Articles with example PHP code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example Rust code]]
[[Category:Arrays]]
[[Category:Programming constructs]]