27 March 2022 (In calibre version 5.40)
- The ability to define local functions. For example, the following example computes an approximate duration in years, months, days from a number of days. It uses the defined local function to_plural() to format the numbers for output.
Code:
program:
days = 2112;
years = floor(days/360);
months = floor(mod(days, 360)/30);
days = days - ((years*360) + (months * 30));
def to_plural(v, str):
if v == 0 then return '' fi;
return v & ' ' & (if v == 1 then str else str & 's' fi) & ' '
fed;
to_plural(years, 'year') & to_plural(months, 'month') & to_plural(days, 'day')
Notes:- The grammar for defining a function is
Code:
function ::= 'def' function_name '(' argument_expr [',' argument_expr]* ')' ':'
expression_list 'fed'
argument_expr ::= identifier | identifier '=' expression
- Functions must be defined before they are used.
- The outer scope local variables are not visible inside a function.
- The parameters in the definition can have default values.
- You can call a function with fewer arguments than defined parameters. Parameters matched to 'missing' arguments are given their default value or the empty string if there isn't a default value.
- A 'concatenate strings' operator '&'. The expression
Code:
'aaa' & 'bbb' & 'ccc'
is equivalent to
Code:
strcat('aaa', 'bbb', 'ccc')