View Single Post
Old 03-26-2022, 03:25 PM   #363
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,499
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Yes, I wasn't going for an exact number -- just a rough idea of how long it would be. Thanks.
I got back in front of a computer. Here is a tested version.
Code:
program:
	days = 912;
	years = if (x = floor(days/360)) > 0 then x else '' fi;
	months = if (x = floor(mod(days, 360)/30)) > 0 then x else '' fi;
	days = if (x = days - ((years*360) + (months * 30))) > 0 then x else ''fi;
	strcat(
		if years then strcat (years, if years == 1 then ' year ' else ' years ' fi) fi,
		if months then strcat (months, if months == 1 then ' month ' else ' months ' fi) fi,
		if days then strcat (days, if days == 1 then ' day ' else ' days ' fi) fi
	)
The repeated code to make the plural bothers me, as it has before. I am going to look at defining functions. Something like this, which doesn't yet work of course.
Code:
program:
	days = 912;
	years = if (x = floor(days/360)) > 0 then x else '' fi;
	months = if (x = floor(mod(days, 360)/30)) > 0 then x else '' fi;
	days = if (x = days - ((years*360) + (months * 30))) > 0 then x else ''fi;

	function to_plural(v, str) {
		if v == 0 then return '' fi;
		p = if v == 1 then str else strcat(str, 's') fi;
		strcat(v, ' ', p, ' ')
	}

	strcat(
		to_plural(years, 'year'),
		to_plural(months, 'month'),
		to_plural(days 'day')
	)
The keyword "function" creates a "temporary" stored template with the function name. The arguments() call will be automatically generated from the argument list. For various reasons these temporary stored templates can't be recursive, which is true of 'real' stored templates as well.

I haven't worked out what happens if there is a name collision. It is likely that I won't allow them to avoid nested symbol tables and all that comes with those.

I am also getting tired of writing
Code:
strcat('foo', 'bar')
and am considering making an inline form, possibly
Code:
'foo' & 'bar'
chaley is offline   Reply With Quote