How can I limit the number of recrusions in a macro using recursions.

162 views
Skip to first unread message

Jan

unread,
Oct 16, 2019, 7:07:03 PM10/16/19
to TiddlyWiki
Hello,
want to show the title of a chain of tiddlers which mention the next on in a field called thread.
This works fine like this
\define plotchain()
<$set name="chain" value="JJ/Das merkwürdige Tier">
$(chain)$
<<reku>>
</$set>
\end
\define chainer() [[$(chain)$]]
\define reku()
<$list filter="[list[$(chain)$!!thread]] [list[$(chain)$!!storylink]]" variable="chain">
<<chainer>>
<<reku>>
</$list>
\end

<$vars chain="JJ/Das merkwürdige Tier">
<<chainer>>
<<reku>>
</$vars>

But if someone builds a cirle this crashes the wiki.
Is there a way of limiting recursions e.g. by using a filter with math-operators?

Thanks!
Jan


TonyM

unread,
Oct 16, 2019, 7:19:05 PM10/16/19
to TiddlyWiki
Jan

Here is a quick high level answer, let me know if you want more details.

There are existing examples out there that avoid this like the standard TOC however one method that comes to mind is as you iterate each title add it to a variable "all members" (even a data tiddler) and before doing so check it is not already named in that variable. You can just change the action for it to do nothing, the recursion will then move on with the next in line.

I have an algorithm (in a button) where I actually place  the "all members" variable into each tiddler as a field called branch-branchname in your case this would be 
thread-threadname thus each tiddler knows all its predecessors. The thread name can be based on the first tiddler. This would allow multiple threads to cross the same tiddlers without interfering with the other thread. Since you know all the members of the thread you could store the full thread in any tiddler.

Just some ideas
Tony

Jan

unread,
Oct 16, 2019, 7:58:51 PM10/16/19
to tiddl...@googlegroups.com
Hi Tony,
yes I need the details, I already thought of the toc-macro...but I could not figure out how limiting recursions is done there.
I think this is the more promising approach.

Thanks! Jan
--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/205a7345-aa5b-4b90-9757-9ad96929e146%40googlegroups.com.

TonyM

unread,
Oct 17, 2019, 12:53:25 AM10/17/19
to TiddlyWiki
Jan,

Is this a flat list of tiddlers? Perhaps recursion is not needed.

If you had a seperate tiddler, lets call it path, and all tiddlers involved would be tagged with path then <<tag path>> will allow you to view and reorder the list. Your next and previous buttons could iterate this list of path tagged tiddlers, identifying what next and previous is and providing a button to move in either direction. The value is you cannot add the same tiddler twice to this list of tagged tiddlers thus you will never encounter a circle. 

Tony
To unsubscribe from this group and stop receiving emails from it, send an email to tiddl...@googlegroups.com.

Jan

unread,
Oct 17, 2019, 4:07:01 AM10/17/19
to tiddl...@googlegroups.com
Hi Tony,
I need to generate a (flat) list by using a recursive macro.
I found a somewhat bizarre solution:
------------

\define plotchain()
<$set name="chain" value="JJ/Das merkwürdige Tier">
$(chain)$
<<reku>>
</$set>
\end
\define chainer() [[$(chain)$]]
\define reku()
<$list filter="[list[$(chain)$!!thread]] [list[$(chain)$!!storylink]]" variable="chain">
<$list filter="[<limiter>addprefix[x]!prefix[xxxxxxxxxxxx]]" variable="limiter">
<<limiter>> <!--This is not necessary but shows that it works....-->
<<chain>>
<<reku>>
</$list>
</$list>
\end

<$vars chain="JJ/Das merkwürdige Tier" marker="y">
<<chainer>>
<<reku>>
</$vars>
--------------------------

I could not figure out another way to do so.
Strange but Yeahhh!

Jan
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/473b4f62-70f6-4dd9-8638-12cc85b66873%40googlegroups.com.

TonyM

unread,
Oct 17, 2019, 7:54:09 AM10/17/19
to TiddlyWiki
Jan

Bimlases kin operator can generate flat lists from a hierarchy all by itself in a single filter.

