# Quantifiers

Learn how to express numerical and logical quantifiers in CQL.

Quantifiers allow you to determine whether all, some, or none of the elements in a collection satisfy a particular condition. You can think of them as a for loop that evaluates each element to see if it satisfies a given predicate.

These are some examples of questions you can answer with quantifiers:

- Are all items in the shopping cart over $20?
- How many sports-related interests does this user have?
- Does the shopping cart contain items that are out of stock?

Depending on the question you want to answer, you may need to use different quantifiers. These are classified as either numerical or logical, according to the nature of the question they answer.

The following table summarizes the quantifiers available in CQL:

Quantifier | Description |
---|---|

No | Checks if none of the elements in a collection satisfy a given condition. |

Some | Checks if at least one of the elements in a collection satisfies a given condition. |

Every | Checks if all elements in a collection satisfy a given condition. |

Not every | Checks if not all elements in a collection satisfy a given condition. |

At least | Checks if at least a certain number of elements in a collection satisfy a given condition. |

At most | Checks if no more than a specified number of elements in a collection satisfy a given condition. |

Exactly | Checks if an exact number of elements in a collection satisfy a given condition. |

## Logical quantifiers

Logical quantifiers answer questions that revolve around whether a predicate is satisfied by the elements in a collection. They do not focus on the number of elements but rather on the existence or absence of such elements.

For example, you can use logical quantifiers to check whether all items in the shopping cart are electronics or whether the user has applied a discount to any of the items in the cart.

The following sections introduce the different logical quantifiers available and how to use them to answer these types of questions.

### No

The *no* quantifier checks whether none of the elements in a collection satisfy a given condition.

Note that this quantifier always returns true for empty collections. This is known in logic as vacuous truth, meaning that the quantifier is satisfied because no elements in the collection do not satisfy the condition.

Technically, yes. Since there are no unicorns in the room, there's no unicorn that isn't shining. Logic is fun, isn't it?

#### Syntax

This quantifier has the following syntax:

no /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

Either *satisfy* or *satisfies* can be used interchangeably:

no /*<item>*/ in /*<collection>*/ satisfy /*<condition>*/

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

no /*<item>*/ in /*<collection>*/ indexed by /*<index>*/ satisfies /*<condition>*/

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 color is not present in a collection:

no color in ["red", "green", "blue"] satisfies color is "yellow" // true

The following example is a bit more advanced, as it uses the indexed by clause to refer to the index of the element in the collection:

no item in cart indexed by index satisfies item's brand is "Apple" and index >= 1

In the above example, the item variable refers to the element in the collection, while the index variable refers to the index of the element in the collection. The quantifiers then determine whether the user has added Apple products as the second or subsequent item in the cart.

### Some

The *some* quantifier checks whether at least one element in a collection satisfies a given condition.

Note that the result is always false for empty collections. This is because there is not a single element in the collection that satisfies the condition.

#### Syntax

This quantifier has the following syntax:

some /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

Either *satisfy* or *satisfies* can be used interchangeably:

some /*<item>*/ in /*<collection>*/ satisfy /*<condition>*/

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

some /*<item>*/ in /*<collection>*/ indexed by /*<index>*/ satisfies /*<condition>*/

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 check whether a color is present in a collection:

some color in ["red", "green", "blue"] satisfies color is "green" // true

The following example shows how to use the index of the element in the condition:

some item in cart indexed by index satisfies item's brand is "Apple" and index < 3

In the above example, it checks whether the user has added Apple products among the first three items in the cart.

### Every

The *every* quantifier checks whether all elements in a collection satisfy a given condition.

Note that this quantifier always returns true for empty collections. This is known in logic as vacuous truth, meaning that the quantifier is satisfied because there are no elements in the collection that do not satisfy the condition.

This might sound funny, but since there are no unicorns in the room, there's no unicorn that isn't shining. So, technically, they all are!

#### Syntax

This quantifier has the following syntax:

every /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

Either *satisfy* or *satisfies* can be used interchangeably:

every /*<item>*/ in /*<collection>*/ satisfy /*<condition>*/

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

