mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-08-23 10:06:44 +00:00
![Saq Imtiaz](/assets/img/avatar_default.png)
* Add :sort filter run prefix, docs and tests. Also extended .utils.makeCompareFunction with a flag for caseSensitivity. * Documentation updates * Move case sensitivity handling entirely to utils method so it is reusable
50 lines
2.9 KiB
Plaintext
50 lines
2.9 KiB
Plaintext
created: 20201117073343969
|
|
modified: 20210428084013109
|
|
tags: [[Filter Syntax]] [[Filter Run Prefix Examples]]
|
|
title: Filter Run Prefix (Examples)
|
|
type: text/vnd.tiddlywiki
|
|
|
|
!! Difference between `+` and `:intersection`
|
|
|
|
The `+` prefix should be thought of as an "AND" in formal logic, e.g. "give me all titles that satisfy condition A ''and'' condition B". But it's not suitable for all cases; if condition B uses a filter operator that replaces its input, then it will be difficult to use the `+` prefix. For example, if you wanted to find out what tags two tiddlers have in common, you might try to write a filter expression like:
|
|
|
|
* <$link to="tags Operator">`[[field Operator]tags[]] +[[compare Operator]tags[]]`</$link>
|
|
|
|
But that won't work, because the second filter run will end up throwing away its input and replacing it with an input consisting of the single title `[[compare Operator]]`. So the result you'd get from that filter expression would be just the tags of the `compare Operator` tiddler.
|
|
|
|
For cases like this, the `:intersection` prefix is what you need. It takes the filter output so far, //sets it aside//, and starts the next filter run with all tiddler titles as input. Then once the latest filter run has completed, it takes the latest output, compares it to the set-aside output, and produces a new output that contains only titles that appeared in both the set-aside output and the latest output. So to get only the tags that the `field Operator` and `compare Operator` tiddlers have in common, you would write a filter expression like this:
|
|
|
|
* <$link to="tags Operator">`[[field Operator]tags[]] :intersection[[compare Operator]tags[]]`</$link>
|
|
|
|
The following examples use the [[field Operator]] and [[compare Operator]] tiddlers and their respective tags.
|
|
|
|
<<.operator-example 1 "[[field Operator]tags[]]">>
|
|
<<.operator-example 2 "[[compare Operator]tags[]]">>
|
|
|
|
<<.operator-example 3 "[[field Operator]tags[]] +[[compare Operator]tags[]]" """The "field Operator" tiddler's tags are lost, so only the "compare Operator" tiddler's tags are returned""">>
|
|
|
|
<<.operator-example 4 "[[field Operator]tags[]] :intersection[[compare Operator]tags[]]" """Returns the tags that both tiddlers have in common""">>
|
|
|
|
!! `:reduce` examples
|
|
|
|
```
|
|
[tag[shopping]] :reduce[get[quantity]add<accumulator>]
|
|
```
|
|
|
|
is equivalent to:
|
|
|
|
```
|
|
\define num-items() [get[quantity]add<accumulator>]
|
|
|
|
[tag[shopping]reduce<num-items>]
|
|
```
|
|
|
|
Specifying a default value when input is empty:
|
|
|
|
`[tag[non-existent]] :reduce[get[price]multiply{!!quantity}add<accumulator>] :else[[0]]`
|
|
|
|
<$macrocall $name=".tip" _="""Unlike the [[reduce Operator]], the `:reduce` prefix cannot specify an initial value for the accumulator, so its initial value will always be empty (which is treated as 0 by mathematical operators). So `=1 =2 =3 :reduce[multiply<accumulator>]` will produce 0, not 6. If you need to specify an initial accumulator value, use the [[reduce Operator]]."""/>
|
|
|
|
!! `:sort` examples
|
|
|
|
See [[Sort Filter Run Prefix (Examples)]] |