Skip to content

Instantly share code, notes, and snippets.

@holmesw
Last active March 28, 2024 08:12
Show Gist options
  • Save holmesw/642942a300dcdec583bea5b57b44a9ab to your computer and use it in GitHub Desktop.
Save holmesw/642942a300dcdec583bea5b57b44a9ab to your computer and use it in GitHub Desktop.
Function Composition in XQuery 3.1

Function Composition in XQuery 3.1

Overview

This module provides a compose function.

Namespace

The module and default function namespaces are https://tinyurl.com/9apf36he.

Function: compose

Description

This function applies each function in the $functions sequence to the $items sequence. If $functions is empty, it returns $items. Otherwise, it recursively applies the first function in $functions to each item in $items, and then calls itself with the result and the remaining functions.

Parameters

  • $items: The items to be processed. It is of type item()*.
  • $functions: The functions to be applied to the items. It is of type function(item()*)*.

Returns

The result of applying each function to the items. The return type is item()*.

Usage

declare function compose(
    $items as item()*,
    $functions as function(item()*) as item()**
) as item()* {
    if (fn:empty($functions)) then $items
    else compose(fn:for-each($items, fn:head($functions)), fn:tail($functions))
};

References

: https://en.wikipedia.org/wiki/Function_composition_(computer_science)

(:~
: @version 1.0
: @see https://tinyurl.com/9apf36he
: @module.description This module provides a compose function.
:)
xquery version "3.1";
module namespace compose = "https://tinyurl.com/9apf36he";
declare default function namespace "https://tinyurl.com/9apf36he";
(:~
: @function.description This function applies each function in the $functions
: sequence to the $items sequence. If $functions is empty, it returns $items.
: Otherwise, it recursively applies the first function in $functions to each
: item in $items, and then calls itself with the result and the remaining
: functions.
: @param $items The items to be processed.
: @param $functions The functions to be applied to the items.
: @return The result of applying each function to the items.
:)
declare function compose(
$items as item()*,
$functions as function(item()*) as item()**
) as item()* {
if (fn:empty($functions)) then $items
else compose(fn:for-each($items, fn:head($functions)), fn:tail($functions))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment