Changes

Jump to navigation Jump to search
no edit summary
=Conventions=
All functions in this module are assigned to the <code>{{Code|http://basex.org/modules/hof</code> }} namespace, which is statically bound to the <code>{{Code|hof</code> }} prefix.<br/>
=Functions=
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|hof:id</b>(|$expr as item()*) as |item()*</code>}}
|-
| '''Summary'''
| '''Examples'''
|
* <code>{{Code|hof:id(1 to 5)</code> }} returns <code>{{Code|1 2 3 4 5</code>}}
* With higher-order functions:
<pre class="brush:xquery">
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|hof:const</b>(|$expr as item()*, $ignored as item()*) as |item()*</code>}}
|-
| '''Summary'''
| '''Examples'''
|
* <code>{{Code|hof:const(42, 1337)</code> }} returns <code>{{Code|42</code>}}.
* With higher-order functions:
<pre class="brush:xquery">
)
</pre>
* Another use-case: When inserting a key into a map, <code>{{Code|$f</code> }} descides how to combine the new value with a possibly existing old one. <code>{{Code|hof:const</code> }} here means ignoring the old value, so that's normal insertion.
<pre class="brush:xquery">
let $insert-with := function($f, $map, $k, $v) {
)
</pre>
returns <code>{{Code|3 42</code>}}
|}
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|hof:fold-left1</b>(|$f as function(item()*, item()) as item()*, $seq as item()+) as |item()*</code>}}
|-
| '''Summary'''
| '''Examples'''
|
* <code>{{Code|hof:fold-left1(function($a, $b) { $a + $b }, 1 to 10)</code> }} returns <code>{{Code|55</code>}}.* <code>{{Code|hof:fold-left1(function($a, $b) { $a + $b }, ())</code> }} throws <code>{{Code|XPTY0004</code>}}, because <code>{{Code|$seq</code> }} has to be non-empty.
|}
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|hof:until</b>(|$pred as function(item()*) as xs:boolean, $f as function(item()*) as item()*, $start as item()*) as |item()*</code>}}
|-
| '''Summary'''
|Applies the function <code>{{Code|$f</code> }} to the initial value <code>{{Code|$start</code> }} until the predicate <code>{{Code|$pred</code> }} applied to the result returns <code>{{Code|true()</code>}}.
|-
| '''Examples'''
|
* <code>{{Code|hof:until(function($x) { $x ge 1000 }, function($y) { 2 * $y }, 1)</code> }} returns <code>{{Code|1024</code>}}.
* Calculating the square-root of a number by iteratively improving an initial guess:
<pre class="brush:xquery">
return $sqrt(25)
</pre>
returns <code>{{Code|5.000000000053722</code>}}.
|}
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|hof:top-k-by</b>(|$seq as item()*, $sort-key as function(item()) as item(), $k as xs:integer) as |item()*</code>}}
|-
| '''Summary'''
|Returns the <code>{{Code|$k</code> }} items in <code>{{Code|$seq</code> }} that are greatest when sorted by the result of <code>{{Code|$f</code> }} applied to the item. The function is a much more efficient implementation of the following scheme:
<pre class="brush:xquery">
(
| '''Examples'''
|
* <code>{{Code|hof:top-k-by(1 to 1000, hof:id#1, 5)</code> }} returns <code>{{Code|1000 999 998 997 996</code>}}* <code>{{Code|hof:top-k-by(1 to 1000, function($x) { -$x }, 3)</code> }} returns <code>{{Code|1 2 3</code>}}* <code>{{Code|hof:top-k-by(<x a='1' b='2' c='3'/>/@*, xs:integer#1, 2)/node-name()</code> }} returns <code>{{Code|c b</code>}}
|}
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|hof:top-k-with</b>(|$seq as item()*, $lt as function(item(), item()) as xs:boolean, $k as xs:integer) as |item()*</code>}}
|-
| '''Summary'''
|Returns the <code>{{Code|$k</code> }} items in <code>{{Code|$seq</code> }} that are greatest when sorted in the order of the ''less-than'' predicate <code>{{Code|$lt</code>}}. The function is a general version of <code>{{Code|hof:top-k-by($seq, $sort-key, $k)</code>}}.
|-
| '''Examples'''
|
* <code>{{Code|hof:top-k-with(1 to 1000, function($a, $b) { $a lt $b }, 5)</code> }} returns <code>{{Code|1000 999 998 997 996</code>}}* <code>{{Code|hof:top-k-with(-5 to 5, function($a, $b) { abs($a) gt abs($b) }, 5)</code> }} returns <code>{{Code|0 1 -1 2 -2</code>}}
|}
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu