Design Challenge simple AND, OR NOT conditions in filters

Filters are extremely capable and can do the equivalent of the logical conditions;

  • AND OR NOT and even NAND NOR XOR and XNOR

However they do this within the filter syntax. This means if you are new to TiddlyWiki even AND and OR are not obvious, and only tend to be understood once you gain a deeper understanding of the filter syntax.

  • This seems it may present a tiddlywiki adoption barrier.

With the introduction of functions/custom functions and the %if syntax we are empowered to write clearer filter conditions and structures. However the above logical conditions are not well catered for.

The Challenge for you/us, if you choose to accept it, is how can we provide some methods to allow all users, but particularly new users to make use of these logical conditions within filters.

  • Essential to this endeavour is coming up with the appropriate description how to do this, although we should be able to make it easier for users.
  • Some custom functions, procedures or even widgets are likely to help
  • We may find some slight core additions will help close any gaps.

Any ideas?

1 Like

Because filters can become burdensome to grok, I’d prefer the next expansion of the shortcut syntax to include…

<% if <expr> %>

where <expr> is any wikitext construct (filter, variable, text-ref, etc) that returns text or blank.

Falsey values:

  blank

  0, -0, 0.0

  "", '' (empty strings)

Truthy values are values that fail the falsey test(s).

Then introduce the logical keywords…

<% if <expr1> AND <expr2> %>
  ...
<% if <expr1> OR <expr2> %>
  ...
etc.

One could argue that if could be implied…

<% <expr1> AND <expr2> %>
  ...
<% end %>

but only if you don’t need else / elseif.

I was also feeling depressed when I didn’t know how to use if else in the filter expression. But I remember I searched in the forum and got the answer.

Now I totally understand. Here are some example from the translate macro (procedure) PR

Equivalent for Nullish coalescing operator (??)

(fake) JS: <lingo-base-fallback> ?? <lingo-base> , which is the OR

Filter:

\function target-lingo-base()
[<lingo-base-fallback>!is[blank]else<lingo-base>]
\end target-lingo-base

the !is[blank] is the NOT.

Equivalent for Conditional (ternary) operator

(fake) JS: target-lingo-base().endsWith('/') ? target-lingo-base() : target-lingo-base() + '/'

Filter:

<!-- Allow lingo-base to have or not have / ending slash, for better dev experience. Then add, e.g. `en-GB/Key` after it. -->
\function lingo-target()
[function[target-lingo-base]suffix[/]]
:then[function[target-lingo-base]]
~[function[target-lingo-base]addsuffix[/]]
\end lingo-target

Since we are using [function[xxx]], you can do AND in the function, so the ternary operator will check the result of AND.

How to do && AND logic

Just use [xxx] [xxx], use Space to connect two filter expression, and check !is[blank] using +

\function variable-A-and-variable-B()
[<A>] [<B>] +[!is[blank]]
\end variable-A-and-variable-B

Single line Pure Filter?

I forget how to use it as pure filter, it is much harder!

@CodaCoder I like your suggestion but it also involves a somwhat new syntax. I wonder if we can find a way that will operate in a standard filter as well?

Note the following behaves as an OR

  • <%if [all[current]get[caption]] [all[current]get[description]] %>

The following appears to be AND

  • [all[current]get[caption]] :and[all[current]get[description]]
  • but falsely says its true if the first is empty.

We seem so close already?

Sorry I really don’t follow @linonetwo in part my challenge is for simplicity.

Yes, I realize I’m using wikitext instead of single line pure filter.

“Simple” pure filter is much harder than “seemly Complex” filter + wikitext

I went nuts at one point on tiddlywik.com searching for words like “boolean” and “disjunction” and “conditional” and really had trouble grasping how such logical concepts were nowhere in sight! (Some of these words appear there now, but in content that was created just earlier this year.)

Translating logical operators into TW filter syntax is a high bar that may have deterred more than one otherwise interested user.

1 Like

@TW_Tones I think you’re starting to drift…

Every part of TW is new syntax to new users. You started out by saying…

So I responded with my idea/proposal.

Now you’re saying…

So which is it? What exactly are you talking about?

In any case, my proposal doesn’t mean you lose the ability to use filters – <% %> already supports them. I was proposing to expand on the shortcut syntax for if, such that other wikitext forms are allowed to be used as input to the conditional tests.

The difference between using the current syntax in a helpful way OR adding to the syntax (to be helpful)

  • I am not against your proposal but want to see if it can be achieved with current syntax.
  • If we adopt your suggested syntax it will not necessarily work in other filters. Your approach may be the best, but I don’t think we have exhausted the current syntax.

For example assume this;

\function current.has.field(fieldname return:"true") [<currentTiddler>]has<fieldname>then<return>]

This following is the equivalent to OR

<%if [current.has.field[caption]] :or[current.has.field[description]] %>
Caption OR description <<condition>>
<%else%>
Neither
<%endif%>
  • You can add more such as :or[current.has.field[test]]

The following is the equivalent to AND

<%if [current.has.field[caption]] :intersection[current.has.field[description]] %>
Caption AND description <<condition>>
<%else%>
Not both
<%endif%>
  • As you can see above, the :or “looks right”.
  • But the AND needs the :intersection not the :and

You can see, we are close but not there for a simple use of the standard filter syntax.

  • I wonder if a smart custom filter operator, or some other method can get the desired outcome?
  • Then how can we do the other logical operators.

[Edited] You can also add :intersection[current.has.field[test]] to the above. so for AND use :intersection but I am not yet happy.

Like I suggested:

<% if <<my-var>> XOR [some-filter] %>
  ...
<% else %>
  ...
<% endif %>
1 Like

I understand but could we get OR/AND/XOR in our filters?

  • I have not yet tried combinations.