Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Fact check for Head/Tail #451

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions library/std/arrays.qs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace Microsoft.Quantum.Arrays {
///
/// # Example
/// ```qsharp
/// let evensCount = Count(x -> x % 2 == 0, [1, 3, 6, 7, 9]);
/// let evensCount = Count(x -> x % 2 == 0, [1, 3, 6, 7, 9]);
/// // evensCount is 1.
/// ```
function Count<'T>(predicate : ('T -> Bool), array : 'T[]) : Int {
Expand Down Expand Up @@ -371,7 +371,7 @@ namespace Microsoft.Quantum.Arrays {
function FlatMapped<'TInput, 'TOutput>(mapper : ('TInput -> 'TOutput[]), array : 'TInput[]) : 'TOutput[] {
mutable output = [];
for element in array {
set output += mapper(element);
set output += mapper(element);
}
output
}
Expand Down Expand Up @@ -482,6 +482,7 @@ namespace Microsoft.Quantum.Arrays {
/// # Output
/// The first element of the array.
function Head<'A> (array : 'A[]) : 'A {
Fact(Length(array) > 0, "Array must have at least 1 element");
array[0]
}

Expand Down Expand Up @@ -1041,7 +1042,7 @@ namespace Microsoft.Quantum.Arrays {
/// ```qsharp
/// let sortedArray = Sorted(LessThanOrEqualI, [3, 17, 11, -201, -11]);
/// ```
///
///
/// # Remarks
/// The function `comparison` is assumed to be transitive, such that
/// if `comparison(a, b)` and `comparison(b, c)`, then `comparison(a, c)`
Expand All @@ -1059,7 +1060,7 @@ namespace Microsoft.Quantum.Arrays {
// Sort each sublist, then merge them back into a single combined
// list and return.
SortedMerged(
comparison,
comparison,
Sorted(comparison, left),
Sorted(comparison, right)
)
Expand Down Expand Up @@ -1093,7 +1094,7 @@ namespace Microsoft.Quantum.Arrays {
/// array that match the given locations.
///
/// # Remarks
/// If `locations` contains repeated elements, the corresponding elements
/// If `locations` contains repeated elements, the corresponding elements
/// of `array` will likewise be repeated.
///
/// # Type Parameters
Expand All @@ -1109,9 +1110,9 @@ namespace Microsoft.Quantum.Arrays {
/// # Output
/// An array `out` of elements whose locations correspond to the subarray,
/// such that `out[index] == array[locations[index]]`.
///
///
/// # Example
///
///
/// ```qsharp
/// let array = [1, 2, 3, 4];
/// let permutation = Subarray([3, 0, 2, 1], array); // [4, 1, 3, 2]
Expand Down Expand Up @@ -1210,7 +1211,9 @@ namespace Microsoft.Quantum.Arrays {
/// # Output
/// The last element of the array.
function Tail<'A> (array : 'A[]) : 'A {
array[Length(array) - 1]
let size = Length(array);
Fact(size > 0, "Array must have at least 1 element");
array[size - 1]
}

/// # Summary
Expand Down