Changes

Jump to navigation Jump to search
m
Text replacement - "<syntaxhighlight lang="xquery">" to "<pre lang='xquery'>"
This [[Module Library|XQuery Module]] adds some useful higher-order functions, additional to the [[Higher-Order Functions]] provided by the official specification.
 
With {{Announce|Version 11}}, many functions have been removed in favor of new features of XQuery 4:
 
{|
|- valign="top"
| '''BaseX 10'''
| '''XQuery 4'''
|- valign="top"
| {{Code|hof:drop-while}}
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-drop-while <code>fn:items-starting-where</code>]
|- valign="top"
| {{Code|hof:id}}
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-identity <code>fn:identity</code>]
|- valign="top"
| {{Code|hof:until}}
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-iterate-while <code>fn:iterate-while</code>]
|- valign="top"
| {{Code|hof:take-while}}
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-take-while <code>fn:items-before</code>]
|}
=Conventions=
All functions in this module are assigned to the <code><nowiki>http://basex.org/modules/hof</nowiki></code> namespace, which is statically bound to the <code>{{Code|hof</code> }} prefix.<br/>
=FunctionsLoops=
==hof:idfold-left1== {|width='100%'|-valign="top"| width='90120' | '''SignaturesSignature'''|<code><bpre>hof:id</b>fold-left1( $input as item()+, $expr action as function(item()*, item()) as item()*) as item()*</codepre>|-valign="top"
| '''Summary'''
|Returns its argument unchanged. This function isn't useful on its ownWorks the same as [[Higher-Order Functions#fn:fold-left|fn:fold-left]], but can does not need a seed, because the sequence must be used as argument to other highernon-order functionsempty.|-valign="top"
| '''Examples'''
|
* <code>{{Code|hof:idfold-left1(1 to 5)</code> returns <code>1 2 3 4 5</code>* With higher-order functions:<pre class="brush:xquery">let $sort-by := 10, function($fa, $seqb) { for $x in a + $seq order by $f($xb }) return $x }} returns {{Code|55}}.let $sort * {{Code|hof:= $sortfold-byleft1((hof:id#1, ?), $reverse-sort := $sort-by(function($xa, $b) { -$x a + $b }, ?)return ( $sort((1}} throws {{Code|XPTY0004}}, 5, 3, 2, 4)), 'because {{Code|', $reverseseq}} has to be non-sort((1, 5, 3, 2, 4)))</pre>returns: <code>1 2 3 4 5 | 5 4 3 2 1</code>empty.
|}
==hof:constscan-left== {|width='100%'|-valign="top"| width='90120' | '''SignaturesSignature'''|<code><bpre>hof:const</b>scan-left( $input as item()*, $expr zero as item()*, $ignored action as function(item()*, item()) as item()*) as item()*</codepre>|-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 is similar to other higher[[Higher-Order Functions#fn:fold-left|fn:fold-order functionsleft]], e.g. when but it returns a function combining two list of successive reduced values from the left. It is expected and one only wants equivalent to retain the :<pre lang='xquery'>declare function hof:scan-left($input, $acc, $action) { if(empty($input)) then $acc else ( $acc, hof:scan-left one.(tail($input), $action($acc, head($input)), $action) )};</pre>|-valign="top"
| '''Examples'''
|
* <code>hof:const(42, 1337)</code> returns <code>42</code>.* With higher-order functionsReturns triangular numbers:<pre classlang="brush:'xquery"'>let $zip-sum := function($f, $seq1, $seq2) { sum(map-pairs($f, $seq1, $seq2)) }let $sum-all := $zip-sum(function($a, $b) { $a + $b }, ?, ?), $sum-left := $zip-sum(hof:const#2, ?, ?)return ( $sum-all((1, 1, 1, 1, 1), 1 to 5), $sumscan-left((1, 1, 1, 1, 1), 1 to 5))</pre>* Another use-case: When inserting a key into a map10, <code>$f</code> descides how to combine the new value with a possibly existing old one. <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) { let $old := $map($k), $new := if($old) then $f($v0, $old) else $v return map:new(($map, map{ $k := $new })) }let $map := map{ 'foo' := 1 }let $add := $insert-with(function($a, $b) {$a + $b}, ?, ?, ?), $insert := $insert-with(hof:const#2, ?, ?, ?)return ( $add($map, 'foo', 2)('foo'), $insert($map, 'foo', 42)('foo'))
</pre>
returns <code>3 42</code>
|}
==hof:fold-left1=Sorting={||-| width='90' | '''Signatures'''|<code><b>=hof:foldtop-left1</b>($f as function(item()*, item()) as item()*, $seq as item()+) as item()*</code>|k-| '''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.|-| '''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.|}by==
==hof:until=={|width='100%'|-valign="top"| width='90120' | '''SignaturesSignature'''|<code><bpre>hof:until</b>top-k-by( $pred input as function(item()*) as xs:boolean, $f key as function(item()*) as item()*, $start k as item()*xs:integer) as item()*</codepre>|-valign="top"
| '''Summary'''
|Applies Returns the function <code>{{Code|$f</code> to the initial value <code>k}} items in {{Code|$start</code> until input}} that are greatest when sorted by the predicate <code>result of {{Code|$pred</code> key}} applied to the result returns item. The function is a much more efficient implementation of the following scheme:<codepre lang='xquery'>true(for $item in $input order by $key($item) descending return $item)[position()<= $k]</codepre>.|-valign="top"
| '''Examples'''
|
* <code>{{Code|hof:untiltop-k-by(function($x1 to 1000, hof:id#1, 5) }} returns { $x ge {Code|1000 999 998 997 996}, 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 { {Code|hof:untiltop-k-by( 1 to 1000, function($resx) { abs($res * $res - $x) < 0.00001 }, function($guess3) }} returns {{ ($guess + $x div $guess) div Code|1 2 3}, $x )}return $sqrt* <code>hof:top-k-by(25)<x a='1' b='2' c='3'/pre>returns <code>5.000000000053722/@*, xs:integer#1, 2)/node-name()</code>.returns {{Code|c b}}
|}
==hof:top-k-bywith== {{Mark|Introduced with Version 7.2:}}
{|width='100%'|-valign="top"| width='90120' | '''SignaturesSignature'''|<code><bpre>hof:top-k-by</b>with( $seq input as item()*, $sort-key comparator as function(item()) as , item()) as xs:boolean, $k as xs:integer) as item()*</codepre>|-valign="top"
| '''Summary'''
|Returns the <code>{{Code|$k</code> }} items in <code>{{Code|$seq</code> input}} that are greatest when sorted by in the result order of <code>the ''less-than'' predicate {{Code|$f</code> applied to the itemcomparator}}. The function is a much more efficient implementation general version of the following scheme:<pre class="brush{{Function||hof:xquery">( for $x in $seq order by $sorttop-key($x) descending return $x)[position() <= $k]</pre>-by}}.|-valign="top"
| '''Examples'''
|
* <code>{{Code|hof:top-k-bywith(1 to 1000, hof:id#1function($a, $b) { $a lt $b }, 5)</code> }} returns <code>{{Code|1000 999 998 997 996</code>}}* <code>{{Code|hof:top-k-bywith(1 -5 to 10005, function($xa, $b) { -abs($a) gt abs($x b) }, 35)</code> }} returns <code>{{Code|0 1 2 3</code>* <code>hof:top-k-by(<x a='1' b='2' c='3'/>/@*, xs:integer#1, -2)/node-name()</code> returns <code>c b</code>}}
|}
==hof:top-k-with=Identity=
{{Mark|Introduced with Version 7.2==hof:}}const==
{|width='100%'|-valign="top"| width='90120' | '''SignaturesSignature'''|<code><bpre>hof:top-k-with</b>const( $seq input as item()*, $lt ignore as function(item(), item()) as xs:boolean, $k as xs:integer*) as item()*</codepre>|-valign="top"
| '''Summary'''
|Returns its first argument unchanged and ignores 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>second. The This function is a general version of <code>hof:topisn’t useful on its own, but can be used as argument to other higher-k-by($seqorder functions, $sort-keye.g., $k)</code>when a function combining two values is expected and one only wants to retain the left one.|-valign="top"
| '''Examples'''
|
* {{Code|hof:const(42, 1337)}} returns {{Code|42}}.* With higher-order functions:<codepre lang='xquery'>hoflet $zip-sum :top= function($f, $seq1, $seq2) { sum(for-keach-withpair(1 to 1000$seq1, $seq2, $f))}let $sum-all := $zip-sum(function($a, $b) { $a lt + $b }, ?, ?)let $sum-left := $zip-sum(hof:const#2, ?, ?)return ( $sum-all((1, 1, 1, 1, 1), 1 to 5), $sum-left((1, 1, 1, 1, 1), 1 to 5))</code> returns <code>1000 999 998 997 996</codepre>* Another use-case: When inserting a key into a map, {{Code|$f}} decides how to combine the new value with a possibly existing old one. {{Code|hof:const}} here means ignoring the old value, so that's normal insertion.<codepre lang='xquery'>hoflet $insert-with := function($f, $map, $k, $v) { let $old := $map($k) let $new := if($old) then $f($v, $old) else $v return map:merge(($map, map:top-entry($k, $new)))}let $map := map { 'foo': 1 }let $add := $insert-with(-5 to 5, function($a, $b) { abs($a) gt abs(+ $b) }, 5?, ?, ?)</code> returns <code>0 1 let $ins := $insert-1 with(hof:const#2 -, ?, ?, ?)return ( $add($map, 'foo', 2)('foo'), $ins($map, 'foo', 42)('foo'))</codepre>returns {{Code|3 42}}
|}
=Changelog=
===;Version 711.2===0* Removed: {{Code|hof:until}} (replaced with {{Code|fn:iterate-while}}, {{Code|hof:if}} (replaced with {{Code|fn:identity}}, {{Code|hof:drop-while}} (replaced with {{Code|fn:items-starting-where}}), {{Code|hof:take-while}} (replaced with {{Code|fn:items-before}}) ;Version 9.5* Added: {{Function||hof:drop-while}}
;Version 8.1* Added: [[#hof:top-k-by{{Function||hof:topscan-k-by]]left}}, [[#hof:top-k-with{{Function||hof:toptake-k-with]]* Removed: [[#hof:iterate|hof:iterate]]while}}
===;Version 7.0===2* Added: {{Function||hof:top-k-by}}, {{Function||hof:top-k-with}}* Removed: hof:iterate
;Version 7.0
* module added
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu