Difference between revisions of "Higher-Order Functions Module"
Jump to navigation
Jump to search
m (Text replace - "| valign='top' | " to "| ") |
|||
Line 9: | Line 9: | ||
|<code><b>hof:id</b>($expr as item()*) as item()*</code> | |<code><b>hof:id</b>($expr as item()*) as item()*</code> | ||
|- | |- | ||
− | + | | '''Summary''' | |
|Returns its argument unchanged. This function isn't useful on its own, but can be used as argument to other higher-order functions. | |Returns its argument unchanged. This function isn't useful on its own, but can be used as argument to other higher-order functions. | ||
|- | |- | ||
− | + | | '''Examples''' | |
| | | | ||
* <code>hof:id(1 to 5)</code> returns <code>1 2 3 4 5</code> | * <code>hof:id(1 to 5)</code> returns <code>1 2 3 4 5</code> | ||
Line 39: | Line 39: | ||
|<code><b>hof:const</b>($expr as item()*, $ignored as item()*) as item()*</code> | |<code><b>hof:const</b>($expr as item()*, $ignored as item()*) as item()*</code> | ||
|- | |- | ||
− | + | | '''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, e.g. when a function combining two values is expected and one only wants to retain the left one. | |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, e.g. when a function combining two values is expected and one only wants to retain the left one. | ||
|- | |- | ||
− | + | | '''Examples''' | |
| | | | ||
* <code>hof:const(42, 1337)</code> returns <code>42</code>. | * <code>hof:const(42, 1337)</code> returns <code>42</code>. | ||
Line 81: | Line 81: | ||
|<code><b>hof:fold-left1</b>($f as function(item()*, item()) as item()*, $seq as item()+) as item()*</code> | |<code><b>hof:fold-left1</b>($f as function(item()*, item()) as item()*, $seq as item()+) as item()*</code> | ||
|- | |- | ||
− | + | | '''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. | |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. | ||
|- | |- | ||
− | + | | '''Errors''' | |
|''XPTY0004'' if <code>$seq</code> is empty | |''XPTY0004'' if <code>$seq</code> is 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 }, 1 to 10)</code> returns <code>55</code>. | ||
Line 99: | Line 99: | ||
|<code><b>hof:until</b>($pred as function(item()*) as xs:boolean, $f as function(item()*) as item()*, $start as item()*) as item()*</code> | |<code><b>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>$f</code> to the initial value <code>$start</code> until the predicate <code>$pred</code> applied to the result returns <code>true()</code>. | |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>. | ||
|- | |- | ||
− | + | | '''Examples''' | |
| | | | ||
* <code>hof:until(function($x) { $x ge 1000 }, function($y) { 2 * $y }, 1)</code> returns <code>1024</code>. | * <code>hof:until(function($x) { $x ge 1000 }, function($y) { 2 * $y }, 1)</code> returns <code>1024</code>. | ||
Line 128: | Line 128: | ||
|<code><b>hof:top-k-by</b>($seq as item()*, $sort-key as function(item()) as item(), $k as xs:integer) as item()*</code> | |<code><b>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>$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: | |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"> | <pre class="brush:xquery"> | ||
Line 138: | Line 138: | ||
</pre> | </pre> | ||
|- | |- | ||
− | + | | '''Errors''' | |
|''XPTY0004'' if <code>$sort-key</code> doesn't return exactly one item | |''XPTY0004'' if <code>$sort-key</code> doesn't return exactly one item | ||
|- | |- | ||
− | + | | '''Examples''' | |
| | | | ||
* <code>hof:top-k-by(1 to 1000, hof:id#1, 5)</code> returns <code>1000 999 998 997 996</code> | * <code>hof:top-k-by(1 to 1000, hof:id#1, 5)</code> returns <code>1000 999 998 997 996</code> | ||
Line 157: | Line 157: | ||
|<code><b>hof:top-k-with</b>($seq as item()*, $lt as function(item(), item()) as xs:boolean, $k as xs:integer) as item()*</code> | |<code><b>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>$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($seq, $sort-key, $k)</code>. | |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($seq, $sort-key, $k)</code>. | ||
|- | |- | ||
− | + | | '''Examples''' | |
| | | | ||
* <code>hof:top-k-with(1 to 1000, function($a, $b) { $a lt $b }, 5)</code> returns <code>1000 999 998 997 996</code> | * <code>hof:top-k-with(1 to 1000, function($a, $b) { $a lt $b }, 5)</code> returns <code>1000 999 998 997 996</code> |
Revision as of 00:42, 26 May 2012
This XQuery Module adds some useful higher-order functions, additional to the Higher-Order Functions provided by the official specification. All functions are introduced with the hof:
prefix, which is linked to the statically declared http://basex.org/modules/hof
namespace.
Contents
Functions
hof:id
Signatures | hof:id($expr as item()*) as item()*
|
Summary | Returns its argument unchanged. This function isn't useful on its own, but can be used as argument to other higher-order functions. |
Examples |
let $sort-by := function($f, $seq) { for $x in $seq order by $f($x) return $x } let $sort := $sort-by(hof:id#1, ?), $reverse-sort := $sort-by(function($x) { -$x }, ?) return ( $sort((1, 5, 3, 2, 4)), '|', $reverse-sort((1, 5, 3, 2, 4)) ) returns: |
hof:const
Signatures | hof:const($expr as item()*, $ignored as item()*) as item()*
|
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, e.g. when a function combining two values is expected and one only wants to retain the left one. |
Examples |
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), $sum-left((1, 1, 1, 1, 1), 1 to 5) )
let $insert-with := function($f, $map, $k, $v) { let $old := $map($k), $new := if($old) then $f($v, $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') ) returns |
hof:fold-left1
Signatures | hof:fold-left1($f as function(item()*, item()) as item()*, $seq as item()+) as item()*
|
Summary | Works the same as fn:fold-left($f, $seed, $seq), but doesn't need a seed, because the sequence must be non-empty. |
Errors | XPTY0004 if $seq is empty
|
Examples |
|
hof:until
Signatures | hof:until($pred as function(item()*) as xs:boolean, $f as function(item()*) as item()*, $start as item()*) as item()*
|
Summary | Applies the function $f to the initial value $start until the predicate $pred applied to the result returns true() .
|
Examples |
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) returns |
hof:top-k-by
Signatures | hof:top-k-by($seq as item()*, $sort-key as function(item()) as item(), $k as xs:integer) as item()*
|
Summary | Returns the $k items in $seq that are greatest when sorted by the result of $f applied to the item. The function is a much more efficient implementation of the following scheme:
( for $x in $seq order by $sort-key($x) descending return $x )[position() <= $k] |
Errors | XPTY0004 if $sort-key doesn't return exactly one item
|
Examples |
|
hof:top-k-with
Signatures | hof:top-k-with($seq as item()*, $lt as function(item(), item()) as xs:boolean, $k as xs:integer) as item()*
|
Summary | Returns the $k items in $seq that are greatest when sorted in the order of the less-than predicate $lt . The function is a general version of hof:top-k-by($seq, $sort-key, $k) .
|
Examples |
|
Changelog
Version 7.2
- Added: hof:top-k-by, hof:top-k-with
- Removed: hof:iterate
Version 7.0
- module added