View Single Post
Old 01-06-2023, 05:26 AM   #454
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,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Thanks. Another question: what is the difference, if any, between using a first_non_empty and an if-then-elif in a situation like this?

first_non_empty is a bit easier for me (less misplaced fi's, for one!) so I'm inclined to keep it unless there's some large advantage to the other.

Code:
program:

	if $#booktype == 'Fanfiction' then
		first_non_empty(
			if 
				$#fanficstatus == 'In-Progress' 
				&& days_between(format_date(today(), 'yyyy-MM-dd'), $#fanficupdated) >= 365 
			then 
				'clock.png' 
			fi,
			
			if 
				!'^cover:' inlist $#admintags 
			then 
				'test' 
			fi
		)
	fi
Code:
program:

	if $#booktype == 'Fanfiction' then
		if 
			$#fanficstatus == 'In-Progress' 
			&& days_between(format_date(today(), 'yyyy-MM-dd'), $#fanficupdated) >= 365 
		then
 			'clock.png'

		elif 
			!'^cover:' inlist $#admintags 
		then
			'test'
		fi
	fi
The list of ifs is slightly faster (less than microseconds), not enough to worry about.

I have wondered whether I should make a variant that combines first_non_empty() and switch(), perhaps named first_true(). It would take a list of argument pairs where each pair is a condition expression and a value expression. For each pair in order it would evaluate the condition. If True then it would evaluate the value expression and return it, otherwise try the next pair. There would be a 'none are true' value at the end, like switch().

This would be faster than either first_non_empty() or a series of ifs, and would probably be easier to read. But it would be yet another maintenance and documentation chore. It is also quite easy to do in a python template. I am still thinking about it.
chaley is offline   Reply With Quote