singleBy

Full name: taxi.stdlib.singleBy

Signature

declare extension function <T,A> singleBy(collection:T[], groupingFunction: (T) -> A, searchValue: A):T

Description

Returns the item from a collection where the value returned by the grouping function matches the search value.

Similar to single(), but results are first grouped by the selector function, and these results are cached to improve future performance when querying the same collection multiple times.

// Find a product by its SKU code using a cached search
find { inventory: Inventory } as {
   appleMacbook: inventory.products.singleBy((product) -> product.sku, "MB-PRO-2023")
}

Examples

Looks up products by SKU using a cached index. Faster than single() when querying the same collection multiple times.

Try it out

Schema
Query Plan
Play with this snippet by editing it here, or edit it on Taxi Playground
Result
Query failed
Next
Welcome to Taxi