Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce let filter run prefix for assigning a filter run result to a variable #8972

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

Jermolene
Copy link
Member

@Jermolene Jermolene commented Mar 7, 2025

This PR introduces a new filter run prefix :let that assigns the result of the filter run to a variable that is made available for the remaining filter runs of the filter expression.

It solves the problem that otherwise it is impossible to compute values for filter operator parameters; parameters can only be a literal string, text reference or variable reference. The :let filter run prefix provides a solution:

:let:index[my.custom.function<param>,[2]] [<data>jsonget[top],<index>]

Note that within the filter run the all[] operator (with a blank parameter) will return all the input titles received by the :let filter run prefix, not the input titles received by the overall filter expression. This permits the results of other filter run prefixes to be assigned to a variable. For following example first sorts all the input tiddlers by length, and then uses the length of the shortest title as the second index of a JSON lookup:

[all[tiddlers]] :sort:number[get[text]length[]] :let:index[all[]] [<data>jsonget[top],<index>]

This PR also introduces the ability for variables to contain list of results, not just single strings. They can be assigned with the :let filter run prefix, or the new <$letlist> widget. The full list of values can be retrieved with the new varlist operator; in other contexts only the first item in the list is used as the variable value.

Note that this PR is intended to be a more generic replacement for the experiment :apply filter operator that is currently part of #8702 - see the code and the docs

Copy link

github-actions bot commented Mar 7, 2025

Confirmed: Jermolene has already signed the Contributor License Agreement (see contributing.md)

Copy link

netlify bot commented Mar 7, 2025

Deploy Preview for tiddlywiki-previews ready!

Name Link
🔨 Latest commit 76e1c21
🔍 Latest deploy log https://app.netlify.com/sites/tiddlywiki-previews/deploys/67cddc34d955f40008535bb3
😎 Deploy Preview https://deploy-preview-8972--tiddlywiki-previews.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@pmario
Copy link
Member

pmario commented Mar 8, 2025

I do get an RSOD if I do :let:test in the AdvancedSearch Filter tab.

Show screenshot

image

@pmario
Copy link
Member

pmario commented Mar 8, 2025

I am not overly happy with special handling of a :let:test[all[]] -- all[] usually means all tiddlers. Now it means only the first one. IMO that's inconsistent and confusing.

May be

  • all[first] -- a b :let:index[all[first]] [<data>jsonget[top],<index>]

IMO first would also make sense in general, so we could remove the "special" handling?!



I did play with: [tag[HelloThere]format:titlelist[]join[ ]] :let:test[all[]] [enlist<test>] to test several elements. It's complex

I could imagine an all[prerun] which would give us a much shorter version of the above test filter [tag[HelloThere]] :let:test[all[prerun]] [<test>enlist-input[]]

@pmario
Copy link
Member

pmario commented Mar 8, 2025

The RSOD only triggers if the input is typed. Not if copy / pasted. :letabc also triggers an RSOD

Not needed because the input to the filter run is available
Extend let filter run prefix to store list of results, and add varlist operator for accessing variables as a list.

We already had partial support for variables returning a list of values in order for functions to work, now we extend it so that any variable can be used to store a list

We should extend the set widget so that it returns a result list that can be accessed with the varlist operator
@Jermolene
Copy link
Member Author

I do get an RSOD if I do :let:test in the AdvancedSearch Filter tab.

Thanks @pmario that should be fixed now.

I am not overly happy with special handling of a :let:test[all[]] -- all[] usually means all tiddlers. Now it means only the first one. IMO that's inconsistent and confusing.

I've extended the implementation of variables so that they can store a result list as well as a single value. This allows the let filter run prefix to assign the entire results to the variable. The new varlist[name] operator retrieves a variable as a result list.

Terrible name. Annoyingly, we can't overload the existing let or set widgets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants