Examples
Advanced XPR expression patterns for real-world use cases.
Top-N Leaderboard
Sort by score descending, take the top 3, return names only.
scores.sortBy(x => x.score).reverse().take(3).map(x => x.name)→ ["Alice", "Carol", "Bob"]
Context
{"scores": [{"name": "Alice", "score": 95}, {"name": "Bob", "score": 87}, {"name": "Carol", "score": 92}, {"name": "Dave", "score": 78}]}Destructuring in Pipeline
Extract fields from each object while mapping.
users.map(({name, age}) => `${name} (${age})`)→ ["Alice (30)", "Bob (25)"]
Context
{"users": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}Age Calculation from Birthdate
Compute age in years from an ISO 8601 date string.
dateDiff(parseDate(user.dob), now(), "years")Age in years from birthdate (relative to today)
Context
{"user": {"dob": "1990-06-15T00:00:00Z"}}Regex Extraction
Extract a branch name from a Git ref string.
match(event.ref, "refs/heads/(.+)") ?? "unknown"→ "main"
Context
{"event": {"ref": "refs/heads/main"}}Object Reshape with Spread
Build a new object shape from an existing one, overriding specific fields.
{...item, fullName: item.first + " " + item.last, age: null}→ {"first": "Jane", "last": "Doe", "age": null, "role": "admin", "fullName": "Jane Doe"}
Context
{"item": {"first": "Jane", "last": "Doe", "age": 28, "role": "admin"}}Active/Inactive Partition
Split a list into two groups and count each.
let parts = items.partition(x => x.active); [parts[0].length, parts[1].length]→ [3, 1]
Context
{"items": [{"active": true}, {"active": false}, {"active": true}, {"active": true}]}Null-Safe Address Resolution
Walk a deeply nested nullable path with a fallback.
user?.address?.city ?? user?.location?.city ?? "Unknown"→ "Paris"
Context
{"user": {"address": null, "location": {"city": "Paris"}}}Circle Radius from Area
Compute radius from area using PI and sqrt.
Type-Safe Numeric Sum
Filter to numeric fields only, then sum.
fields.filter(x => isNumber(x.value)).map(x => x.value).sum()→ 32.99
Context
{"fields": [{"name": "price", "value": 29.99}, {"name": "tag", "value": "sale"}, {"name": "qty", "value": 3}]}Normalize Array to Object Lookup
Index a list by a unique key for O(1) access.
items.keyBy(x => x.id)→ {"a": {"id": "a", "val": 1}, "b": {"id": "b", "val": 2}, "c": {"id": "c", "val": 3}}
Context
{"items": [{"id": "a", "val": 1}, {"id": "b", "val": 2}, {"id": "c", "val": 3}]}Strip Null Fields
Remove null-valued entries from an object.
user.entries().filter(([k, v]) => v != null) |> fromEntries→ {"name": "Alice", "city": "NYC"}
Context
{"user": {"name": "Alice", "age": null, "city": "NYC", "phone": null}}Date Range Report
Format a date and compute days elapsed since an event.
let ts = parseDate(event.date); `${formatDate(ts, "yyyy-MM-dd")}: ${dateDiff(ts, now(), "days")} days ago`Formatted date with days elapsed since the event (relative to today)
Context
{"event": {"date": "2024-01-01T00:00:00Z"}}