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 (Functional composition)

xquery version "3.0";
declare function local:compose($f1 as function(item()) as item(), $f2 as function(item()) as item())
as function(item()) as item() {
function($item) as item() {
$f2($f1($item))
}
};
(: 5 * x - 2 :)
let $linearFunction :=
local:compose(
function($number) {$number * 5},
function($number) {$number - 2})
(: 3 * x^2 - 4 :)
let $quadraticFunction :=
local:compose(
function($number) {$number * $number * 3},
function($number) {$number - 4})
return
<result>
<test1>{$linearFunction(5)} should equal 5 * 5 - 2 = 23</test1>
<test2>{$quadraticFunction(4)} should equal 3 * 4^2 - 4 = 44</test2>
</result>
(:
**********************************************
XQuery output:
**********************************************
<result>
<test1>23 should equal 5 * 5 - 2 = 23</test1>
<test2>44 should equal 3 * 4^2 - 4 = 44</test2>
</result>
:)

No comments:

Post a Comment