Changes

Jump to navigation Jump to search
4,633 bytes added ,  01:06, 1 March 2012
Created page with "This module adds some useful higher-order functions that were left out of the official spec. All functions are introduced with the <code>hof:</code> prefix, which is linked to th..."
This module adds some useful higher-order functions that were left out of the official spec. All functions are introduced with the <code>hof:</code> prefix, which is linked to the statically declared <code>http://basex.org/modules/hof</code> namespace.

=Functions=

==hof:id==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>hof:id</b>($expr as item()*) as item()*</code>
|-
| valign='top' | '''Summary'''
|Returns its argument unchanged. This function isn't useful on its own, but can be used as argument to other higher-order functions.
|-
| valign='top' | '''Examples'''
|
* <code>hof:id(1 to 5)</code> returns <code>1 2 3 4 5</code>
|}

==hof:const==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>hof:const</b>($expr as item()*, $ignored as item()*) as item()*</code>
|-
| valign='top' | '''Summary'''
|Returns its first argument unchanged and irgores the second. This function isn't useful on its own, but can be used as argument to other higher-order functions.
|-
| valign='top' | '''Examples'''
|
* <code>hof:const(42, 1337)</code> returns <code>42</code>.
|}

==hof:fold-left1==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>hof:fold-left1</b>($f as function(item()*, item()) as item()*, $seq as item()+) as item()*</code>
|-
| valign='top' | '''Summary'''
|Works the same as [[Higher-Order_Functions#fn:fold-left($f, $seed, $seq)|fn:fold-left($f, $seed, $seq)]], but doesn't need a seed, because the sequence must be non-empty.
|-
| valign='top' | '''Errors'''
|''XPTY0004'' if <code>$seq</code> is empty
|-
| valign='top' | '''Examples'''
|
* <code>hof:fold-left1(function($a, $b) { $a + $b }, 1 to 10)</code> returns <code>55</code>.
* <code>hof:fold-left1(function($a, $b) { $a + $b }, ())</code> throws <code>XPTY0004</code>, because <code>$seq</code> has to be non-empty.
|}

==hof:until==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>hof:until</b>($pred as function(item()*) as xs:boolean, $f as function(item()*) as item()*, $start as item()*) as item()*</code>
|-
| valign='top' | '''Summary'''
|Applies the function <code>$f</code> to the initial value <code>$start</code> until the predicate <code>$pred</code> applied to the result returns <code>true()</code>.
|-
| valign='top' | '''Examples'''
|
* <code>hof:until(function($x) { $x ge 1000 }, function($y) { 2 * $y }, 1)</code> returns <code>1024</code>.
* Calculating the square-root of a number by iteratively improving an initial guess:
<pre class="brush:xquery">
let $sqrt := function($x as xs:double) as xs:double {
hof:until(
function($res) { abs($res * $res - $x) < 0.00001 },
function($guess) { ($guess + $x div $guess) div 2 },
$x
)
}
return $sqrt(25)
</pre>
returns <code>5.000000000053722</code>.
|}

==hof:top-k-by==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>hof:top-k-by</b>($sort-key as function(item()) as item(), $k as xs:integer, $seq as item()*) as item()*</code>
|-
| valign='top' | '''Summary'''
|Returns the <code>$k</code> items in <code>$seq</code> that are greatest when sorted by the result of <code>$f</code> applied to the item. The function is a much more efficient implementation of the following scheme:
<pre class="brush:xquery">
(
for $x in $seq
order by $sort-key($x)
return $x
)[position() <= $k]
</pre>
|-
| valign='top' | '''Errors'''
|''XPTY0004'' if <code>$sort-key</code> doesn't return exactly one item
|-
| valign='top' | '''Examples'''
|
* <code>hof:top-k-by(hof:id#1, 5, 1 to 1000)</code> returns <code>1000 999 998 997 996</code>
* <code>hof:top-k-by(function($x) { -$x }, 3, 1 to 1000)</code> returns <code>1 2 3</code>
* <code>hof:top-k-by(xs:integer#1, 2, <x a='1' b='2' c='3'/>/@*)/node-name()</code> returns <code>c b</code>
|}

==hof:top-k-with==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>hof:top-k-with</b>($lt as function(item()?, item()?) as xs:boolean, $k as xs:integer, $seq as item()*) as item()*</code>
|-
| valign='top' | '''Summary'''
|Returns the <code>$k</code> items in <code>$seq</code> that are greatest when sorted in the order of the ''less-than'' predicate <code>$lt</code>. The function is a general version of <code>hof:top-k-by($sort-key, $k, $seq)</code>.
|-
| valign='top' | '''Examples'''
|
* <code>hof:top-k-with(function($a, $b) { $a lt $b }, 5, 1 to 1000)</code> returns <code>1000 999 998 997 996</code>
* <code>hof:top-k-with(function($a, $b) { abs($a) gt abs($b) }, 5, -5 to 5)</code> returns <code>0 1 -1 2 -2</code>
|}

=Recent Changes=

* removed <code>hof:iterate</code>
* added <code>hof:top-k-by</code> and <code>hof:top-k-with</code>

[[Category:XQuery]]
editor, reviewer
33

edits

Navigation menu