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)Context: {"scores": [{"name": "Alice", "score": 95}, {"name": "Bob", "score": 87}, {"name": "Carol", "score": 92}, {"name": "Dave", "score": 78}]}
→ ["Alice", "Carol", "Bob"]
Destructuring in Pipeline
Extract fields from each object while mapping.
users.map(({name, age}) => `${name} (${age})`)Context: {"users": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}
→ ["Alice (30)", "Bob (25)"]
Age Calculation from Birthdate
Compute age in years from an ISO 8601 date string.
dateDiff(parseDate(user.dob), now(), "years")Context: {"user": {"dob": "1990-06-15T00:00:00Z"}}
→ 35 (approximate, depends on current date)
Regex Extraction
Extract a branch name from a Git ref string.
match(event.ref, "refs/heads/(.+)") ?? "unknown"Context: {"event": {"ref": "refs/heads/main"}}
→ "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}Context: {"item": {"first": "Jane", "last": "Doe", "age": 28, "role": "admin"}}
→ {"first": "Jane", "last": "Doe", "age": null, "role": "admin", "fullName": "Jane Doe"}
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]Context: {"items": [{"active": true}, {"active": false}, {"active": true}, {"active": true}]}
→ [3, 1]
Null-Safe Address Resolution
Walk a deeply nested nullable path with a fallback.
user?.address?.city ?? user?.location?.city ?? "Unknown"Context: {"user": {"address": null, "location": {"city": "Paris"}}}
→ "Paris"
Circle Radius from Area
Compute radius from area using PI and sqrt.
let r = sqrt(area / PI); round(r * 100) / 100Context: {"area": 78.54}
→ 5.0
Type-Safe Numeric Sum
Filter to numeric fields only, then sum.
fields.filter(x => isNumber(x.value)).map(x => x.value).sum()Context: {"fields": [{"name": "price", "value": 29.99}, {"name": "tag", "value": "sale"}, {"name": "qty", "value": 3}]}
→ 32.99
Normalize Array to Object Lookup
Index a list by a unique key for O(1) access.
items.keyBy(x => x.id)Context: {"items": [{"id": "a", "val": 1}, {"id": "b", "val": 2}, {"id": "c", "val": 3}]}
→ {"a": {"id": "a", "val": 1}, "b": {"id": "b", "val": 2}, "c": {"id": "c", "val": 3}}
Strip Null Fields
Remove null-valued entries from an object.
user.entries().filter(([k, v]) => v != null) |> fromEntriesContext: {"user": {"name": "Alice", "age": null, "city": "NYC", "phone": null}}
→ {"name": "Alice", "city": "NYC"}
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`Context: {"event": {"date": "2024-01-01T00:00:00Z"}}
→ "2024-01-01: 804 days ago" (approximate)