Exactly quantifier

Learn how to check that a number of items in a collection satisfy a condition.

This quantifier checks if an exact number of elements in a collection satisfy a given condition.

Syntax

This quantifier has the following syntax:

exactly /*<number>*/ of any /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/
Try in Playground

Either satisfy or satisfies can be used interchangeably:

exactly /*<number>*/ of any /*<item>*/ in /*<collection>*/ satisfy /*<condition>*/
Try in Playground

You can specify whether you want to consider only distinct elements:

exactly /*<number>*/ of any distinct /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/
Try in Playground

You can also specify a name to refer to the index of the element in the collection:

exactly /*<number>*/ of any /*<item>*/ in /*<collection>*/ indexed by /*<index>*/ satisfies /*<condition>*/
Try in Playground

You must replace the /*<item>*/ and /*<index>*/ placeholders with a valid identifier. These variables will refer to the element and its index in the collection, as exemplified in the following section.

Examples

Here is an example that checks if a collection contains exactly two even numbers:

exactly 2 of any number in [2, 4, 6] satisfies number mod 2 is 0 // false
Try in Playground

The following example checks if the first two products added to the cart are below $50:

exactly 2 of any item in cart indexed by index satisfies item's price < 50 and index < 2
Try in Playground

Lastly, this example checks if a user's shopping cart contains exactly two distinct products from the "Electronics" category:

exactly 2 of any distinct item in cart satisfies item's category is "Electronics"
Try in Playground