If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Tuesday, March 19, 2013

XQuery3.0: Higher-Order functions (Filtering)

(:
Below I show 2 different ways of using the filter function. Both do exactly the same but one uses
an inline function expression while the other uses a named function reference
:)
(:
*********************************************************************
Example of using filter with inline function expression
*********************************************************************
:)
return fn:filter(function($person) { $person/@gender = 'female'}, $persons/person)
(:
*********************************************************************
Example of using filter with named function reference (predicate)
*********************************************************************
:)
declare function local:isFemale($person as element(person)) as xs:boolean {
$person/@gender = 'female'
};
let $isFemale := local:isFemale#1
return fn:filter($isFemale, $persons/person)
(:
**********************************************
XQuery output:
**********************************************
<person gender="female">
<country>Belgium</country>
<name>Person C</name>
<age>80</age>
</person>
<person gender="female">
<country>Netherlands</country>
<name>Person E</name>
<age>30</age>
</person>
:)

No comments:

Post a Comment