TW Switch Case Script

73 views
Skip to first unread message

Mohammad

unread,
Jul 4, 2019, 5:06:56 AM7/4/19
to TiddlyWiki
In Javascript, C, C++ we have a programming construct called Switch-Case


switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}


This code can do equivalent operation! Is this according to TW good programming practice?
Is there any better solution? I think looking in list items may be another solution! Does anybody has tried such solution before? 


\define items() One Two [[Three Four]] Five

\define switch(item)
<$list filter="[enlist<items>search<__item__>]" emptyMessage="Zero">
<<currentTiddler>>
</$list>
\end

<<switch One>>
<<switch "Three Four">>
<<switch "Ten">>


This line

<$list filter="[enlist<items>search<__item__>]" emptyMessage="Zero">

Can be like this also

<$list filter="[enlist<items>title<__item__>]" emptyMessage="Zero">


What do you think?

TonyM

unread,
Jul 4, 2019, 8:21:07 AM7/4/19
to TiddlyWiki
Mohammad

"Case" is a common programming structure although it can get quite sophisticated. I went thru a process of building ways to build methods for all the basic programming structures in tiddlywiki more than a year ago. I will see what I can dig up.

Tony

Mohammad

unread,
Jul 4, 2019, 8:24:52 AM7/4/19
to TiddlyWiki
Thanks Tony!
I would be happy to have a look if it is on public domain or if you dont mind to share!

--Mohammad

Mat

unread,
Jul 4, 2019, 9:16:47 AM7/4/19
to TiddlyWiki
Mohammad - very cool and should be really useful to have it as a defined pattern.

It could be made more general if "switch(item, list)"

<:-)

Mohammad

unread,
Jul 4, 2019, 11:12:34 AM7/4/19
to TiddlyWiki
Hi Mat,
 I will elaborate on that and modify to have switch(item, list)

One simple case is like below:

\define switch(item, list:"a b c")
<$list filter="[enlist<__list__>search<__item__>]" emptyMessage="Default">
You passed <<currentTiddler>>
</$list>
\end

<<switch a>>

<<switch b>>


A more sophisticated cab be lie this

\define a() This is the action for a.
\define b() This is action for b.
\define c() This is action for c.
\define default() This is default action.

\define switch(item, list:"a b c")
<$list filter="[enlist<__list__>search<__item__>]" emptyMessage=<<default>> variable="actMacro">
<$macrocall $name=<<actMacro>> />
</$list>
\end


<<switch a>>

<<switch b>>

<<switch d>>


When in a tiddler the switch is called in that tiddler the action macro can be defined!
The code required the same action macro name as item name.

--Mohammad

Mat

unread,
Jul 4, 2019, 12:49:12 PM7/4/19
to TiddlyWiki
You could even get specific error messages

<$list filter="[enlist<__list__>search<__item__>]" emptyMessage="""No $item$ in: $list$""">

<:-)

Jeremy Ruston

unread,
Jul 4, 2019, 4:41:34 PM7/4/19
to tiddl...@googlegroups.com
Hi Mohammad

Your construction with list widgets is a pretty good approximation of a switch/case construction. The only frustration is that it is possible for a value to trigger more than one of the list widgets; it would be better if there was a way to make things stop after the first match.

One could certainly imagine a <$switch>/<$case> widget pair that could resolve that issue and might be more efficient as well. Perhaps after v5.1.20

Best wishes

Jeremy

--
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 post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/b5fb6465-db3a-4809-a878-5b475f3c1b99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mohammad

unread,
Jul 4, 2019, 5:39:02 PM7/4/19
to TiddlyWiki
Hi Jeremy,
 That sounds good! I will also file a  feature request for this and hopefully have a <$switch>/<$case> widget pair after 5.1.20

--Mohammad


On Friday, July 5, 2019 at 1:11:34 AM UTC+4:30, Jeremy Ruston wrote:
Hi Mohammad

Your construction with list widgets is a pretty good approximation of a switch/case construction. The only frustration is that it is possible for a value to trigger more than one of the list widgets; it would be better if there was a way to make things stop after the first match.

One could certainly imagine a <$switch>/<$case> widget pair that could resolve that issue and might be more efficient as well. Perhaps after v5.1.20

Best wishes

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

Mohammad

unread,
Jul 4, 2019, 5:51:51 PM7/4/19
to TiddlyWiki
Added to TW-Scripts
Reply all
Reply to author
Forward
0 new messages