Changes

Jump to navigation Jump to search
19 bytes added ,  14:44, 12 December 2017
no edit summary
=Function Items=
Probably the most important new feature in XQuery 3.0 are ''function items'', i. e., items that act as functions, but can also be passed to and from other functions and expressions. This feature makes functions ''first-class citizens'' of the language. The [[XQuery_3.0#Function_Items|XQuery 3.0]] page goes into details on how function items can be obtained.
== Function Types ==
Like every XQuery item, function items have a ''sequence type''. It can beused to specify the ''arity'' (number of arguments the function takes) andthe argument and result types.
The most general function type is <code>function(*)</code>. It's the type of allfunction items. The following query for example goes through a list of XQueryitems and, if it is a function item, prints its arity:
<pre class="brush:xquery">
''Result:'' <code>3 1</code>
The notation for specifying argument and return types is quite intuitive, as itclosely resembles the function declaration. The XQuery function
<pre class="brush:xquery">
</pre>
for example has the type <code>function(xs:string, xs:integer) as xs:string</code>. It isn't possible to specify only the argument and not the resulttype or the other way round. A good place-holder to use when no restrictionis wanted is <code>item()*</code>, as it matches any XQuery value.
Function types can also be nested. As an example we take <code>local:on-sequences</code>, which takes a function defined on single items and makes it work on sequences as well:
};
</pre>
 We'll willl see later how <code>fn:for-each(...)</code> works. The type of <code>local:on-sequences(...)</code> on the other hand is easily constructed, if a bit long:
<code>function(function(item()) as item()*) as function(item()*) as item()*</code>.
A ''higher-order function'' is a function that takes other functions as arguments and/or returns them as results. <code>fn:for-each</code> and <code>local:on-sequences</code> from the last chapter are nice examples.
With the help of higher-order functions, one can extract common patterns of''behaviourbehavior'' and abstract them into a library function.
== Higher-Order Functions on Sequences ==
Some usage patterns on sequences are so common that the higher-order functionsdescribing them are in the XQuery standard libraries. They are listed here, togetherwith their possible XQuery implementation and some motivating examples.
===fn:for-each===
|-
| width='120' | '''Signatures'''
|{{Func|fn:for-each|$seq as item()*, $fun function as function(item()) as item()*)|item()*}}
|-
| '''Summary'''
|Applies the function item specified <code>$funfunction</code> to every element item of the sequence <code>$seq</code> and returns all of the results as a single sequence.
|-
| '''Examples'''
|-
| width='120' | '''Signatures'''
|{{Func|fn:for-each-pair|$seq1 as item()*, $seq2 as item()*, $fun function as function(item(), item()) as item()*|item()*}}
|-
| '''Summary'''
|''zips'' Applies the elements from specified {{Code|$function}} to the two sequences successive pairs of items of <code>$seq1</code> and <code>$seq2</code> together with the function <code>$f</code>. It stops after the shorter Evaluation is stopped if one sequence endsyields no more items.
|-
| '''Examples'''
== Folds ==
A ''fold'', also called ''reduce'' or ''accumulate'' in other languages, is a verybasic higher-order function on sequences. It starts from a seed value and incrementallybuilds up a result, consuming one element from the sequence at a time and combining it withthe aggregate of a user-defined function.
Folds are one solution to the problem of not having ''state'' in functional programs.Solving a problem in ''imperative'' programming languages often means repeatedly updatingthe value of variables, which isn't allowed in functional languages.
Calculating the ''product'' of a sequence of integers for example is easy in <code>Java</code>:
 
<pre class="brush:java">
public int product(int[] seq) {
}
</pre>
 
Nice and efficient implementations using folds will be given below.
The ''linear'' folds on sequences come in two flavoursflavors. They differ in the direction in which they traverse the sequence:
===fn:fold-left===
|-
| width='120' | '''Signatures'''
|{{Func|fn:fold-left|$seq as item()*, $seed as item()*, $fun function as function(item()*, item()) as item()*|item()*}}
|-
| '''Summary'''
$seq as item()*,
$seed as item()*,
$fun function as function(item()*, item()) as item()*
) as item()* {
if(empty($seq)) then $seed
else local:fold-left(
fn:tail($seq),
$funfunction($seed, fn:head($seq)), $funfunction
)
};
|-
| width='120' | '''Signatures'''
|{{Func|fn:fold-right|$seq as item()*, $seed as item()*, $fun function as function(item(), item()*) as item()*|item()*}}
|-
| '''Summary'''
$seq as item()*,
$seed as item()*,
$fun function as function(item(), item()*) as item()*
) as item()* {
if(empty($seq)) then $seed
else $funfunction(
fn:head($seq),
local:fold-right(tail($seq), $seed, $funfunction)
)
};
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu