Changes

Jump to navigation Jump to search
69 bytes removed ,  22:04, 13 March 2012
no edit summary
| valign='top' | '''Summary'''
|Applies the function item <code>$f</code> to every element of the sequence <code>$seq</code> and returns all of the results as a sequence.
|-
| valign='top' | '''XQuery'''
|<pre class="brush:xquery">
declare function local:map(
$f as function(item()) as item()*,
$seq as item()*
) as item()* {
for $x in $seq
return $f($seq)
};
</pre>
|-
| valign='top' | '''Examples'''
''Result:'' <code>FOOBAR bar 6</code>
</li></ul>
|-
| valign='top' | '''XQuery 1.0'''
|<pre class="brush:xquery">
declare function local:map(
$f as function(item()) as item()*,
$seq as item()*
) as item()* {
for $x in $seq
return $f($seq)
};
</pre>
|}
| valign='top' | '''Summary'''
|Applies the boolean predicate <code>$pred</code> to all elements of the sequence <code>$seq</code>, returning those for which it returns <code>true()</code>.
|-
| valign='top' | '''XQuery'''
|<pre class="brush:xquery">
declare function local:filter(
$pred as function(item()) as xs:boolean,
$seq as item()*
) as item()* {
$seq[$pred(.)]
};
</pre>
|-
| valign='top' | '''Examples'''
<pre class="brush:xquery">
let $first-upper := function($str) {
let $first := fn:substring($str, 1, 1) return $first eq fn:upper-case($first) }
return fn:filter($first-upper, ('FooBar', 'foo', 'BAR'))
</pre>
<pre class="brush:xquery">
let $is-prime := function($x) {
$x gt 1 and (every $y in 2 to ($x - 1) satisfies $x mod $y ne 0) }
return filter($is-prime, 1 to 20)
</pre>
$seq
)
};
</pre>
|-
| valign='top' | '''XQuery 1.0'''
|<pre class="brush:xquery">
declare function local:filter(
$pred as function(item()) as xs:boolean,
$seq as item()*
) as item()* {
$seq[$pred(.)]
};
</pre>
| valign='top' | '''Summary'''
|''zips'' the elements from the two sequences <code>$seq1</code> and <code>$seq2</code> together with the function <code>$f</code>. It stops after the shorter sequence ends.
|-
| valign='top' | '''XQuery'''
|<pre class="brush:xquery">
declare function local:map-pairs(
$f as function(item(), item()) as item()*,
$seq1 as item()*,
$seq2 as item()*
) as item()* {
for $pos in 1 to min(length($seq1), length($seq2))
return $f($seq1[$pos], $seq2[$pos])
};
</pre>
|-
| valign='top' | '''Examples'''
<pre class="brush:xquery">
let $number-lines := function($str) {
fn:string-join( fn:map-pairs( concat(?, ': ', ?), 1 to 1000, tokenize($str, '\r?\n|\r') ), '&amp;#xa;' ) }
return $number-lines(
'hello world, how are you?'
)
</pre>
<pre class="brush:xquery">
let $is-sorted := function($seq) {
every $b in fn:map-pairs( function($a, $b) { $a le $b }, $seq, fn:tail($seq) ) satisfies $b }
return (
$is-sorted(1 to 10),
</pre>
''Result:'' <code>true false</code>
|-
| valign='top' | '''XQuery 1.0'''
|<pre class="brush:xquery">
declare function local:map-pairs(
$f as function(item(), item()) as item()*,
$seq1 as item()*,
$seq2 as item()*
) as item()* {
for $pos in 1 to min(length($seq1), length($seq2))
return $f($seq1[$pos], $seq2[$pos])
};
</pre>
</li></ul>
|}
<pre class="brush:xquery">
$f($f($f($f($f(0, 1), 2), 3), 4), 5)
</pre>
|-
| valign='top' | '''XQuery'''
|As folds are more general that ''FLWOR'' expressions, the implementation isn't as concise as the former ones:
<pre class="brush:xquery">
declare function local:fold-left(
$f as function(item()*, item()) as item()*,
$seed as item()*,
$seq as item()*
) as item()* {
if(empty($seq)) then $seed
else local:fold-left(
$f,
$f($seed, fn:head($seq)),
fn:tail($seq)
)
};
</pre>
|-
<pre class="brush:xquery">
let $product := fn:fold-left(
function($result, $i) { $result * $i }, 1, ? )
return $product(1 to 5)
</pre>
<pre class="brush:xquery">
let $from-digits := fold-left(
function($n, $d) { 10 * $n + $d }, 0, ? )
return (
$from-digits(1 to 5),
''Result:'' <code>12345 42</code>
</li></ul>
|-
| valign='top' | '''XQuery 1.0'''
|As folds are more general that ''FLWOR'' expressions, the implementation isn't as concise as the former ones:
<pre class="brush:xquery">
declare function local:fold-left(
$f as function(item()*, item()) as item()*,
$seed as item()*,
$seq as item()*
) as item()* {
if(empty($seq)) then $seed
else local:fold-left(
$f,
$f($seed, fn:head($seq)),
fn:tail($seq)
)
};
</pre>
|}
$f(1, $f(2, $f(3, $f(4, $f(5, 0)))))
</pre>
|-
| valign='top' | '''XQuery'''
|<pre class="brush:xquery">
declare function local:fold-right(
$f as function(item(), item()*) as item()*,
$seed as item()*,
$seq as item()*
) as item()* {
if(empty($seq)) then $seed
else $f(
fn:head($seq),
local:fold-right($f, $seed, tail($seq))
)
};
</pre>
Note that the order of the arguments of <code>$f</code> are inverted compared to that in <code>fn:fold-left(...)</code>.
|-
| valign='top' | '''Examples'''
<pre class="brush:xquery">
let $product := fn:fold-right(
function($i, $result) { $result * $i }, 1, ? )
return $product(1 to 5)
</pre>
<pre class="brush:xquery">
let $reverse := fn:fold-right(
function($item, $rev) { $rev, $item }, (), ? )
return $reverse(1 to 10)
</pre>
''Result:'' <code>10 9 8 7 6 5 4 3 2 1</code>
</li></ul>
|-
| valign='top' | '''XQuery 1.0'''
|<pre class="brush:xquery">
declare function local:fold-right(
$f as function(item(), item()*) as item()*,
$seed as item()*,
$seq as item()*
) as item()* {
if(empty($seq)) then $seed
else $f(
fn:head($seq),
local:fold-right($f, $seed, tail($seq))
)
};
</pre>
Note that the order of the arguments of <code>$f</code> are inverted compared to that in <code>fn:fold-left(...)</code>.
|}
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu