Re: [tw5] Re: [good practice] A multi steps IF using filters

2021-01-24 Thread Mohammad Rahmani
Thank you @Mark. It seems the {{{filter...}}} has a lot of potential to
create conditional statements. The actual use case was based on the user
interaction a set of classes shall be applied to few html tags. I used the
dictionary tiddler, but for small use cases I like your approach.

@Tones, thank you. I like these kinds of threads where the scripting
features of TW are discussed and I always learn new things.

Thank you Jed, Mat, Mark, and Tones.


Best wishes
Mohammad


On Mon, Jan 25, 2021 at 4:00 AM 'Mark S.' via TiddlyWiki <
tiddlywiki@googlegroups.com> wrote:

> Maybe too literal, but easier to read and extend:
>
> \define myfun(x)
> <$vars u={{{ [enlist[xxx one two three four]allbefore<__x__>count[]] }}} >
>   This is u: <>
> 
> \end
>
>
>
> On Sunday, January 24, 2021 at 2:24:58 AM UTC-8 Mohammad wrote:
>
>> I tried to implement the below pseudo code in TW 5.1.23
>>
>> if x=one then
>>   print(1)
>> elseif x=two then
>>  print(2)
>> elseif x=three then
>>  print(3)
>> else
>>  print(x is not valid)
>> end
>>
>> with
>>
>> \define myfun(x)
>> <$vars u={{{[<__x__>match[one]then[1]] [<__x__>match[two]then[2]]
>> [<__x__>match[three]then[3]]  +[addsuffix[ lili]]  ~[<__x__>addsuffix[ is
>> not valid]]}}} >
>>   This is u: <>
>> 
>> \end
>>
>>  It works fine on https://tiddlywiki.com/   by calling macro as below
>>
>> <>
>> <>
>> <>
>> <>
>>
>> I wish to know is this a good practice in TW 5.1.23? If not what better
>> solution do you propose?
>>
>>
>> Best wishes
>> Mohammad
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/tiddlywiki/99c1cf6c-db74-491b-85e2-8b45e729dab2n%40googlegroups.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/CAAV1gMCAL6EyBL6jJ0u%2BqMCo68-%3D-ve%2B1d_fzcG3-7WL_uSqNQ%40mail.gmail.com.


Re: [tw5] Re: [good practice] A multi steps IF using filters

2021-01-24 Thread TW Tones
Mohammad,

I know your example is more a general example, but if we look at the 
specific example translating a number to a number-word we have the 
advantage that the input is an integer.

\define number-words() one two three four five six seven eight nine

<$list filter="[range[1,9]]">
   <> {{{ [split[ ]nth] 
}}}


But this could be extended further

You could also use the number as the index to a json or dictionary tiddler.

Regards'
Tones

On Monday, 25 January 2021 at 05:09:00 UTC+11 Mohammad wrote:

> Mat,
> Thank you! Well yes a dictionary tiddler can be used. I have used such a 
> solution in my recent plugin Tamasha.
> I also implement the switch-case using your proposed solution. Really 
> powerful.
>
> By the way, there are situations where the operands are not constant. I 
> just tried to see how I can implement a multi step IF.
> But I was not sure if my solution follows a good programming style or not!
>
> Best wishes
> Mohammad
>
>
> On Sun, Jan 24, 2021 at 6:10 PM Mat  wrote:
>
>> @Mohammad
>>
>> It is not the IF-THEN structure you're asking for but if the input and 
>> output are predefined like that, then it'd be simpler to use a data tiddler 
>> to just get the value i.e {{datatiddler##two}} ---> 2
>>
>> <:-)
>>
>> On Sunday, January 24, 2021 at 11:24:58 AM UTC+1 Mohammad wrote:
>>
>>> I tried to implement the below pseudo code in TW 5.1.23
>>>
>>> if x=one then
>>>   print(1)
>>> elseif x=two then
>>>  print(2)
>>> elseif x=three then
>>>  print(3)
>>> else
>>>  print(x is not valid)
>>> end
>>>
>>> with
>>>
>>> \define myfun(x)
>>> <$vars u={{{[<__x__>match[one]then[1]] [<__x__>match[two]then[2]] 
>>> [<__x__>match[three]then[3]]  +[addsuffix[ lili]]  ~[<__x__>addsuffix[ is 
>>> not valid]]}}} >
>>>   This is u: <>
>>> 
>>> \end
>>>
>>>  It works fine on https://tiddlywiki.com/   by calling macro as below
>>>
>>> <>
>>> <>
>>> <>
>>> <>
>>>
>>> I wish to know is this a good practice in TW 5.1.23? If not what better 
>>> solution do you propose?
>>>
>>>
>>> Best wishes
>>> Mohammad
>>>
>> -- 
>>
> 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/ff7f5cc9-a710-4894-949a-b5e83b2788ecn%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/0e090a96-28c5-4f40-9e79-84b32cf88cafn%40googlegroups.com.


Re: [tw5] Re: [good practice] A multi steps IF using filters

2021-01-24 Thread Mohammad Rahmani
Mat,
Thank you! Well yes a dictionary tiddler can be used. I have used such a
solution in my recent plugin Tamasha.
I also implement the switch-case using your proposed solution. Really
powerful.

By the way, there are situations where the operands are not constant. I
just tried to see how I can implement a multi step IF.
But I was not sure if my solution follows a good programming style or not!

Best wishes
Mohammad


On Sun, Jan 24, 2021 at 6:10 PM Mat  wrote:

> @Mohammad
>
> It is not the IF-THEN structure you're asking for but if the input and
> output are predefined like that, then it'd be simpler to use a data tiddler
> to just get the value i.e {{datatiddler##two}} ---> 2
>
> <:-)
>
> On Sunday, January 24, 2021 at 11:24:58 AM UTC+1 Mohammad wrote:
>
>> I tried to implement the below pseudo code in TW 5.1.23
>>
>> if x=one then
>>   print(1)
>> elseif x=two then
>>  print(2)
>> elseif x=three then
>>  print(3)
>> else
>>  print(x is not valid)
>> end
>>
>> with
>>
>> \define myfun(x)
>> <$vars u={{{[<__x__>match[one]then[1]] [<__x__>match[two]then[2]]
>> [<__x__>match[three]then[3]]  +[addsuffix[ lili]]  ~[<__x__>addsuffix[ is
>> not valid]]}}} >
>>   This is u: <>
>> 
>> \end
>>
>>  It works fine on https://tiddlywiki.com/   by calling macro as below
>>
>> <>
>> <>
>> <>
>> <>
>>
>> I wish to know is this a good practice in TW 5.1.23? If not what better
>> solution do you propose?
>>
>>
>> Best wishes
>> Mohammad
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/tiddlywiki/ff7f5cc9-a710-4894-949a-b5e83b2788ecn%40googlegroups.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/CAAV1gMDfUuM9Jn87MdHPV%3DHFNgtrpH3vSwMO6vX5ueDpiOm%3D-g%40mail.gmail.com.


Re: [tw5] Re: [good practice] A multi steps IF using filters

2021-01-24 Thread Mohammad Rahmani
Thank you Jed,
I have not used the lookup operator, seems promising. I will give a try.


Best wishes
Mohammad


On Sun, Jan 24, 2021 at 2:41 PM Jed Carty  wrote:

> I don't see anything wrong with using that macro, but I do have an
> alternative that I think is more flexible.
> Depending on the situation this may not work, but here is the same result
> using lookup:
>
> \define myfun(x)
> <$vars u={{{ [<__x__>lookup:$x$ doesn't exist[value ]] }}}
> \end
>
> then you make a series of tiddlers with the titles 'value one', 'value
> two', 'value three'  with 1, 2 and 3 in their text fields.
>
> The downside is that you can't distribute it as just the macro, the
> benefit is that you can extend it to as many options as you want without
> editing the macro.
>
> On Sunday, January 24, 2021 at 11:34:00 AM UTC+1 Mat wrote:
>
>> Very minor but for readability you can break the line in multiple places.
>>
>> <:-)
>>
>> On Sunday, January 24, 2021 at 11:24:58 AM UTC+1 Mohammad wrote:
>>
>>> I tried to implement the below pseudo code in TW 5.1.23
>>>
>>> if x=one then
>>>   print(1)
>>> elseif x=two then
>>>  print(2)
>>> elseif x=three then
>>>  print(3)
>>> else
>>>  print(x is not valid)
>>> end
>>>
>>> with
>>>
>>> \define myfun(x)
>>> <$vars u={{{[<__x__>match[one]then[1]] [<__x__>match[two]then[2]]
>>> [<__x__>match[three]then[3]]  +[addsuffix[ lili]]  ~[<__x__>addsuffix[ is
>>> not valid]]}}} >
>>>   This is u: <>
>>> 
>>> \end
>>>
>>>  It works fine on https://tiddlywiki.com/   by calling macro as below
>>>
>>> <>
>>> <>
>>> <>
>>> <>
>>>
>>> I wish to know is this a good practice in TW 5.1.23? If not what better
>>> solution do you propose?
>>>
>>>
>>> Best wishes
>>> Mohammad
>>>
>> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/tiddlywiki/63dedb45-c31a-4a78-9733-caecd865df0an%40googlegroups.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/tiddlywiki/CAAV1gMBXmJwitFQsxrx98Ropx3jW2tr9eYJbGn4G3PQ2Trc2iQ%40mail.gmail.com.