1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-01 09:43:16 +00:00
TiddlyWiki5/editions/tw5.com/tiddlers/filters/examples/reduce Operator (Examples).tid
Robin Munn f60d0ef109
reduce and :reduce handle empty input identically (#5255)
Fixes #5246. Now the reduce operator and :reduce filter run prefix will
both return empty output when their input is empty, so that both can be
chained together with the else operator or :else prefix.
2020-12-11 10:07:52 +00:00

39 lines
1.7 KiB
Plaintext

created: 20201004154413968
modified: 20201204131137081
tags: [[Operator Examples]] [[reduce Operator]]
title: reduce Operator (Examples)
type: text/vnd.tiddlywiki
\define add-price() [get[price]multiply{!!quantity}add<accumulator>]
\define num-items() [get[quantity]add<accumulator>]
\define join-with-commas() [<index>compare:number:gt[0]then<accumulator>addsuffix[, ]addsuffix<currentTiddler>else<currentTiddler>]
\define multiply-input() [multiply<accumulator>]
\define display-variable(name)
''<$text text=<<__name__>>/>'': <code><$text text={{{ [<__name__>getvariable[]] }}}/></code>
\end
These examples use the following predefined variables:
* <<display-variable add-price>>
* <<display-variable num-items>>
* <<display-variable join-with-commas>>
* <<display-variable multiply-input>>
They also use the following data tiddlers:
<ul>
<$list filter="[tag[shopping]!has[draft.of]]">
<li>
''<$link><$text text=<<currentTiddler>>/></$link>'' quantity: <$text text={{!!quantity}}/>, price: <$text text={{!!price}}/>
</li>
</$list>
</ul>
<<.operator-example 1 "[tag[shopping]reduce<num-items>]">>
<<.operator-example 2 "[tag[shopping]reduce<add-price>]">>
<<.operator-example 3 "[tag[shopping]reduce<join-with-commas>]" "Uses `<index>` to act differently on the first item than the rest">>
<<.operator-example 4 "[tag[non-existent]reduce<add-price>]" "Empty input produces empty output">>
<<.operator-example 5 "[tag[non-existent]reduce<add-price>else[0]]" "Use `else` to ensure output if input was empty">>
<<.operator-example 6 "=1 =2 =3 +[reduce<multiply-input>]" "Empty initial value is treated as 0 by mathematical operators">>
<<.operator-example 7 "=1 =2 =3 +[reduce<multiply-input>,[1]]" "Setting initial value is sometimes necessary for correct results">>