Can I leverage custom functions better (for sorting, more direct filtering)?

Hi folks,

I’m probably missing something obvious, but have scratched my head enough over this for now…

Working with a custom function, I’m trying to figure out (1) how to sort tiddlers according to the yield of the custom function for each tiddler, and (2) how to leverage the custom function in Advanced Search filter — so as to export a set of tiddlers based on what the custom function yields.

Along the way, I should also note a gap in our documentation at tw-com: function Operator has a link at bottom for Examples, but it’s a dead end. Maybe some examples could be generated to help with questions such as mine…

So: I have a custom function called tally.micro. It’s an overall score for a student’s assignment, where — for pedagogical and privacy reasons — I never display the overall score on our shared semi-public site (and I don’t even store it in a field). Instead I have little Harvey-ball icons for how well the assignment submission satisfies each element of scoring rubric. From those rubric elements, I can reconstruct an overall numeric score for the assignment. That’s what the tally.micro custom function does, which I use behind the scenes and in end-of-semester grading communications. (Yes, of course students can also do this reconstruction. But the idea is to focus student attention on the qualitative information more than on the numbers, and to minimize temptation for students to engage in quantitative comparison.)

I’d love to be able to (1) sort tiddlers by their tally.micro score; (2) get Advanced Search to filter for tiddlers with a certain tally.micro score range.

Here’s my implementation as it currently stands, not quite able to accomplish either of the above, though I can leverage the function to get an (unsorted) list of tiddlers that satisfy a filter condition using the custom function in a nested list.

Much appreciation for any insight!

Hi springer,

  • You will need to upgrade that wiki to the latest TiddlyWiki release in order for functions and filter run prefixes to play nice, there were a few bugs that needed resolution.
  • Note that in using tally.micro, the argument to the function is not needed as your function does not accept an argument and operates directly on the currentTiddler variable. So you can use tally.micro[] instead of tally.micro<currentTiddler>.
  • In order to use your filter in advanced search, you need a pure filter, that is without any other wikitext:
    • [has[xf]has[id]] :filter[tally.micro[]compare:number:gt[4.6]]
    • This is a very good example of the kind of use case for which the :filter prefix for filter runs was created
  • In order to sort by the tally, you can add on a :sort filter run. This is inefficient because we need to calculate the tally for each tiddler twice, once for the comparison and once for the sort, but it should not matter much unless the number of tiddlers being tallied is in the hundreds or thousands:
    • [has[xf]has[id]] :filter[tally.micro[]compare:number:gt[4.6]] :sort:integer[<tally.micro>]
    • The requirement to be able to use the filter in Advanced Search precludes the possibility to cache the tally in a variable and reuse it.
  • If you use the comparison filter often enough, this itself could also be saved as a global function for convenience.

(The code above is untested, if it does not perform as expected just let us know.)

2 Likes

Uuf, rookie mistake not to upgrade first. I saw the custom function basically worked, and didn’t do my homework after that. :flushed:

Yep, all of those things (like using tally.micro[] as a filter step, and using the :filter filter run prefix to focus in on the subset) were things that I had tried, but which weren’t quite working in 5.3.1 … all playing very nicely now, thanks!

I may not keep the link up long (or may remove many of the tiddlers providing the data soon) but here’s the effect in practice, in case anyone is following.

1 Like