Code simplification Challenge - a smart config values select

170 views
Skip to first unread message

TW Tones

unread,
Jul 4, 2020, 12:33:15 AM7/4/20
to TiddlyWiki
Folks,

Fancy yourself a tiddlywiki wizard (or wannabee)?, are you up to the challenge?, think the solution is obvious?, like to learn through trying?

This is an optional coding challenge to provide a better solution, more concise code, if you are interested.
  • I am in the middle of creating a solution, the solution contains a configuration tiddler $:/config/name
  • I have long sought to automate configuration settings and have done so with the code below.
  • I would like to see if it can be simplified further.
  • I thought I would open this as a coding challenge for anyone interested, basically since each select statement differs only on value pairs
    eg yes no / show hide, there may be a way to simplify, or shorten the macro.
The solution
  • You call this macro with a tiddlername (defaults to current tiddler) and fieldname, default field is "fieldname", (because text could be dangerous)
  • Based on the value it finds there, it allows you to select the value to its natural alternative, eg Yes you can toggle to no, true to false etc...
  • If no value is found the first select widget, allows you to select one from all the available values "yes no show hide true false left right top bottom"
  • However there is a select widget for each of the common pairs of values and they differ only by the two filter used.
The challenge;
  • See if you can write the same code in much shorter yet easier to configure/customisation form. For example add the additional options enable/disable.
Extended challenges (for the foolhardily)
  • Once a config value pair is selected, set the config-values field in the config tiddler to those two values eg config-values="true false"
  • Allow the order to be set so the first value in config-values is considered the default.
  • Create a view template that detects tiddlers containing one of these values and show the config value select macro on those tiddlers
I will do this eventually myself but do not yet have the required inspiration as to how, so I thought I would open it to the community as a challenge.

The tiddler is also attached for your convenience, works on empty.html, tiddlywiki.com or my playground. The macro is both defined and called in this tiddler.

\define smart-configure(tiddlername fieldname:"fieldname")
<$set name=tiddlername value="$tiddlername$" emptyValue=<<currentTiddler>> >
<$set name=currentValue value={{{ [<tiddlername>get[$fieldname$]] }}}>
tiddlername
=<<tiddlername>>, fieldname=$fieldname$, currentValue=<<currentValue>><br>

<$list filter="[<currentValue>is[blank]]" variable=nul>
Select initial value:
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >
 
<$list filter='yes no show hide true false left right top bottom'>
     
<option value=<<currentTiddler>>><$view field='title'/></option>
 
</$list>
</
$select>
</$list>

<$list filter="yes no +[match<currentValue>]" variable=nul>
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >
  <$list filter='yes no'>
     <option value=<<currentTiddler>>><$view field='title'/
></option>
 
</$list>
</
$select>
</$list>

<$list filter="show hide +[match<currentValue>]" variable=nul>
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >
  <$list filter='show hide'>
     <option value=<<currentTiddler>>><$view field='title'/
></option>
 
</$list>
</
$select>
</$list>

<$list filter="true false +[match<currentValue>]" variable=nul>
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >
  <$list filter='true false'>
     <option value=<<currentTiddler>>><$view field='title'/
></option>
 
</$list>
</
$select>
</$list>

<$list filter="left right +[match<currentValue>]" variable=nul>
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >
  <$list filter='left right'>
     <option value=<<currentTiddler>>><$view field='title'/
></option>
 
</$list>
</
$select>
</$list>

<$list filter="top bottom +[match<currentValue>]" variable=nul>
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >
  <$list filter='top bottom'>
     <option value=<<currentTiddler>>><$view field='title'/
></option>
 
</$list>
</
$select>
</$list>
</
$set></$set>
\end

<<smart-configure "$:/config/Tiddlers/TitleLinks" text>>

In closing
  • Do you like the idea of such challenges, 
  • shall I release more? 
  • Is there one you would like to raise?
  • Or a challenge you would like me to post?
  • Let me know here or privately.
Regards
TW Tones

 
Select Challenge.json

Felicia Crow

unread,
Jul 4, 2020, 4:52:41 PM7/4/20
to TiddlyWiki
Hi,

First personally I would love for challenges like these to become a thing since not only do I get to learn things I normally would not necessarily need for myself and second I think it would be interesting to see different approaches to the same challenge.

Second here is my solution for the basic challenge:

How the macro works:
  • The Macro is still called with tiddlername and fieldname with the same defaults.
  • The outermost list widgets check if currentValue is empty or not and react accordingly
  • If there is no currentValue the first select will be created with all values currently defined in the corresponding config tiddler.
    • The config tiddler to use is defined in the configTiddler variable to make it easier to change the tiddler. Currently the only hard coded part is that the pairs have to be in the text field.
  • If there is a currentValue - or a value was chosen in the first select - then the second select will be created only showing the corresponding pair. E.g.: If the current value is 'yes' only the options 'yes' and 'no' are available.
    • For now the value pair has to be defined in the config tiddler for this to work. In the version for the extended challenge I hopefully manage to have a way to create a field on setting the initial value that will keep the respective pair within the tiddler itself.
How to extend the macro
  • Simply add another pair/set of options to the config separated at least by ';' for the pair and by at least one space for each option.
  • Only caveat for now is each value in a pair/set has to be a single word/string without spaces.
Macro
\define smart-configure(tiddlername fieldname:"fieldname")
<$set name=configTiddler value="$:/smartConfig/config/pairs">

<$set name=tiddlername value="$tiddlername$" emptyValue=<<currentTiddler>> >
<$set name=currentValue value={{{ [<tiddlername>get[$fieldname$]trim[]] }}}>

tiddlername
=<<tiddlername>>, fieldname=$fieldname$, currentValue=<<currentValue>><br>

<$list filter="[<currentValue>is[blank]]" variable=nul>
Select initial value:
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >

 
<$list filter='[<configTiddler>get[text]split[;]!is[blank]join[ ]]'>
     
<$list filter="[<currentTiddler>split[ ]!is[blank]]">    
       
<option value=<<currentTiddler>>><$view field='title'/></option>
     
</$list>
  </
$list>
</$select>
</
$list>

<$list filter="[<currentValue>!is[blank]]" variable=nul>
<$list filter="[<configTiddler>get[text]split[;]trim[]!is[blank]] +[search<currentValue>]">

<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >

 
<$list filter="[<currentTiddler>split[ ]]">    
   
<option value=<<currentTiddler>>><$view field='title'/></option>
 
</$list>
</
$select>
</$list>
</
$list>

</$set>
</
$set>
</$set>
\end

Configuration
yes no; show hide; true false; left right; top bottom

The attached version of the macro tiddler includes a text edit widget to quickly edit the config tiddler and two examples to see both states of the macro.
The import and tiddlers work on empty.html, tiddlywiki.com and the playground.

For now I will leave it at that, but will look into the extended challenge within the next days since it has some very interesting points to solve.

Regards,
Felicia
SmartConfigure.json

TW Tones

unread,
Jul 4, 2020, 9:38:32 PM7/4/20
to TiddlyWiki
Felicia,

Great work. 

I am happy to do more challenges, I hope others also support this idea, and they are welcome to do it themselves.

In chrome I downloaded your json to a scratch area, went to tiddlywiki.com, dragged the file and dropped it in tiddlywiki.com and tested it. I added enable disable to the value config values. It works and it reuses the select statements reducing the amount of code required.

The edit pairs could be changed to 
Edit Pairs: <$edit-text tiddler="$:/smartConfig/config/pairs" field="text" tag=input size=80/>

I used this small test opn your example and mine
<$list filter="[prefix[$:/config/]]">
<
<smart-configure fieldname:"text">>
</$list>
Something is not right.

Regards
Tony



Xavier Cazin

unread,
Jul 5, 2020, 4:47:28 AM7/5/20
to tiddl...@googlegroups.com
Hi Tony,

Fun idea for a Sunday morning :-) Here is a proposed refactoring, based on the use of the subfilter operator. It doesn't tackle your extended challenges, but I believe it would make them easier to implement:

\define test_value_pair(value_pair, state_field)
<$list filter="[subfilter<__value_pair__>] +[match<currentValue>]" variable=nul>
<$select tiddler=<<tiddlername>> field=<<__state_field__>> default=<<currentValue>> >
  <$list filter='[subfilter<__value_pair__>]'>

     <option value=<<currentTiddler>>><$view field='title'/></option>
  </$list>
</$select>
</$list>
\end


\define smart-configure(tiddlername fieldname:"fieldname")
<$set name=tiddlername value="$tiddlername$" emptyValue=<<currentTiddler>> >
<$set name=currentValue value={{{ [<tiddlername>get[$fieldname$]] }}}>
tiddlername=<<tiddlername>>, fieldname=$fieldname$, currentValue=<<currentValue>><br>

<$list filter="[<currentValue>is[blank]]" variable=nul>
Select initial value:
<$select tiddler=<<tiddlername>> field="$fieldname$" default=<<currentValue>> >
  <$list filter='yes no show hide true false left right top bottom'>
     <option value=<<currentTiddler>>><$view field='title'/></option>
  </$list>
</$select>
</$list>

<$list filter="[[yes no]] [[show hide]] [[true false]] [[left right]] [[top bottom]]" variable=config_pair>
  <$macrocall $name="test_value_pair" value_pair=<<config_pair>> state_field=<<__fieldname__>> />

</$list>

</$set></$set>
\end

<<smart-configure "$:/config/Tiddlers/TitleLinks" text>>


Cheers,
-- Xavier Cazin


--
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/8bdd0936-16e7-48c8-a6f1-9d6cb587784fo%40googlegroups.com.

Felicia Crow

unread,
Jul 6, 2020, 3:01:58 AM7/6/20
to TiddlyWiki
Hi Tony,

sorry for not answering yesterday, but my migraine apparently now gets also triggered by smaller changes in weather.

Thanks for the reminder of the size parameter! Somehow I was so convinced at the time that it had to be something with width and found nothing except for css changes that I used a textarea for the bigger size.
The whole it has to be width was probably while I also overread it in the documentation.

To your test, can you elaborate on what you think is not right? I tried both versions in the same wiki and they gave me the same result.

Regards,
Felicia

TW Tones

unread,
Jul 6, 2020, 6:42:38 AM7/6/20
to TiddlyWiki
Felicia,

sorry for not answering yesterday, but my migraine apparently now gets also triggered by smaller changes in weather.


I feel for you with migraines although I have being told, to keep a log before you conclude things like  "now gets also triggered by smaller changes in weather" because you can teach yourself to respond to triggers, so it becomes a self fulfilling prophecy. But I am sure you know what is best for yourself, so many different things can trigger them. 

My mother suffered them a lot at one point but they disappeared, lets hope the same happens for you :)
 
Thanks for the reminder of the size parameter! Somehow I was so convinced at the time that it had to be something with width and found nothing except for css changes that I used a textarea for the bigger size.
The whole it has to be width was probably while I also overread it in the documentation.

To your test, can you elaborate on what you think is not right? I tried both versions in the same wiki and they gave me the same result.

Yes, my little test I though broke your solution, then I noticed it also broke on my original post. I have not tried to solve it yet. Another challenge?

best of luck
thanks for your go.
Tony
 

Felicia Crow

unread,
Jul 6, 2020, 7:24:24 AM7/6/20
to TiddlyWiki
Thanks for your kind words!
That was why I wrote apparently since I know from years of dealing with my migraine and observation that larger changes in weather and/or temperature triggers it - always "fun" in weeks where it gets from sunny to rain and back from one day to the next - so seeing how the weather here went from no wind and heat on saturday to roughly the same temperature, but very windy yesterday it is my current working theory for why my migraine got triggered yesterday, not a definitiv conclusion.

It is great to hear that they disappeared for your mother and yeah I hope that it will get better eventually.
One silver lining is that at least by now I am better at dealing with them so I am not fully out of comission, but I still would not have had the focus to test out and find a solution to your test so that's why I decided to get back to you when my brain was working again.


To the test itself: What confused me was that it worked for the tiddlers the filter gives back, but that the filter itself only gives back some tiddlers even when I add is[shadow] to it. So for now yes it seems to be another challenge.

Thanks again for your kind words and best of luck to you too,
Felicia

Birthe C

unread,
Jul 6, 2020, 9:53:35 AM7/6/20
to TiddlyWiki
Felicia and TW Tones,

I know it is used for another purpose, but have you seen the mono theme and how it used the EmptyStoryMessage.

I also know the migraine all too well. I was one of the lucky ones, it disappeared with age as it is often the case. Until then several days a month. my best friend and I had lots of problems with migraine, when we were at university. Then we found that of course we did not get rid of it, but it got milder, when we ate pure licorice. Pure...not the candy one with all sorts in the mix. (And the timing to ate it......)

Birthe

TW Tones

unread,
Jul 6, 2020, 7:27:21 PM7/6/20
to TiddlyWiki
Birthe,

Yes JD is using a preview layout. His side view is also interesting.

My Thoughts were ultimately to build an easy to use tiddler thumbnail method for navigating tiddlers somewhat graphically. If we look at tiddlywiki.com we have this;

Snag_4c150f13.png

  • But this depends on thumbnail images to be graphically meaning full about the content.
  • To be effective preview thumbnails need to contain an impression and ideally an appealing one, of the tiddlers content.
  • We could increase the size with hover or other methods to preview the tiddler from a preview thumbnail.
  • JD's example reminds me Mohammad's Shiraz has a number of layout like cards that could be used to provide thumbnails and timelines etc...
  • Come to think of it I do not hear from Mohammad so often. He has being cut off from us before due to international politics as he is in Iran. I hope he is safe.
Perhaps we need an off topic forum?

Fortunately for me I do not suffer migraines, but have had half a dozen in my life, one which was lights in my eye no pain, so I can have some insight. My separate interest in neurology  (lay scientist) and the way the brain works, and along with referred pain I get due to a pinched nerve in my neck,  I have long had an interest in this mine/brain/pain system.

Possibly one of the most important things I have picked up is the mind/brain/pain relationships are also influenced by your state of mind, not just the pain but our reaction to it. The truth is there is a psychosomatic component but the common bias against mental health and psychological problems, including the way we treat our-self, stand in the way of real progress in this area. Unfortunately where there is uncertainty a lot of "snake oil" and untested "Alt-medicine" that confuses this further. Hence my original comment on the subject to be careful, of self fulfilling observations. Unfortunately this occurs, but it is hard to explain to people (and understand myself) because of these common biases, I hope Felicia was not at all insulted with my mention of this. I genuinely wanted to help, because I empathise with this condition. One close friend suffers this every other day along with back problems.  Just before I posted I stopped to ask myself if I was "mansplaining", fortunately Felicia did not respond as if I had.

As background I am a modern sceptic and are aware we are often victims of common fallacies in our thinking, I cultivate critical thinking in myself and where appropriate others. 
Of course deniers are not sceptics, although they often claim so, most sceptics apply the sceptical view and use evidence to resolve the questions, for example climate change is true, most conspiracy theories are false and covid-19 is a real threat if someone is sceptical of these they know too little and are suffering from the Dunniong-Kruger effect.

Regards
Tony
Reply all
Reply to author
Forward
Message has been deleted
0 new messages