Changes

Jump to navigation Jump to search
475 bytes added ,  23:09, 19 May 2013
The ''linear'' folds on sequences come in two flavours. They differ in the direction in which they traverse the sequence:
===fn:fold-left($f, $seed, $seq)=== {{Mark|Updated with Version 7.7:}} the <code>$seq</code> and <code>$fun</code> arguments have been swapped.<br/> 
{|
|-
| width='90' | '''Signatures'''
|<code><b>fn:fold-left</b>($f seq as item()*, $seed as item()*, $fun as function(item()*, item()) as item()*) as item()*</code><br /><font color='gray'>Old signature: fn:fold-left</b>($fun as function(item()*, item()) as item()*, $seed as item()*, $seq as item()*) as item()*</code><br /font>
|-
| '''Summary'''
|<ul><li>Product of a sequence of integers:
<pre class="brush:xquery">
let $product := fn:fold-left(?, 1, function($result, $i) { $result * $i }, 1, ?
)
return $product(1 to 5)
<li>Illustrating the evaluation order:
<pre class="brush:xquery">
fn:fold-left(1 to 5, '$seed',
concat('$f(', ?, ', ', ?, ')'),
'$seed',
1 to 5
)
</pre>
<li>Building a decimal number from digits:
<pre class="brush:xquery">
let $from-digits := fold-left(?, 0, function($n, $d) { 10 * $n + $d }, 0, ?
)
return (
<pre class="brush:xquery">
declare function local:fold-left(
$f as function(item()*, item()) seq as item()*,
$seed as item()*,
$seq fun as function(item()*, item()) as item()*
) as item()* {
if(empty($seq)) then $seed
else local:fold-left(
fn:tail($fseq), $ffun($seed, fn:head($seq)), fn:tail($seq)fun
)
};
|}
===fn:fold-right($f, $seed, $seq)=== {{Mark|Updated with Version 7.7:}} the {{Code|$seq}} and {{Code|$fun}} arguments have been swapped.<br/> 
{|
|-
| width='90' | '''Signatures'''
|<code><b>fn:fold-right</b>($f seq as item()*, $seed as item()*, $fun as function(item(), item()*) as item()*</code><br /><font color='gray'>Old signature: fn:fold-right</b>($fun as function(item(), item()*) as item()*, $seed as item()*, $seq as item()*) as item()*</code><br /font>
|-
| '''Summary'''
|The ''right fold'' <code>fn:fold-right($fseq, $seed, $seqfun)</code> traverses the from the right.The query <code>fn:fold-right($f1 to 5, 0, 1 to 5$f)</code> for example would be evaluated as:
<pre class="brush:xquery">
$f(1, $f(2, $f(3, $f(4, $f(5, 0)))))
|<ul><li>Product of a sequence of integers:
<pre class="brush:xquery">
let $product := fn:fold-right(?, 1, function($i, $result) { $result * $i }, 1, ?
)
return $product(1 to 5)
<li>Illustrating the evaluation order:
<pre class="brush:xquery">
fn:fold-right(1 to 5, '$seed', concat('$ffun(', ?, ', ', ?, ')'), '$seed', 1 to 5
)
</pre>
''Result:'' <code>$ffun(1, $f(2, $f(3, $f(4, $f(5, $seed)))))</code>
</li>
<li>Reversing a sequence of items:
|<pre class="brush:xquery">
declare function local:fold-right(
$f as function(item(), item()*) as item()*,
$seed as item()*,
$seq as item()*, $fun as function(item(), item()*) as item()*
) as item()* {
if(empty($seq)) then $seed
else $ffun(
fn:head($seq),
local:fold-right(tail($fseq), $seed, tail($seq)fun)
)
};
</pre>
Note that the order of the arguments of <code>$ffun</code> are inverted compared to that in <code>fn:fold-left(...)</code>.
|}
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu