Difference between revisions of "Higher-Order Functions Module"

From BaseX Documentation
Jump to navigation Jump to search
m (Text replacement - "<syntaxhighlight lang="xquery">" to "<pre lang='xquery'>")
Tags: Mobile web edit Mobile edit
 
(77 intermediate revisions by 6 users not shown)
Line 1: Line 1:
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.
+
This [[Module Library|XQuery Module]] adds some useful higher-order functions, additional to the [[Higher-Order Functions]] provided by the official specification.
  
=Functions=
+
With {{Announce|Version 11}}, many functions have been removed in favor of new features of XQuery 4:
  
==hof:id==
 
 
{|
 
{|
|-
+
|- valign="top"
| valign='top' width='90' | '''Signatures'''
+
| '''BaseX 10'''
|<code><b>hof:id</b>($expr as item()*) as item()*</code>
+
| '''XQuery 4'''
|-
+
|- valign="top"
| valign='top' | '''Summary'''
+
| {{Code|hof:drop-while}}
|Returns its argument unchanged. This function isn't useful on its own, but can be used as argument to other higher-order functions.
+
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-drop-while <code>fn:items-starting-where</code>]
|-
+
|- valign="top"
| valign='top' | '''Examples'''
+
| {{Code|hof:id}}
|
+
| [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-identity <code>fn:identity</code>]
* <code>hof:id(1 to 5)</code> returns <code>1 2 3 4 5</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>]
 
|}
 
|}
  
==hof:const==
+
=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|hof}} prefix.<br/>
| valign='top' width='90' | '''Signatures'''
+
 
|<code><b>hof:const</b>($expr as item()*, $ignored as item()*) as item()*</code>
+
=Loops=
|-
 
| 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==
 
