Combinable — Combining Values
1.74 kB minified + gzipped
Each time window reports its own request count and warning messages. A report over four windows must total the counts, concatenate the lists, and still produce zero requests and no warnings when the list is empty. Writers who loop by hand repeat the starting values at each call site, and one of them starts the count at one.
Core Behavior
Section titled “Core Behavior”Combinable<A> is a pair: an empty value and combine(next)(accumulated), which merges one more
value into the running result.
combinereceives the new item first and the accumulated value second;combineAllstarts fromemptyand processes items in input order.structderives a rule from field rules, building an empty record and a fresh record for every combination.maybetreatsNoneas the identity and combines the inner values when both sides areSome.- Built-in identities are
""forstring,0forsum,1forproduct,trueforall,falseforany, and[]forarray.
Reach for Combinable when a collection must fold into one value under defined merge rules. When
two records merge by shallow overwrite, Rec provides merge and mergeWith. The
choosing guide compares the modules that share this problem.
Example: Traffic Summaries
Section titled “Example: Traffic Summaries”A service records request counts and warning messages for each time window. The report combines every window into one summary and has to return a useful summary when the list is empty.
combineAll starts from the rule’s empty value, then passes each item and the current accumulation
to combine:
flowchart TD
Empty["Start from empty"] --> More{"Another window?"}
More -->|Yes| Add["Combine window into summary"]
Add --> More
More -->|No| Summary["Return summary"]
Combining Window Summaries
Section titled “Combining Window Summaries”array concatenates without mutating either input, and struct applies each field rule and creates
a new record. The empty list returns the struct’s empty record, with zero requests and no warnings.
Overriding Window Fields
Section titled “Overriding Window Fields”A report sometimes needs the latest window to override earlier fields instead of accumulating them.
Rec.merge performs a shallow merge with right precedence:
The Rec guide covers key selection and collision handling with mergeWith.
Core Operations
Section titled “Core Operations”Creating Rules
Section titled “Creating Rules”| Signature | What it does | Identity |
|---|---|---|
Combinable.string |
Concatenates strings. | "" |
Combinable.sum |
Adds numbers. | 0 |
Combinable.product |
Multiplies numbers. | 1 |
Combinable.all |
Combines booleans with AND. | true |
Combinable.any |
Combines booleans with OR. | false |
Combinable.array<A>() |
Concatenates arrays. | [] |
Combinable.maybe(inner) |
Lifts an inner rule to optional values. | None |
Building Structured Rules
Section titled “Building Structured Rules”| Signature | What it does | Result |
|---|---|---|
Combinable.struct(fields) |
Derives a record rule from field rules. | Combinable<R> |
Running a Fold
Section titled “Running a Fold”| Signature | What it does | Result |
|---|---|---|
Combinable.combineAll(rule) |
Folds an array in input order. | A; the rule’s empty value for []. |
Failure Behavior and Edge Cases
Section titled “Failure Behavior and Edge Cases”combine receives the next item first. For string and array, reversing the arguments changes
the result:
combineAll returns empty for an empty array and never calls combine.
struct captures its rule keys when it is built and writes only those keys into the result. Fields
on an input that the rule does not list are absent from the combined record; the rule does not start
from the first item.
maybe treats None on either side as the identity, so None combined with None returns None.
Two Some values combine the inner values and wrap the result in Some.
empty and combine are not checked against each other. A custom rule whose empty is not an
identity produces a different result for an empty array than for the same value combined explicitly;
verify both argument orders when defining one.