Skip to content

Instantly share code, notes, and snippets.

Avatar
🚲
bike shedding

Juri Leino line-o

🚲
bike shedding
View GitHub Profile
@line-o
line-o / hex2int.xq
Last active Aug 8, 2021
Convert hex string to xs:integer
View hex2int.xq
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
View interview-question.xq
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
retrieve all XPaths to nodes within an element
View current-winner.xq
(: 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
distinct values from a given list in the order appearance
View distinct-ordered.xq
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
dicey - use randomness with comfort
View dicey.xq
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
Serialize a URL query string in XQuery
View qs.xqm
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
:)
View create-stats.xq
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 Feb 24, 2021
Performance comparison of filtering atomics in list A not contained in list B
View filter-time-comparison.xq
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 Oct 12, 2020 — forked from joewiz/fib-exist.xq
XQuery Fibonacci reducer function, with timing, for eXist-db
View fib-exist.xq
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 :=
View simple-group-by.xq
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 :=