How make filter to sum up field values?

I have a bunch of fields in the current tiddler. Each field has a numeric value. I can filter out to get only my custom field names but now I a total sum of their values. Can’t quite figure out the filter for this:

[all[current]fields:exclude<xfields>] ...and now?...

Thanks!

Try this:
[all[current]fields:exclude<xfields>] :reduce[<..currentTiddler>get<currentTiddler>add<accumulator>]

4 Likes

@tw-FRed - thank you! I’m really glad I asked because I would absolutely not have solved that!

Here’s another way, using :map[...] and sum[]

<$vars xfields="title created modified text tags">
{{{ [<currentTiddler>fields:exclude<xfields>] :map[<..currentTiddler>get<currentTiddler>] +[sum[]] }}}

-e

1 Like

Excellent. That one is somewhat more intuitive. Thanks Eric.

Meta Discussion:

This discusses filter operators that do not [yet] exist.

This form [<..currentTiddler>get<currentTiddler>] seems to me to be so fundamental to these and related filter that it may deserve its own operator especially for handling fields. Or perhaps we can improve this?

I find the use of MAP and REDUCE may help those who have big data and other experience but they are not average user friendly at all. Are they so spectacularly useful to deserve the jargon?

Perhaps if we could set the result to a variable name for use within the filter it would make it easier/clearer?

[<currentTiddler>fields:exclude<xfields>setvar[fieldname]] +[all[current]get<fieldname>] +[sum[]]

The point is the multiple values returned are returned as fieldname not currentTiddler meaning the original currentTiddler/all[current] is usable and not given the new values.

Perhaps even the all[current] could have an alternative? eg here[]

[here[]get<fieldname>]

Arguably we could use storyTiddler;

[<storyTiddler>get<fieldname>]

For which an alias could be set in the story iteration to <<here>> or even '<<~>>` which in dictionaries “~” refers to the word it is describing.

thus

[<~>get<fieldname>]

  • setvar could also be “output[varname]”.

thus

[<currentTiddler>fields:exclude<xfields>setvar[fieldname]] +[<~>get<fieldname>] +[sum[]]

Such ideas would help people to not need to read into a filter the meaning of currentTiddler depending on where it is in the filter, because they can name the variable which refers to the output of a run for use in subsequent runs or filter logic. We get to “name” the results supporting the sematic readability of the filter. And in my suggestion <~> will always refer to the real currentTiddler.

1 Like