Skip to content

Instantly share code, notes, and snippets.

View line-o's full-sized avatar
🧩
tiling

Juri Leino line-o

🧩
tiling
View GitHub Profile
@line-o
line-o / distinct-ordered.xq
Created May 4, 2021 19:05
distinct values from a given list in the order appearance
xquery version "3.1";
declare function local:distinct-order ($to-sort, $order) {
for $item in $to-sort[.=$order]
let $group-key := $item
group by $group-key
order by index-of($order, $group-key)[1]
return $group-key
};
@line-o
line-o / dicey.xq
Last active May 5, 2021 06:28
dicey - use randomness with comfort
xquery version "3.1";
declare namespace dicey="http://line-o.de/ns/dicey";
declare function dicey:sequence ($n as xs:integer,
$generator as map(xs:string, item())) as item()* {
fold-left(
1 to $n,
map { "sequence": (), "generator": $generator},
dicey:reducer#2
@line-o
line-o / qs.xqm
Last active May 1, 2021 19:43
Serialize a URL query string in XQuery
xquery version "3.1";
module namespace qs="http://line-o.de/ns/qs";
(:~
: Append nothing to names of parameters with multiple values
: ?single=v1&multi=v2&multi=v3
:)
xquery version "3.1";
map {
"date" : current-dateTime(),
"packages" : array {
for $get-package-event in collection("/db/apps/public-repo-data/logs")//type[.="get-package"]/..
group by $name := $get-package-event/package-name/string()
let $event-count := count($get-package-event)
order by $event-count descending
return
@line-o
line-o / filter-time-comparison.xq
Created February 24, 2021 13:48
Performance comparison of filtering atomics in list A not contained in list B
xquery version "3.1";
declare variable $local:millisecond := xs:dayTimeDuration('PT0.001S');
declare variable $local:max := 3000;
declare variable $local:reference := for-each(1 to $local:max, xs:string(?));
declare variable $local:test := for-each((3 to 400, 402 to 2900), xs:string(?));
declare function local:one () {
let $as-map :=
@line-o
line-o / fib-exist.xq
Last active October 12, 2020 14:45 — forked from joewiz/fib-exist.xq
XQuery Fibonacci reducer function, with timing, for eXist-db
xquery version "3.1";
(: adapted from https://stackoverflow.com/a/4936099 :)
declare function local:fib-reducer ($r, $n) { $r[1] + $r[2], $r[1] };
declare function local:fib($n as xs:integer) {
fold-left((1 to $n), (0,1), local:fib-reducer#2)[1]
};
let $results :=
xquery version "3.1";
import module namespace xbow="http://line-o.de/xq/xbow";
let $length := 100000
let $sequence-of-maps := (1 to $length) ! map { "n": ., "values": ("a" || util:random(100), "b" || util:random(100), "c" || util:random(100)) }
let $keyed-sequence :=
@line-o
line-o / find-document-by-id.xq
Created March 29, 2020 08:05
Retrieve full path to a document in an exist-db collection when only its document ID is known
xquery version "3.1";
declare
function local:find($path, $document-id as xs:integer) as xs:string* {
local:find-in-collection($path, (), $document-id)
};
declare
function local:resource ($collection as xs:string, $resource as xs:string, $document-id as xs:integer) as xs:string? {
let $path := $collection || '/' || $resource
@line-o
line-o / mod-fn-stats.xq
Created February 16, 2020 21:35
existdb module and function name lengths
xquery version "3.1";
import module namespace xbow = "http://line-o.de/xq/xbow";
declare namespace xqdoc="http://www.xqdoc.org/1.0";
declare variable $local:length-category-rules := [
xbow:le(2), xbow:le(4), xbow:le(8), xbow:le(12), xbow:gt(12) ];
declare function local:gather-and-count ($names) {
$names
@line-o
line-o / random-number-generator.xq
Last active October 27, 2019 10:02
polyfill `fn:random-number-generator`
xquery version "3.1";
declare function local:random-number () {
util:random(1000000000) div 1000000000
};
declare function local:random-number-generator () {
map {
'number': local:random-number(),
'next': local:random-number-generator#0,