created: 20211124151912931
modified: 20211124170117511
tags: [[Filter Syntax]] [[Filter Run Prefix]]
title: Reduce Filter Run Prefix
type: text/vnd.tiddlywiki

<<.from-version "5.1.23">>

|''purpose'' |replaces all filter output so far with a single item by repeatedly applying a filter run to each input title |
|''input'' |all titles from previous filter runs |
|''output''|the accumulated single item |

Each input title from previous runs is passed to this run in turn. The result of each previous call to this run is made available in the next call via the variable named "accumulator". The result of the last call to this run is returned as the output.  A typical use is to add up the values in a given field of each input title.

The following variables are available within the filter run:

* ''accumulator'' - the result of the previous filter run
* ''currentTiddler'' - the input title
* ''..currentTiddler'' - the value of the variable `currentTiddler` outside the filter run. <<.from-version "5.2.0">>
* ''index'' - the numeric index of the current list item (with zero being the first item in the list)
* ''revIndex'' - the reverse numeric index of the current list item (with zero being the last item in the list)
* ''length'' - the total length of the input list

<<.tip "Compare named filter run prefix `:reduce` with [[reduce Operator]] which is used to flatten a list of items down to a single item by repeatedly applying a subfilter.">> 

```
[tag[shopping]] :reduce[get[quantity]add<accumulator>]
```

is equivalent to:

```
\define num-items() [get[quantity]add<accumulator>]

[tag[shopping]reduce<num-items>]
```

[[Examples|Reduce Filter Run Prefix (Examples)]]