microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/num2-sim

Branches

Tags

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

Clone

HTTPS

Download ZIP

library/core/core.qs

61lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4namespace Std.Core {
5 /// # Summary
6 /// Returns the number of elements in the input array `a`.
7 ///
8 /// # Input
9 /// ## a
10 /// Input array.
11 ///
12 /// # Output
13 /// The total number of elements in the input array `a`.
14 ///
15 /// # Example
16 /// ```qsharp
17 /// Message($"{ Length([0, 0, 0]) }"); // Prints 3
18 /// ```
19 function Length<'T>(a : 'T[]) : Int {
20 body intrinsic;
21 }
22
23 /// # Summary
24 /// Creates an array of given `length` with all elements equal to given
25 /// `value`. `length` must be a non-negative integer.
26 ///
27 /// # Description
28 /// Use this function to create an array of length `length` where each
29 /// element is equal to `value`. This way of creating an array is preferred
30 /// over other methods if all elements of the array must be the same and
31 /// the length is known upfront.
32 ///
33 /// # Input
34 /// ## value
35 /// The value of each element of the new array.
36 /// ## length
37 /// Length of the new array.
38 ///
39 /// # Output
40 /// A new array of length `length`, such that every element is `value`.
41 ///
42 /// # Example
43 /// ```qsharp
44 /// // Create an array of 3 Boolean values, each equal to `true`
45 /// let array = Repeated(true, 3);
46 /// ```
47 function Repeated<'T>(value : 'T, length : Int) : 'T[] {
48 if length < 0 {
49 fail "Length must be a non-negative integer";
50 }
51
52 mutable output = [];
53 for _ in 1..length {
54 set output += [value];
55 }
56
57 output
58 }
59
60 export Length, Repeated;
61}
62