Have a look at it.

Tony

Mark S.

unread,
Oct 17, 2019, 12:23:27 PM10/17/19
to TiddlyWiki
This does literally what you asked for, though I'm not sure that's what you meant:

\define recurseme(yourvar,count)
<$list filter="[<__count__>!regexp[^0$]]" emptyMessage="TOO MANY RECURSIONS!" >
I
'm doing important things here! <br/>
<$set name="count" filter="[<__count__>subtract[1]]" select=0>
<$macrocall $name=recurseme yourvar="Stuff" count=<<count>>/>
</$set>
</$list>
\end

<<recurseme "more" "5">>

Put your own code where it says "I'm doing important stuff here." (Be sure to back up first).

Good luck!

Mohammad

unread,
Oct 17, 2019, 3:50:21 PM10/17/19
to TiddlyWiki
Sounds great Mark!

I think it is not far from writing Fibonacci sequence in Tiddlywiki :-)

--Mohammad

Mark S.

unread,
Oct 17, 2019, 4:56:34 PM10/17/19
to TiddlyWiki


\define fibber(last,next,count)
<$list filter="[<__count__>!regexp[^0$]]" emptyMessage="<<last>> <<next>>" >

<$set name="count" filter="[<__count__>subtract[1]]" select=0>
<$set name="next" filter="[<__last__>add<__next__>]" select=0>
<<__last__>> <$macrocall $name=fibber last=<<__next__>> next=<<next>> count=<<count>>/>
</
$set></$set>
</$list>
\end

<<fibber "0" "1" "6">>


Mark S.

unread,
Oct 17, 2019, 5:09:05 PM10/17/19
to TiddlyWiki
Oops. Here's a fix for fibonacci

\define fibber(last,next,count)<$list filter="[<__count__>!regexp[^0$]]" emptyMessage="<<__last__>> <<__next__>>" >

<$set name="count" filter="[<__count__>subtract[1]]" select=0>
<$set name="next" filter="[<__last__>add<__next__>]" select=0>
<<__last__>> <$macrocall $name=fibber last=<<__next__>> next=<<next>> count=<<count>>/>
</
$set></$set>
</$list>
\end


<<fibber "0" "1" "6">>

Mohammad

unread,
Oct 18, 2019, 12:16:36 AM10/18/19
to TiddlyWiki
Hi Mark!
 This is amazing! Fibonacci sequence code is among the benchmark code to show how efficient a language support the recursive operation.
 This code shows Tiddlywiki scripting language is powerful and of course shows your talent :-)

--Mohammad

Mohammad

unread,
Oct 18, 2019, 12:17:05 AM10/18/19
to tiddl...@googlegroups.com

Jan

unread,
Oct 18, 2019, 4:59:52 AM10/18/19
to tiddl...@googlegroups.com
Hi Mark!
Thanks, that is much more elegant than my "prefix"-workarround which I posted!

Jan


Am 18.10.2019 um 06:17 schrieb Mohammad:
Added to TW-Scripts.
--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.

Mohammad

unread,
Oct 18, 2019, 5:58:48 PM10/18/19
to TiddlyWiki
Resend for email users!

The entry in TW-Scripts
https://kookma.github.io/TW-Scripts/#Fibonacci%20Sequence

--Mohammad

@TiddlyTweeter

unread,
Oct 19, 2019, 9:32:31 AM10/19/19
to TiddlyWiki
Mark S.' Fibonacci sequence macro is really interesting & clever.

I'm getting interested in the idea of using macros similar to that to select Tiddlers to create "sample sub-series".

An example would be for a screenplay you want to analyse. Its well known that most mainstream movies have a "three-act" structure and sub-structures/patterns within them. 

It might be interesting to sample a screenplay that is in, say, 1,000 tiddlers, using mathematical patterns to better understand its screenplay structure.

That sampling would not be from a Fibonacci sequence, but seeing it, I realised we can now do "sample patterns" macros quite elegantly. So far I been using random sampling. This opens a better way.

Early thoughts. I'll formulate a better question about this at some point.

Best wishes
Josiah
Reply all
Reply to author
Forward
0 new messages