fluffos/testsuite/std/reduce.lpc
Yucong Sun 592dfc0688 Prefer .lpc source extension; rename testsuite to .lpc
Source resolution is now explicit-extension-exact: load_object("/foo.c")
probes only foo.c, never foo.lpc (and vice versa); extension-less names
prefer .lpc and fall back to .c. Object identity stays extension-blind:
object names carry no extension, any spelling finds a loaded object, and
the registry is consulted before the filesystem. The caller's raw
spelling now flows through find_object()/inherit/master/simul_efun loads
instead of being pre-stripped away.

- filename_to_obname and otable basename() strip .lpc too (children())
- save_object() strips either source extension before appending .o
- replace_program()/function_exists() handle both suffixes
- testsuite: all LPC sources renamed to .lpc; runner globs, master
  get_include_path cases, and program-name assertions updated;
  README.md rewritten with the extension rules and suite conventions
- new single/tests/efuns/dual_extension.lpc pins exact-pick, fallback,
  no-crossover, identity, and registry-before-filesystem with .c/.lpc
  fixture pairs in /clone

Verified: ctest 297/297 and driver-autotest x3 (ASan Debug) plus
RelWithDebInfo ctest + autotest.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 20:48:48 -04:00

69 lines
2.2 KiB
Text

//:FUNCTION reduce
//Iterate over an <arr>, passing each element to a function <fun>.
//The function should return a new value which becomes the new
//total. Function <fun> can be a function or a string. If it is a
//string, it will call the named function in the calling object
//(via previous_object()).
//
//Parameters:
//(required) mixed *elements - the array to be worked upon
//(required) function | string fun - the reducer function
//(optional) mixed initial - the initial value to set
//(optional) mixed args... - additional arguments to pass to <fun>
//
//
//The following is passed to the function at every iteration:
//(required) mixed total - The initial value, or the previously returned value of the function
//(required) mixed currentValue - The value of the current element
//(optional) int currentIndex - The array index of the current element
//(optional) mixed *arr - The array the current element belongs to
//(optional) mixed args... - Additional arguments
//
//The function should return what will become the new total.
//
//:EXAMPLE
//reduce( ({ 1, 2, 3, 4 }), function( int total, int current ) {
// return total + current ;
//}) ;
////Result: 10
//
//reduce( ({ 1, 2, 3, 4 }), function( int total, int current ) {
// return total + current ;
//}, 10) ;
////Result: 20
//
//Inspired by Javascript's Array.reduce()
varargs mixed reduce( mixed *elements, mixed fun, mixed initial, mixed args... )
{
mixed result ;
int num_elements, element ;
if(nullp(elements)) error("Missing argument 1 to reduce.\n") ;
if(nullp(fun)) error("Missing argument 2 to reduce.\n") ;
result = initial ;
if( functionp( fun ) )
{
for( element = 0, num_elements = sizeof( elements ); element < num_elements; element++ )
{
result = evaluate( fun, result, elements[ element ], element, elements, args... ) ;
}
}
else if( stringp( fun ) )
{
object ob = previous_object() ;
for( element = 0, num_elements = sizeof( elements ); element < num_elements; element++ )
{
result = call_other ( ob, fun, result, elements[ element ], element, elements, args... ) ;
}
}
else
{
error("Bad argument 2 to reduce.\n") ;
}
return result ;
}