# single

Source: https://taxilang.org/docs/language/stdlib/single

**Full name:** `taxi.stdlib.single`

## Signature

```taxi
declare extension function <T> single(collection:T[], callback: (T) -> Boolean):T
```

## Description

Returns the first item from a collection that matches the provided predicate. Throws an error if no matching item is found.

This function is useful when you need to find a specific element in a collection based on a condition.

```taxi
// Find a product by its SKU code
find { inventory: Inventory } as {
   appleMacbook: inventory.products.single((product) -> product.sku == "MB-PRO-2023")
}
```

## Examples

Finds the single product matching the given SKU. Throws if no match or more than one match is found.

Schema:

```taxi
model Product {
   name: ProductName inherits String
   sku: SKU inherits String
   price: Price inherits Decimal
}
model Inventory {
   products: Product[]
}
```

Query:

```taxi
given {
   inventory: Inventory = {
      products: [
         { name: 'Laptop', sku: 'LT-001', price: 1200.00 },
         { name: 'Smartphone', sku: 'SP-002', price: 999.99 },
         { name: 'Tablet', sku: 'TB-003', price: 499.50 }
      ]
   }
}
find {
   smartphoneDetails: Product = inventory.products.single((product: Product) -> product.sku == 'SP-002')
}
```

Result:

```json
{
  "smartphoneDetails": {
    "name": "Smartphone",
    "sku": "SP-002",
    "price": 999.99
  }
}
```
