Rating macro where parameter defines number of time a value is repeated

I would like to create a macro (I’m using an older wiki) that would repeat a value based on the number defined by the parameter. I envision something like this:

\define rating(num): Where “*” is returned the number of times specified in “num,” for example:

<<rating 1>>: returns *
<<rating 2>>: returns **
<<rating 3>>: returns ***
etc.

Is there a way to do this?

Also, if it’s possible, what would the updgraded “procedure” look like?

Try this out:

\define rating(num) <$text text={{{ [range<__num__>] :map[[*]] +[join[]] }}} />

The equivalent procedure (TW 5.3.0+) is very similar: we simply use <num> instead of <__num__> to reference the num parameter.

\procedure rating(num) <$text text={{{ [range<num>] :map[[*]] +[join[]] }}} />

You’ll notice that both macro and procedure need a text widget to display the plain-text output of the filter. However, if you’re using 5.3.0+, we can do this even more simply with a function, also called with the same <<rating 3>>-type syntax:

\function rating(num) [range<num>] :map[[*]] +[join[]]
2 Likes

Looks similar to this Tricks for refactoring ugly long filters? - #2 by etardiff

Thank you, etardiff!

Although this doesn’t work on my file for some unknown reason, I tested it on the most recent Tiddlywiki version and all three work like a charm.

My TiddlyWiki version is 5.1.23, which doesn’t seem terribly old, but perhaps that is the problem? I get the following error:

“Filter Error: Unknown prefix for filter run”

If anyone knows the cause/fix for this, please let me know. But, the problem is obviously not in the syntax that etardiff provided since that works splendidly.

I may have to just give up pursuing this for this particular file.

Thank you much!

MR

Oh, you’re missing the :map filter run prefix; it was introduced in 5.2.0. v5.1.23 is quite old (released in December 2020); you’ll be missing a number of useful filter operators in addition to 5.3.0+ features like procedures and functions (and a lot of other non-filter goodies).

I’d recommend upgrading just for access to things like :map, even if you never need most of the other newer features! I think the vast majority of active members here are using at least 5.2.0+, and that will color any suggestions you receive.

I suppose I should, but I have fear of the consequences. :grimacing: I will give it a trial run.

Thank you!