This is a stripped-down version of a single section of Grok TiddlyWiki, optimized for fast loading and readability by search engines. Some features are missing.

For the full Grok TiddlyWiki experience, please visit the wiki version of this page.

Ex:MultiplyByTwoTwoWays/answer

 8th October 2024 at 7:03am

The trick is to have a parameter that defaults to blank (or any other value that nobody can use as a valid number to be multiplied, often called a sentinel value in programming), and create a first filter run that removes the parameter from the pipeline (making it empty) if the parameter is still set to the sentinel value. If you still have a value, then the user of the function supplied a parameter and you can multiply it by two.

That settles the parameter case. Now for the input tiddler case: use the :else (also spelled ~) filter run prefix to supply a second run which is used only if the first run had no results (thus no parameter was passed). In this run, multiply the input tiddler by 2.

All together:

\function multiply-anything-by-two(number:"") [<number>!is[blank]multiply[2]] :else[multiply[2]]

* {{{ [[4]function[multiply-anything-by-two]] }}} should equal 8.
* {{{ [function[multiply-anything-by-two],[4]] }}} should equal 8.

  • 8 should equal 8.
  • 8 should equal 8.
Go to question: Ex:MultiplyByTwoTwoWays