_.reduce
Fold a List Into One Value — but Bring Your Own Memo
- list_or_dict
- memo
- *
_.reduce is the fold node: it boils a whole list down to a single value by repeatedly applying a combine function. Sum a list of numbers, concatenate a list of strings, build a tally. It's the most powerful node in the underscore family - and the least beginner-friendly, because it needs two things you can't always conjure in a graph: an initial value and a callable.
How it works
The iteration keeps an accumulator (the memo) and calls your combine function once per element: memo = fn(memo, value, index), then hands the accumulated result to the next step. That's classic reduce - the description even calls out the four-argument iteratee (memo, value, index, list).
Two inputs:
list_or_dict- the collection (or a_.CHAIN)memo- the starting value of the accumulator
Output: the folded result.
The traps - both verified
First, the memo. If you leave memo unplugged, this port doesn't do what underscore.js does. Instead of using the first element as the initial accumulator, it defaults the memo to an empty list and calls your function with it: fn([], first_value, 0). For anything numeric, that's [] + 1 → crash. I hit exactly that. So: always supply a memo. Sum starting at 0, concatenation starting at "", tally starting at {}.
Second, the callable. The combine function has to come from somewhere. There's no widget for it - you need an upstream node that emits a Python function, like a lambda/function node from another pack or this pack's underscore function export. And the function must accept three arguments (memo, value, index), because that's what the loop passes. A two-arg lambda that works fine in plain Python will throw takes 2 positional arguments but 3 were given.
When it's worth the ceremony
Reduce earns its keep when the fold is genuinely the right shape: summing scores, building a count histogram from a list, flattening results into a single string. If you're already running function nodes, it slots right in. If you're not, the family's _.pluck, _.min/_.max and list utilities cover most "combine a list" cases with far less setup - you don't need reduce for a simple sum when there are dedicated nodes for it in logic packs.
Installing
Part of comfy-ovum. ComfyUI Manager → search comfy-ovum → Install, or:
cd ComfyUI/custom_nodes
git clone https://github.com/sfinktah/comfy-ovum
Restart ComfyUI. No models, no extra pip packages for the underscore nodes - the port is bundled in the repo.
The bottom line
Powerful folding, but it demands a memo and a three-argument callable. Supply both and it works; forget the memo and it crashes on the first element.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| list_or_dictopt | * | Primary input object (expected collection). You can still pass any JSON-serializable value. Also accepts _.CHAIN to continue chaining. | |
| memoopt | * | memo: JSON allowed for arrays/objects where applicable. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| * | * | — |