Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 05-13-2022, 11:34 AM   #16
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by kovidgoyal View Post
You could have both, in the for loop context make range special and everywhere else have it return a list. Reporting an error when limit is exceeded seems better to me.

If you do have range be a special function in the for context, you can statically determine when limit is excedded instead of checking on every iteration.
Done, using a context-sensitive range() function.

The grammar for for loops:
Code:
    for_expr        ::= for_list | for_range
    for_list        ::= 'for' identifier 'in' list_expr
                        [ 'separator' separator_expr ] ':' expression_list 'rof'
    for_range       ::= 'for' identifier 'in' range_expr ':' expression_list 'rof'
    range_expr      ::= 'range' '(' [ start_expr ',' ] stop_expr
                        [ ',' step_expr [ ',' limit_expr ] ] ')'
    list_expr       ::= top_expression
    separator_expr  ::= top_expression
    start_expr      ::= top_expression
    stop_expr       ::= top_expression
    step_expr       ::= top_expression
    limit_expr      ::= top_expression
In the context of a for loop the range() function uses the Python 3 range function, with the exception that the limit parameter is added to prevent (near-)infinite loops. Assuming that step is positive, generation stops when
Code:
current_value + step >= stop
The list is empty (no iteration) if start >= stop. No list is actually generated. In any other function call context it generates and returns the resulting list.

The example in post 10 is now expressed as
Code:
program:
	res = '';
	for i in range(strlen($title)):
	    c = substr($title, i, i+1);
	    res = strcat(res, if mod(i, 2) == 0 then uppercase(c) else c fi)
	rof
or using all the range() parameters:
Code:
program:
	res = '';
	for i in range(0, strlen($title), 1, 100):
	    c = substr($title, i, i+1);
	    res = strcat(res, if mod(i, 2) == 0 then uppercase(c) else c fi)
	rof
Range() documentation is:
Quote:
range(start, stop, step, limit) -- returns a list of numbers generated by looping over the range specified by the parameters start, stop, and step, with a maximum length of limit. The first value produced is start. Subsequent values next_v = current_v + step. The loop continues while next_v < stop assuming step is positive, otherwise while next_v > stop. An empty list is produced if start fails the test: start >= stop if step is positive. The limit sets the maximum length of the list and has a default of 1000. The parameters start, step, and limit are optional. Calling range() with one argument specifies stop. Two arguments specify start and stop. Three arguments specify start, stop, and step. Four arguments specify start, stop, step and limit.

Examples:
Code:
range(5) -> '0, 1, 2, 3, 4'
range(0, 5) -> '0, 1, 2, 3, 4'
range(-1, 5) -> '-1, 0, 1, 2, 3, 4'
range(1, 5) -> '1, 2, 3, 4'
range(5, 0, -1) -> '5, 4, 3, 2, 1'
range(1, 5, 2) -> '1, 3'
range(1, 5, 2, 5) -> '1, 3'
range(1, 5, 2, 1) -> error(limit exceeded)

Last edited by chaley; 05-13-2022 at 11:38 AM.
chaley is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Template: Converting a search & replace into a template ownedbycats Library Management 11 03-26-2021 04:32 AM
Request for comments: new template language operations chaley Library Management 3 02-27-2021 12:09 PM
Request: Match Calibre Filename template recipe cbook7 Library Management 10 06-05-2020 05:11 AM
Request: template-making assistance for column built from other columns iienderii Library Management 9 04-04-2016 10:27 PM
Request for Feedback on E-book Web Template andreasw Writers' Corner 5 05-14-2011 12:34 AM


All times are GMT -4. The time now is 10:55 PM.


MobileRead.com is a privately owned, operated and funded community.