Range (computer programming): Difference between revisions

Content deleted Content added
mNo edit summary
 
(11 intermediate revisions by 6 users not shown)
Line 32:
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="csharpkotlin">
data class Range<T: Comparable<T>>(val start: T, val end: T)
</syntaxhighlight>
Line 60:
</syntaxhighlight>
 
[[Rust (programming language)|Rust]] havehas 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]] havehas the {{Mono|..}} and {{Mono|..{{=}}}} operators.
<syntaxhighlight lang="rust">
let heartwarming = "heartwarming!".to_string();
Line 69:
</syntaxhighlight>
 
[[Zig (programming language)|Zig]] also havehas the {{Mono|..}} operator.
<syntaxhighlight lang="zig">
// To iterate over consecutive integers, use the range syntax.
Line 77:
}
</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 ==