Skip to content

Commit 3c6c245

Browse files
committed
[refactor] make use of HoFs in test:apply
Seems to be a tad faster even.
1 parent f253d9f commit 3c6c245

File tree

1 file changed

+15
-23
lines changed
  • exist-core/src/main/resources/org/exist/xquery/lib/xqsuite

1 file changed

+15
-23
lines changed

exist-core/src/main/resources/org/exist/xquery/lib/xqsuite/xqsuite.xql

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -716,28 +716,17 @@ declare function test:apply($func as function(*), $meta as element(function), $a
716716
};
717717

718718
(:~
719-
: Call the given function with parameters taken from $args.
719+
: Call the given function with parameters taken from $args
720+
: since the items in the $args sequence are itself functions
721+
: apply them first.
722+
: The maximum number of items in $args is set to seven.
720723
:)
721-
declare %private function test:apply($func as function(*), $args as item()*) {
722-
switch (count($args))
723-
case 0 return
724-
$func()
725-
case 1 return
726-
$func($args[1]())
727-
case 2 return
728-
$func($args[1](), $args[2]())
729-
case 3 return
730-
$func($args[1](), $args[2](), $args[3]())
731-
case 4 return
732-
$func($args[1](), $args[2](), $args[3](), $args[4]())
733-
case 5 return
734-
$func($args[1](), $args[2](), $args[3](), $args[4](), $args[5]())
735-
case 6 return
736-
$func($args[1](), $args[2](), $args[3](), $args[4](), $args[5](), $args[6]())
737-
case 7 return
738-
$func($args[1](), $args[2](), $args[3](), $args[4](), $args[5](), $args[6](), $args[7]())
739-
default return
740-
error($test:WRONG_ARG_COUNT, "Function takes too many arguments")
724+
declare %private function test:apply($func as function(*), $args as function(*)*) {
725+
if (count($args) gt 7)
726+
then error($test:WRONG_ARG_COUNT, "Function takes too many arguments")
727+
else apply($func,
728+
array:for-each(array{ $args }, (: iterate over an array to supply the arguments to apply :)
729+
apply(?, []))) (: same as $args[$n]() :)
741730
};
742731

743732
declare %private function test:get-test-name($meta as element(function)) as xs:string {
@@ -752,8 +741,11 @@ declare %private function test:get-test-name($meta as element(function)) as xs:s
752741
(:~
753742
: Print out evaluation results for a given test.
754743
:)
755-
declare %private function test:print-result($meta as element(function), $result as item()*,
756-
$assertResult as element(report)*) {
744+
declare %private function test:print-result(
745+
$meta as element(function),
746+
$result as item()*,
747+
$assertResult as element(report)*
748+
) {
757749
<testcase name="{test:get-test-name($meta)}" class="{$meta/@name}">
758750
{
759751
if (exists($assertResult)) then

0 commit comments

Comments
 (0)