Any function

Warning

This documentation refers to the Classic QDK, which has been replaced by the Modern QDK.

Please see https://aka.ms/qdk.api for the API documentation for the Modern QDK.

Namespace: Microsoft.Quantum.Arrays

Package: Microsoft.Quantum.Standard

Given an array and a predicate that is defined for the elements of the array, checks if at least one element of the array satisfies the predicate.

function Any<'T> (predicate : ('T -> Bool), array : 'T[]) : Bool

Input

predicate : 'T -> Bool

A function from 'T to Bool that is used to check elements.

array : 'T[]

An array of elements over 'T.

Output : Bool

A Bool value of the OR function of the predicate applied to all elements.

Type Parameters

'T

The type of array elements.

Example

open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Logical;

function IsThreePresent() : Bool {
    let arrayOfInts = [1, 2, 3, 4, 5];
    let is3Present = IsNumberPresentInArray(3, arrayOfInts);
    return is3Present;
}

function IsNumberPresentInArray(n : Int, array : Int[]) : Bool {
    return Any(EqualI(_, n), array);
}

Remarks

The function is defined for generic types, i.e., whenever we have an array 'T[] and a function predicate: 'T -> Bool we can produce a Bool value that indicates if some element satisfies predicate.