From 8799911162de6e5f5b656edb93a55a7a679cae13 Mon Sep 17 00:00:00 2001 From: Saq Imtiaz Date: Fri, 4 Dec 2020 19:31:23 +0100 Subject: [PATCH] Reduce operator: second optional parameter instead of suffix (#5193) * Tweak reduce operator to use an optional second parameter instead of a suffix * Updated docs --- core/modules/filters/reduce.js | 2 +- .../filters/examples/reduce Operator (Examples).tid | 12 ++++++------ editions/tw5.com/tiddlers/filters/reduce.tid | 8 +++----- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/core/modules/filters/reduce.js b/core/modules/filters/reduce.js index 74d1131eb..fd5ccf568 100644 --- a/core/modules/filters/reduce.js +++ b/core/modules/filters/reduce.js @@ -23,7 +23,7 @@ exports.reduce = function(source,operator,options) { }); // Run the filter over each item var filterFn = options.wiki.compileFilter(operator.operand), - accumulator = operator.suffix || ""; + accumulator = operator.operands[1] || ""; for(var index=0; index` to act differently on the first item than the rest: <<.operator-example 3 "[tag[shopping]reduce]">> -Empty input, no suffix: +Empty input, no second parameter: <<.operator-example 4 "[tag[non-existent]reduce]">> @@ -50,12 +50,12 @@ Note how the output contains a single item with no text. This is not "empty outp <$macrocall $name=".tip" _=<> /> -Empty input, no suffix, followed by <<.op else>>: +Empty input, no second parameter, followed by <<.op else>>: <<.operator-example 5 "[tag[non-existent]reduceelse[0]]">> -Note how the output still contains a single item with no text: <<.op else>> did not trigger. If you want the value to be 0 when <<.op reduce>> has no items to process, you need to specify 0 as the initial value by passing it as a suffix to <<.op reduce>>. +Note how the output still contains a single item with no text: <<.op else>> did not trigger. If you want the value to be 0 when <<.op reduce>> has no items to process, you need to specify 0 as the initial value by passing it as a second parameter to <<.op reduce>>. -Empty input, suffix provided: +Empty input, second parameter provided: -<<.operator-example 6 "[tag[non-existent]reduce:0]">> +<<.operator-example 6 "[tag[non-existent]reduce,[0]]">> diff --git a/editions/tw5.com/tiddlers/filters/reduce.tid b/editions/tw5.com/tiddlers/filters/reduce.tid index d47955da3..debd28c9f 100644 --- a/editions/tw5.com/tiddlers/filters/reduce.tid +++ b/editions/tw5.com/tiddlers/filters/reduce.tid @@ -1,19 +1,17 @@ caption: reduce created: 20201004154131193 -modified: 20201006174749170 +modified: 20201204130601564 op-input: a [[selection of titles|Title Selection]] passed as input to the filter op-output: the final result of running the subfilter <<.place S>> -op-parameter: a [[filter expression|Filter Expression]] +op-parameter: a [[filter expression|Filter Expression]]. Optional second parameter for initial value for accumulator op-parameter-name: S op-purpose: apply a subfilter to each input title in turn, accumulating a single value -op-suffix: Initial value for accumulator -op-suffix-name: V tags: [[Filter Operators]] title: reduce Operator type: text/vnd.tiddlywiki \define reduce-tip() -The <<.op reduce>> operator will always produce output, even if its input was empty. If its input is empty, the output of <<.op reduce>> will be the initial value of the accumulator, i.e. the value of the suffix. One result of this fact is that the <<.op else>> operator will never be triggered if it follows a <<.op reduce>>. The "Empty input" examples show what happens when <<.op reduce>> receives no input. +The <<.op reduce>> operator will always produce output, even if its input was empty. If its input is empty, the output of <<.op reduce>> will be the initial value of the accumulator, i.e. the value of the second parameter. One result of this fact is that the <<.op else>> operator will never be triggered if it follows a <<.op reduce>>. The "Empty input" examples show what happens when <<.op reduce>> receives no input. \end <<.from-version "5.1.23">> The <<.op reduce>> operator runs a subfilter for each input title, passing the result of the previous subfilter run as a variable. The initial value of the accumulator can optionally be specified. It returns the result of the final subfilter run.