microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
661e9fc335e130cfeda06375919902ae5ffacf13

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

library/std/core.qs

99lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4namespace Microsoft.Quantum.Core {
5 /// # Summary
6 /// Returns the number of elements in an array.
7 ///
8 /// # Input
9 /// ## a
10 /// Input array.
11 ///
12 /// # Output
13 /// The total count of elements in an array.
14 ///
15 function Length<'T>(a : 'T[]) : Int {
16 a::Length
17 }
18
19 /// # Summary
20 /// Returns the defined start value of the given range.
21 ///
22 /// # Input
23 /// ## r
24 /// Input range.
25 ///
26 /// # Output
27 /// The defined start value of the given range.
28 ///
29 /// # Remarks
30 /// A range expression's first element is `start`,
31 /// its second element is `start+step`, third element is `start+step+step`, etc.,
32 /// until `end` is passed.
33 ///
34 /// Note that the defined start value of a range is the same as the first element of the sequence,
35 /// unless the range specifies an empty sequence (for example, 2 .. 1).
36 function RangeStart(r : Range) : Int {
37 r::Start
38 }
39
40
41 /// # Summary
42 /// Returns the defined end value of the given range,
43 /// which is not necessarily the last element in the sequence.
44 ///
45 /// # Input
46 /// ## r
47 /// Input range.
48 ///
49 /// # Output
50 /// The defined end value of the given range.
51 ///
52 /// # Remarks
53 /// A range expression's first element is `start`,
54 /// its second element is `start+step`, third element is `start+step+step`, etc.,
55 /// until `end` is passed.
56 ///
57 /// Note that the defined end value of a range can differ from the last element in the sequence specified by the range;
58 /// for example, in a range 0 .. 2 .. 5 the last element is 4 but the end value is 5.
59 function RangeEnd(r : Range) : Int {
60 r::End
61 }
62
63
64 /// # Summary
65 /// Returns the integer that specifies how the next value of a range is calculated.
66 ///
67 /// # Input
68 /// ## r
69 /// Input range.
70 ///
71 /// # Output
72 /// The defined step value of the given range.
73 ///
74 /// # Remarks
75 /// A range expression's first element is `start`,
76 /// its second element is `start+step`, third element is `start+step+step`, etc.,
77 /// until `end` is passed.
78 function RangeStep(r : Range) : Int {
79 r::Step
80 }
81
82 /// # Summary
83 /// Returns a new range which is the reverse of the input range.
84 ///
85 /// # Input
86 /// ## r
87 /// Input range.
88 ///
89 /// # Output
90 /// A new range that is the reverse of the given range.
91 ///
92 /// # Remarks
93 /// Note that the reverse of a range is not simply `end`..`-step`..`start`, because
94 /// the actual last element of a range may not be the same as `end`.
95 function RangeReverse(r : Range) : Range {
96 let start = r::Start + ((r::End - r::Start) / r::Step) * r::Step;
97 start..-r::Step..r::Start
98 }
99}
100