Skip to content

Instantly share code, notes, and snippets.

View line-o's full-sized avatar
🌱
weeding the garden

Juri Leino line-o

🌱
weeding the garden
View GitHub Profile
@line-o
line-o / chmod-recursive.xq
Last active November 20, 2022 06:06 — forked from joewiz/chmod-recursive.xq
[WIP] change user, group, mode on resources and collections in eXist-db recursively
xquery version "3.1";
import module namespace sm="http://exist-db.org/xquery/securitymanager";
import module namespace xmldb="http://exist-db.org/xquery/xmldb";
(: these variables need to be set by the caller :)
declare variable $collection as xs:string external;
declare variable $group as xs:string? external;
declare variable $user as xs:string? external;
declare variable $mode as xs:string external;
@line-o
line-o / hex2int.xq
Last active August 8, 2021 20:16
Convert hex string to xs:integer
xquery version "3.1";
(:
: Parts of gist were taken from by https://mathling.com/code/art/core/utilities.xqy
: Copyright Mary Holstege 2020-2021
: Licensed under CC-BY (https://creativecommons.org/licenses/by/4.0/)
:
: Parts are from the xbow library https://github.com/line-o/xbow
: by Juri Leino
: Licensed under MIT
xquery version "3.1";
(:
two possible solutions to @cassidoo's interview question from:
https://buttondown.email/cassidoo/archive/it-is-absolutely-imperative-that-every-human/
:)
declare function local:product ($result, $next) { $result * $next };
declare function local:grouping ($a, $b, $c) {
let $grouped :=
@line-o
line-o / current-winner.xq
Last active May 19, 2021 21:31
retrieve all XPaths to nodes within an element
(: thanks to Reece H. Dunn for hinting at the fn:path function :)
//node()!(.,@*)!path()
@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 :=