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 / zip.xq
Last active December 5, 2023 19:48
Python like zip function in Xquery
import module namespace array="http://www.w3.org/2005/xpath-functions/array";
import module namespace map="http://www.w3.org/2005/xpath-functions/map";
declare namespace _="//line-o.de/ns/underline";
declare function _:nth-item ($sequence as item()*, $pos as xs:integer) as item()* {
$sequence[$pos]
};
declare function _:reduce-sequences (
@line-o
line-o / prob.xqm
Last active September 22, 2023 19:17
Selecting items skewed by weights was an interesting problem to solve
module namespace prob = "//line-o.de/ns/prob";
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare namespace array = "http://www.w3.org/2005/xpath-functions/array";
(: select by p from prepared sequence of options :)
declare
function prob:select-by-p($options as array(*)+, $p as xs:double) as item()* {
fold-left($options, (),
prob:select(?, ?, $p)
@line-o
line-o / has-cdata.html
Last active February 11, 2023 19:58
Illustrate a bug in existdb that leads to exist.log polluted with error messages
<html>
<body>
<script type="text/javascript"><![CDATA[ console.log(true && 1 < 2) ]]></script>
</body>
</html>
@line-o
line-o / orcid.xqm
Last active February 10, 2023 09:46
Validate ORCIDs (both format and checksum)
xquery version "3.1";
module namespace orcid="orcid/validate";
declare variable $orcid:R := 2;
declare variable $orcid:M := 11;
declare variable $orcid:format := '^(\d{4}-\d{4}-\d{4}-\d{3}|\d{15})[0-9X]$';
declare variable $orcid:is-valid-format := matches(?, $orcid:format);
declare variable $orcid:expected-sum := map{
'1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'X':10
@line-o
line-o / expected-output.txt
Last active January 19, 2023 14:47
fun with permissions
0600 (.rw-------) -> 0700 (.rwx------)
0755 (.rwxr-xr-x) -> 0755 (.rwxr-xr-x)
0660 (.rw-rw----) -> 0770 (.rwxrwx---)
0664 (.rw-rw-r--) -> 0775 (.rwxrwxr-x)
0666 (.rw-rw-rw-) -> 0777 (.rwxrwxrwx)
0622 (.rw--w--w-) -> 0733 (.rwx-wx-wx)
0777 (.rwxrwxrwx) -> 0777 (.rwxrwxrwx)
@line-o
line-o / safe-ebv.xq
Last active March 2, 2023 19:23
A safe way to evaluate the effective boolean value of a sequence of items -> anything that cannot be evaluated is false
xquery version "3.1";
declare function local:safe-effective-boolean-value-if (
$result as item()*
) as xs:boolean {
try {
if ($result)
then true()
else false()
} catch err:FORG0006 {
@line-o
line-o / latest-news.html
Last active June 19, 2023 14:53
Prerender HTML with eXist-db's templating library
<main class="news-list__latest">
<ul>
<li data-template="templates:each" data-template-from="articles" data-template-to="article">
<a data-template="pr:article-link"/>
</li>
</ul>
</main>
@line-o
line-o / update-all.xq
Last active September 8, 2022 14:44
Update all packages were newer versions are available in the package repository
xquery version "3.1";
import module namespace semver = "http://exist-db.org/xquery/semver";
declare namespace http="http://expath.org/ns/http-client";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "json";
declare option output:media-type "application/json";
@line-o
line-o / flatten-one.xq
Last active August 11, 2022 21:14
array:flatten is recursive
declare function local:flatten-one($nested as array(*)) as array(*) {
array:fold-left($nested, [], function ($flattened as array(*), $next as item()*) as array(*) {
typeswitch($next)
case array(*) return array:join(($flattened, $next))
default return array:append($flattened, $next)
})
};
local:flatten-one([[1], 3, map{}, [1, [3,4]], ()])
@line-o
line-o / display.js
Created May 22, 2022 10:57
GPN20 display hack
let tx = document.getElementById("text-input")
let h = 2
let state = Array(24).fill([h, '_'])
let c = ['/', '_', '\\']
let interval
function step () {
let direction = Math.floor(Math.random() * 2.9999) - 1
let d = Math.min(3, Math.max(0, h+direction))
state.shift()