Array Comprehensions¶
Array comprehensions build arrays from other collections using a FOR clause inside a literal【F:language/src/test/kotlin/v2/ebnf.txt†L122-L124】. They’re handy for concise reshaping without an explicit loop.
Basic comprehension¶
squares becomes [1, 4, 9].
With filtering¶
evens becomes [2, 4].
Building objects inline¶
LET items = [
{ name: "pen", qty: 2, price: 9.5 },
{ name: "pad", qty: 1, price: 12.0 },
{ name: "bag", qty: 3, price: 4.75 }
];
LET summaries = [
FOR (item IN items)
IF item.qty > 1
=> { name: item.name, subtotal: item.qty * item.price }
];
OUTPUT { summaries: summaries };
Trailing form¶
You can trail the FOR EACH after the expression:
Try it¶
Paste any of the snippets into the playground and run. Start from the collection-transforms example to compare comprehensions with MAP/FILTER/REDUCE.