every /*<item>*/ in /*<collection>*/ indexed by /*<index>*/ satisfies /*<condition>*/

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 a basic example that checks if all numbers in a collection are even:

every number in [2, 4, 6] satisfies number mod 2 is 0 // true

The following example shows how to use the index of the element in the condition:

every item in cart indexed by index satisfies item's brand is "Apple" and index >= 1

In the above example, it checks whether the user only added Apple products as the second or subsequent item in the cart.

### Not every

The *not every* quantifier checks whether at least one element in a collection does not satisfy a given condition.

Note that the result is always false for empty collections. This is because there is not a single element in the collection that does not satisfy the condition.

#### Syntax

This quantifier has the following syntax:

not every /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

Either *satisfy* or *satisfies* can be used interchangeably:

not every /*<item>*/ in /*<collection>*/ satisfy /*<condition>*/

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

not every /*<item>*/ in /*<collection>*/ indexed by /*<index>*/ satisfies /*<condition>*/

#### Examples

Here is an example that checks if there is at least one number in a collection that is not even:

not every number in [2, 4, 6] satisfies number mod 2 is 0 // false

The following example shows how to use the index of the element in the condition:

not every item in cart indexed by index satisfies item's brand is "Apple" and index >= 1

In the above example, it checks whether the user added at least one non-Apple product as the second or subsequent item in the cart.

## Numerical quantifiers

Numerical quantifiers answer questions directly related to the number of elements in a collection that satisfy a given condition.

Because these quantifiers consider the number of elements in a collection, they allow you to specify whether you want to consider only distinct elements.

For example, you can use a numerical quantifier to check whether a shopping cart contains at least 3 different products with a price greater than $100, or whether a user has at least 2 interests related to a specific topic.

The following sections introduce the different numerical quantifiers available and how to use them to answer these types of questions.

### At least

The *at least* quantifier checks whether at least a given number of elements in a collection satisfies a given condition.

#### Syntax

This quantifier has the following syntax:

at least /*<number>*/ of any /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

Either *satisfy* or *satisfies* can be used interchangeably:

at least /*<number>*/ of any /*<item>*/ in /*<collection>*/ satisfy /*<condition>*/

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

at least /*<number>*/ of any distinct /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

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

at least /*<number>*/ of any /*<item>*/ in /*<collection>*/ indexed by /*<index>*/ satisfies /*<condition>*/

#### Examples

Here is an example that checks if at least two numbers in a collection are even:

at least 2 of any number in [2, 4, 6] satisfies number mod 2 is 0 // true

The following example checks whether the user added at least two Apple products after adding the first item to the cart:

at least 2 of any item in cart indexed by index satisfies item's brand is "Apple" and index >= 1

Lastly, this example checks if a user has at least two distinct products of the category "Electronics" in their cart:

at least 2 of any distinct item in cart satisfies item's category is "Electronics"

### At most

The *at most* quantifier checks whether a given condition is satisfied by no more than a specified number of elements in a collection.

#### Syntax

This quantifier has the following syntax:

at most /*<number>*/ of any /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

Either *satisfy* or *satisfies* can be used interchangeably:

at most /*<number>*/ of any /*<item>*/ in /*<collection>*/ satisfy /*<condition>*/

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

at most /*<number>*/ of any distinct /*<item>*/ in /*<collection>*/ satisfies /*<condition>*/

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

at most /*<number>*/ of any /*<item>*/ in /*<collection>*/ indexed by /*<index>*/ satisfies /*<condition>*/

#### Examples

Here is an example that check whether at most two numbers in a collection are even:

at most 2 of any number in [2, 4, 6] satisfies number mod 2 is 0 // true

The following example checks whether the user added no more than two products over $50 after adding the first item to the cart:

at most 2 of any item in cart indexed by index satisfies item's price > 50 and index >= 1

Lastly, this example checks if a user has at most two distinct products of the category "Electronics" in their cart:

at most 2 of any distinct item in cart satisfies item's category is "Electronics"

### Exactly

The *exactly* 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>*/

Either *satisfy* or *satisfies* can be used interchangeably:

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

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

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

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>*/

#### 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

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

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"