Skip to content

Commit

Permalink
Add examples of recursive methods
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelhkay committed Dec 22, 2023
1 parent 2856af5 commit ee2bccc
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion specifications/xquery-40/src/expressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9712,7 +9712,7 @@ return $incrementors[2](4)]]></eg>

<example id="id-example-method-updating">
<head>Example of an updating method</head>
<p>The following examble demonstrates how a method might be used to increase
<p>The following example demonstrates how a method might be used to increase
the size of a rectangle by a given factor.</p>

<eg><![CDATA[declare item type geo:rect as record(
Expand All @@ -9735,7 +9735,31 @@ declare function geo:rectangle(
<ednote><edtext>Depends on resolution of issue 296 (recursive record types)</edtext></ednote>

</example>

<p>Methods can be used to deliver recursive inline functions, as shown in the example below.</p>

<example id="id-example-method-recursve">
<head>Example of recursive method</head>
<p>The following example implements an anonymous function that computes
the product of sequence of numbers:</p>

<eg><![CDATA[let $product :=
map{"_product": %method fn($in) {
if (empty($in))
then 1
else head($in) * $self?_product(tail($in))}
}?_product
return $product((1.05, 1.04, 1.03))]]></eg>

<p>The next example illustrates a library of functions that are mutually
recursive:</p>

<eg><![CDATA[let $lib :=
map{"even": %method fn($val) {$val = 0 or $self?odd(abs($val)-1)},
"odd": %method fn($val) {not($self?even($val))}
}
return $lib?odd(23)]]></eg>
</example>

</div3>

Expand Down

0 comments on commit ee2bccc

Please sign in to comment.