Skip to content

Instantly share code, notes, and snippets.

@line-o
Last active November 6, 2024 16:41
Show Gist options
  • Save line-o/e2baf40e4fc9b268b9ba8f6b2619b51c to your computer and use it in GitHub Desktop.
Save line-o/e2baf40e4fc9b268b9ba8f6b2619b51c to your computer and use it in GitHub Desktop.
proof of concept to use the output of the proposed function fn:type to find the most specific shared type among a sequence of items
(:
explore if the proposed XQuery 4 function fn:type can be used
to inspect sequences of items to find the most specific shared
type among them
:)
declare namespace ist="//line-o.de/ns/inspect-sequence-types";
declare function ist:find-shared-type ($acc as xs:string*, $next as xs:string*) as xs:string* {
let $count-a := count($acc)
let $count-n := count($next)
return
if (head($acc) eq head($next)) then $acc
else if ($count-a eq 0 or $count-n eq 0) then ()
else if ($count-a eq $count-n) then ist:find-shared-type(tail($acc), tail($next))
else if ($count-n gt $count-a) then ist:find-shared-type($acc, subsequence($next, $count-n - $count-a + 1, $count-a))
else ist:find-shared-type(subsequence($acc, $count-a - $count-n + 1, $count-n), $next)
};
declare function ist:inspect-sequence-type($type-sequences as array(xs:string*)) as array(xs:string*) {
array { array:fold-left($type-sequences, $type-sequences?1, ist:find-shared-type#2) }
};
(: test data that assumes the output of fn:type(item()?) as xs:string* as outlined here
https://github.com/qt4cg/qtspecs/issues/1550#issuecomment-2458192070
:)
let $numeric := [
('xs:integer', 'xs:decimal', 'xs:anyAtomicType', 'item()'),
('xs:positiveInteger', 'xs:integer', 'xs:decimal', 'xs:anyAtomicType', 'item()'),
('xs:decimal', 'xs:anyAtomicType', 'item()')
]
let $atomic := array:append($numeric, ('xs:string', 'xs:anyAtomicType', 'item()'))
let $mixed := array:append($atomic, ('element()', 'item()'))
return (
ist:inspect-sequence-type($numeric), (: ["xs:decimal","xs:anyAtomicType","item()"] :)
ist:inspect-sequence-type($atomic), (: ["xs:anyAtomicType","item()"] :)
ist:inspect-sequence-type($mixed) (: ["item()"] :)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment