The Magic of Functions in TiddlyWiki

Functions allow you to write complex filter operations in a simple, semantic and understandable way. Do you have any special use cases to share with the community here?

My First Example:
I have some notes tagged with machine-learning as parent tiddlers.
The parent tiddler has two field extra-notes, and related-notes which list all notes (tiddlers) in relation to this parent note
The subtiddlers are note tiddler; their title looks like parent-note/child-note e.g. prefixed with main note.

Write a simple filter to list all children tiddlers using list-links.

\function children()
   [all[tiddlers]prefix<currentTiddler>]
   [get[extra-notes]enlist-input[]]
   [get[related-notes]enlist-input[]]
\end children

\function getChildren() [tag[machine-learning]] :map:flat[function[children]]

Then use it like:

<<list-links "[function[getChildren]]">>

What do you think?

Cheat: The old school way is macro and subfilter :wink:

Share you function examples.

4 Likes

Oops, there was some syntax issues. I edited the initial post.

@Mohammad I am moving this topic out of the Tips & Tricks category as it seems to be inviting a discussion rather than presenting a summary of useful techniques. A summary of that eventual discussion would make a useful post in Tips & Tricks.

It would be helpful if the regular members of the community referred to the guidelines for the category to understand its intended usage. I will try to find time in the coming days to go through and clean up this category of the forum.

4 Likes

I went back to search for this post to add this reply, because it’s from this very post that I learnt to do this:

\function map.tiddler( parm1, parm2 ) [all[]] :map[for.each<currentTiddler>,<parm1>,<parm2>]
\function for.each( Tid, parm1, parm2 ) 
[<Tid>]
[<Tid>addsuffix[_field1]]
[<Tid>addsuffix[_field2]]
[<parm1>addprefix[Parm#]]
[<parm2>addprefix[Parm#]]
+[join[ + ]]
\end

{{{ [[Tiddler1]] [[Tiddler2]] +[function[map.tiddler],[P1],[P2]] }}}

Output:

Tiddler1 + Tiddler1_field1 + Tiddler1_field2 + Parm#P1 + Parm#P2
Tiddler2 + Tiddler2_field1 + Tiddler2_field2 + Parm#P1 + Parm#P2

Right now, we use "[all[]] :map[..<currentTiddler>.." in Functions to get access to currentTiddler as a variable. Make another function call with currentTiddler and function parameters as variables offers more flexibility. These variables can now be used freely in multiple filter lists, then join together with /,[],[ ],[<linefeed>],etc to return as a path/string/list/array/text to the original filter expression !

2 Likes