==hof:fold-left1==
{|
+
 
|-
+
{| width='100%'
| valign='top' width='90' | '''Signatures'''
+
|- valign="top"
|<code><b>hof:fold-left1</b>($f as function(item()*, item()) as item()*, $seq as item()+) as item()*</code>
+
| width='120' | '''Signature'''
|-
+
|<pre>hof:fold-left1(
| valign='top' | '''Summary'''
+
  $input  as item()+,
|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.
+
  $action  as function(item()*, item()) as item()*
|-
+
) as item()*</pre>
| valign='top' | '''Errors'''
+
|- valign="top"
|''XPTY0004'' if <code>$seq</code> is empty
+
| '''Summary'''
|-
+
|Works the same as [[Higher-Order Functions#fn:fold-left|fn:fold-left]], but does not need a seed, because the sequence must be non-empty.
| valign='top' | '''Examples'''
+
|- valign="top"
 +
| '''Examples'''
 
|
 
|
* <code>hof:fold-left1(function($a, $b) { $a + $b }, 1 to 10)</code> returns <code>55</code>.
+
* {{Code|hof:fold-left1(1 to 10, function($a, $b) { $a + $b })}} returns {{Code|55}}.
* <code>hof:fold-left1(function($a, $b) { $a + $b }, ())</code> throws <code>XPTY0004</code>, because <code>$seq</code> has to be non-empty.
+
* {{Code|hof:fold-left1((), function($a, $b) { $a + $b })}} throws {{Code|XPTY0004}}, because {{Code|$seq}} has to be non-empty.
 
|}
 
|}
  
==hof:until==
+
==hof:scan-left==
{|
+
 
|-
+
{| width='100%'
| valign='top' width='90' | '''Signatures'''
+
|- valign="top"
|<code><b>hof:until</b>($pred as function(item()*) as xs:boolean, $f as function(item()*) as item()*, $start as item()*) as item()*</code>
+
| width='120' | '''Signature'''
|-
+
|<pre>hof:scan-left(
| valign='top' | '''Summary'''
+
  $input  as item()*,
|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>.
+
  $zero    as item()*,
|-
+
  $action  as function(item()*, item()) as item()*
| valign='top' | '''Examples'''
+
) as item()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|This function is similar to [[Higher-Order Functions#fn:fold-left|fn:fold-left]], but it returns a list of successive reduced values from the left. It is equivalent to:
 +
<pre lang='xquery'>
 +
declare function hof:scan-left($input, $acc, $action) {
 +
  if(empty($input)) then $acc else (
 +
    $acc,
 +
    hof:scan-left(tail($input), $action($acc, head($input)), $action)
 +
  )
 +
};
 +
</pre>
 +
|- valign="top"
 +
| '''Examples'''
 
|
 
|
* <code>hof:until(function($x) { $x ge 1000 }, function($y) { 2 * $y }, 1)</code> returns <code>1024</code>.
+
* Returns triangular numbers:
* Calculating the square-root of a number by iteratively improving an initial guess:
+
<pre lang='xquery'>
<pre class="brush:xquery">
+
hof:scan-left(1 to 10, 0, function($a, $b) { $a + $b })
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>
 
</pre>
returns <code>5.000000000053722</code>.
 
 
|}
 
|}
 +
 +
=Sorting=
  
 
==hof:top-k-by==
 
==hof:top-k-by==
{|
+
 
|-
+
{| width='100%'
| valign='top' width='90' | '''Signatures'''
+
|- valign="top"
|<code><b>hof:top-k-by</b>($sort-key as function(item()) as item(), $k as xs:integer, $seq as item()*) as item()*</code>
+
| width='120' | '''Signature'''
|-
+
|<pre>hof:top-k-by(
| valign='top' | '''Summary'''
+
  $input  as item()*,
|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:
+
  $key   as function(item()) as item(),
<pre class="brush:xquery">
+
  $k     as xs:integer
(
+
) as item()*</pre>
  for $x in $seq
+
|- valign="top"
  order by $sort-key($x)
+
| '''Summary'''
  return $x
+
|Returns the {{Code|$k}} items in {{Code|$input}} that are greatest when sorted by the result of {{Code|$key}} applied to the item. The function is a much more efficient implementation of the following scheme:
 +
<pre lang='xquery'>
 +
(for $item in $input
 +
order by $key($item) descending
 +
return $item
 
)[position() <= $k]
 
)[position() <= $k]
 
</pre>
 
</pre>
|-
+
|- valign="top"
| valign='top' | '''Errors'''
+
| '''Examples'''
|''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(1 to 1000, hof:id#1, 5)}} returns {{Code|1000 999 998 997 996}}
* <code>hof:top-k-by(function($x) { -$x }, 3, 1 to 1000)</code> returns <code>1 2 3</code>
+
* {{Code|hof:top-k-by(1 to 1000, function($x) { -$x }, 3)}} returns {{Code|1 2 3}}
* <code>hof:top-k-by(xs:integer#1, 2, <x a='1' b='2' c='3'/>/@*)/node-name()</code> returns <code>c b</code>
+
* <code>hof:top-k-by(<x a='1' b='2' c='3'/>/@*, xs:integer#1, 2)/node-name()</code> returns {{Code|c b}}
 
|}
 
|}
  
 
==hof:top-k-with==
 
==hof:top-k-with==
{|
+
 
|-
+
{| width='100%'
| valign='top' width='90' | '''Signatures'''
+
|- valign="top"
|<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>
+
| width='120' | '''Signature'''
|-
+
|<pre>hof:top-k-with(
| valign='top' | '''Summary'''
+
  $input      as item()*,
|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>.
+
  $comparator  as function(item(), item()) as xs:boolean,
|-
+
  $k           as xs:integer
| valign='top' | '''Examples'''
+
) as item()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns the {{Code|$k}} items in {{Code|$input}} that are greatest when sorted in the order of the ''less-than'' predicate {{Code|$comparator}}. The function is a general version of {{Function||hof:top-k-by}}.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* {{Code|hof:top-k-with(1 to 1000, function($a, $b) { $a lt $b }, 5)}} returns {{Code|1000 999 998 997 996}}
 +
* {{Code|hof:top-k-with(-5 to 5, function($a, $b) { abs($a) gt abs($b) }, 5)}} returns {{Code|0 1 -1 2 -2}}
 +
|}
 +
 
 +
=Identity=
 +
 
 +
==hof:const==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>hof:const(
 +
  $input  as item()*,
 +
  $ignore  as item()*
 +
) as item()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns its first argument unchanged and ignores the second. This function isn’t useful on its own, but can be used as argument to other higher-order functions, e.g., when a function combining two values is expected and one only wants to retain the left one.
 +
|- 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:const(42, 1337)}} returns {{Code|42}}.
* <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>
+
* With higher-order functions:
 +
<pre lang='xquery'>
 +
let $zip-sum := function($f, $seq1, $seq2) {
 +
  sum(for-each-pair($seq1, $seq2, $f))
 +
}
 +
let $sum-all  := $zip-sum(function($a, $b) { $a + $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)
 +
)
 +
</pre>
 +
* 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.
 +
<pre lang='xquery'>
 +
let $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:entry($k, $new)))
 +
}
 +
let $map := map { 'foo': 1 }
 +
let $add := $insert-with(function($a, $b) { $a + $b }, ?, ?, ?)
 +
let $ins := $insert-with(hof:const#2, ?, ?, ?)
 +
return (
 +
  $add($map, 'foo', 2)('foo'),
 +
  $ins($map, 'foo', 42)('foo')
 +
)
 +
</pre>
 +
returns {{Code|3 42}}
 
|}
 
|}
  
=Recent Changes=
+
=Changelog=
 +
 
 +
;Version 11.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: {{Function||hof:scan-left}}, {{Function||hof:take-while}}
  
* removed <code>hof:iterate</code>
+
;Version 7.2
* added <code>hof:top-k-by</code> and <code>hof:top-k-with</code>
+
* Added: {{Function||hof:top-k-by}}, {{Function||hof:top-k-with}}
 +
* Removed: hof:iterate
  
[[Category:XQuery]]
+
;Version 7.0
 +
* module added

Latest revision as of 18:35, 1 December 2023

This XQuery Module adds some useful higher-order functions, additional to the Higher-Order Functions provided by the official specification.

With Version 11, many functions have been removed in favor of new features of XQuery 4:

BaseX 10 XQuery 4
hof:drop-while fn:items-starting-where
hof:id fn:identity
hof:until fn:iterate-while
hof:take-while fn:items-before

Conventions[edit]

All functions in this module are assigned to the http://basex.org/modules/hof namespace, which is statically bound to the hof prefix.

Loops[edit]

hof:fold-left1[edit]

Signature
hof:fold-left1(
  $input   as item()+,
  $action  as function(item()*, item()) as item()*
) as item()*
Summary Works the same as fn:fold-left, but does not need a seed, because the sequence must be non-empty.
Examples
  • hof:fold-left1(1 to 10, function($a, $b) { $a + $b }) returns 55.
  • hof:fold-left1((), function($a, $b) { $a + $b }) throws XPTY0004, because $seq has to be non-empty.

hof:scan-left[edit]

Signature
hof:scan-left(
  $input   as item()*,
  $zero    as item()*,
  $action  as function(item()*, item()) as item()*
) as item()*
Summary This function is similar to fn:fold-left, but it returns a list of successive reduced values from the left. It is equivalent to:
declare function hof:scan-left($input, $acc, $action) {
  if(empty($input)) then $acc else (
    $acc,
    hof:scan-left(tail($input), $action($acc, head($input)), $action)
  )
};
Examples
  • Returns triangular numbers:
hof:scan-left(1 to 10, 0, function($a, $b) { $a + $b })

Sorting[edit]

hof:top-k-by[edit]

Signature
hof:top-k-by(
  $input  as item()*,
  $key    as function(item()) as item(),
  $k      as xs:integer
) as item()*
Summary Returns the $k items in $input that are greatest when sorted by the result of $key applied to the item. The function is a much more efficient implementation of the following scheme:
(for $item in $input
 order by $key($item) descending
 return $item
)[position() <= $k]
Examples
  • hof:top-k-by(1 to 1000, hof:id#1, 5) returns 1000 999 998 997 996
  • hof:top-k-by(1 to 1000, function($x) { -$x }, 3) returns 1 2 3
  • hof:top-k-by(<x a='1' b='2' c='3'/>/@*, xs:integer#1, 2)/node-name() returns c b

hof:top-k-with[edit]

Signature
hof:top-k-with(
  $input       as item()*,
  $comparator  as function(item(), item()) as xs:boolean,
  $k           as xs:integer
) as item()*
Summary Returns the $k items in $input that are greatest when sorted in the order of the less-than predicate $comparator. The function is a general version of hof:top-k-by.
Examples
  • hof:top-k-with(1 to 1000, function($a, $b) { $a lt $b }, 5) returns 1000 999 998 997 996
  • hof:top-k-with(-5 to 5, function($a, $b) { abs($a) gt abs($b) }, 5) returns 0 1 -1 2 -2

Identity[edit]

hof:const[edit]

Signature
hof:const(
  $input   as item()*,
  $ignore  as item()*
) as item()*
Summary Returns its first argument unchanged and ignores the second. This function isn’t useful on its own, but can be used as argument to other higher-order functions, e.g., when a function combining two values is expected and one only wants to retain the left one.
Examples
  • hof:const(42, 1337) returns 42.
  • With higher-order functions:
let $zip-sum := function($f, $seq1, $seq2) {
  sum(for-each-pair($seq1, $seq2, $f))
}
let $sum-all  := $zip-sum(function($a, $b) { $a + $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)
)
  • Another use-case: When inserting a key into a map, $f decides how to combine the new value with a possibly existing old one. hof:const here means ignoring the old value, so that's normal insertion.
let $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:entry($k, $new)))
}
let $map := map { 'foo': 1 }
let $add := $insert-with(function($a, $b) { $a + $b }, ?, ?, ?)
let $ins := $insert-with(hof:const#2, ?, ?, ?)
return (
  $add($map, 'foo', 2)('foo'),
  $ins($map, 'foo', 42)('foo')
)

returns 3 42

Changelog[edit]

Version 11.0
  • Removed: hof:until (replaced with fn:iterate-while, hof:if (replaced with fn:identity, hof:drop-while (replaced with fn:items-starting-where), hof:take-while (replaced with fn:items-before)
Version 9.5
Version 8.1
Version 7.2
Version 7.0
  • module added