microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
46929774ee858b8564c66061bdb88a5dd9a815f5

Branches

Tags

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

Clone

HTTPS

Download ZIP

library/std/core.qs

105lines · modecode

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