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