View Single Post
Old 03-17-2025, 12:50 PM   #18
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,475
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by DrChiper View Post
The reason I didn't use intermediate vars is because I noticed that the formatter stops processing/evaluating in case of a nil situation, so this seems to be fastest way to process. But as always, correct me if I'm wrong
This is called "shortcutting". If the formatter can determine that a "then" or "else" clause cannot be evaluated then it skips it. The same thing happens with "and" and "or" expressions. For example, given the expression
Code:
'1' || print('foo')
will never print 'foo' because the result of the || is true no matter what the right-hand side does. On the other hand
Code:
'1' && print('foo')
will always print 'foo' because both the left-hand and the right-hand must be true for the expression to be true.
Quote:
Code:
program:
	if (field('title')!=transliterate(field('title')))
	then
		if (field('#subtitle')!=transliterate(field('#subtitle')))
		then
			'true1, true2'
		else
			'true1'
		fi
	else
		if (field('#subtitle')!=transliterate(field('#subtitle')))
		then
			'true2'
		else
			'false'
		fi
	fi
It returns true1 for title, true2 for subtitle, or false when neither contains non-ASCII chars.
FWIW and perhaps doing premature optimization: this will be a bit faster because a) local variables are faster than field lookups, and b) switch_if is faster than a series of ifs.
Code:
program:
# These two lines depend on evaluation being left to right so the
# variable is set before use.
	title_neq = (t = $title) != transliterate(t);
	authors_neq = (a = $authors) != transliterate(a);

# This code works because given two booleans the "truth table" has four entries.
# The order of evaluation is important. As it is we can avoid extra tests because
# if the first test isn't true then one or both of title_neq and authors_neq 
# must be false. That is why the second and third line don't need to check the
# other variable. The switch_if function does shortcutting.

	switch_if(
		title_neq && authors_neq, 'true1, true2',
		title_neq, 'true1',
		authors_neq, 'true2',
		'false')
The switch_if's arguments can be reordered if one pathway, probably resulting in 'false', is the most common. For example:
Code:
program:
	title_neq = (t = $title) != transliterate(t);
	authors_neq = (a = $authors) != transliterate(a);

	switch_if(
# True if both are false
		!(title_neq || authors_neq), 'false',
# True if both are true
		title_neq && authors_neq, 'true1, true2',
# Here, one but not both of title_neq and authors_neq is true.
		title_neq, 'true1',
# title_neq was false so authors_neq must be true.
		'true2')
chaley is offline   Reply With Quote