Skip to content

Instantly share code, notes, and snippets.

View apb2006's full-sized avatar

Andy Bunce apb2006

  • Quodatum
  • London, UK
View GitHub Profile
@apb2006
apb2006 / nested.html
Last active December 10, 2015 21:39
angularjs nested repeats
<!DOCTYPE html>
<html ng-app><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Angular.js Nested Repeatable Objects</title>
<style>
#section-form { margin: auto; width: 960px; }
.section { border: 2px solid #999; margin: 10px 0; padding: 10px; }
.section > label { display: inline-block; padding-right: 10px; text-align: right; vertical-align:top; width: 150px; }
.question { background: #eee; border: 2px solid #aaa; margin: 5px 0; padding: 10px; }
#fader { position:fixed; top: 0px; left: 0px; width: 100%; height:100%; background-color: black; opacity: 0.8; z-index: 3; }
@apb2006
apb2006 / bad.xqm
Created May 16, 2013 16:59
baseX 7.7 passing java objects with function reference. Requires "metadata-extractor-2.6.4.jar" "xmpcore.jar" from http://drewnoakes.com/code/exif/
xquery version "3.0" encoding "UTF-8";
(:~ return image metadata as xml using http://drewnoakes.com/code/exif/
: usage: metadata:read('C:/temp/apic.jpg')
: xml format based on xmlcalabash1
: https://github.com/ndw/xmlcalabash1/blob/master/src/com/xmlcalabash/extensions/MetadataExtractor.java
: @author andy bunce
:)
module namespace metadata = 'apb.image.metadata';
declare default function namespace 'apb.image.metadata';
@apb2006
apb2006 / simple.xq
Last active December 22, 2015 11:19
timeout test
let $s:="
import module namespace test = 'org.apb.modules.TestModule';
declare function local:prime($n){
$n = 2 or ($n > 2 and
(every $d in 2 to xs:integer(math:sqrt($n)) satisfies $n mod $d > 0))
};
let $t:=test:startTimeout(xs:int(10000))
return count((1 to 1000000)[local:prime(.)])
"
@apb2006
apb2006 / stacksize.xq
Created March 29, 2016 21:04
tail recursive
(:~
: tail recursion example
:)
declare function local:sum-a($n){
if($n eq 0) then 1 else $n+ local:sum-a($n - 1)
};
declare function local:sum-b($n){
local:sum-iter(1,$n)
};
declare function local:result($id){
try{
map{ "result":async:result($id)
}
} catch * {
map{ "error": map {
'code': $err:code,
'description': $err:description,
'module': $err:module,
'line': $err:line-number,
@apb2006
apb2006 / trang.xq
Last active April 28, 2016 20:37
Run [Trang](http://www.thaiopensource.com/relaxng/trang.html) from BaseX. Requires trang.jar on path,
declare namespace trang="java:com.thaiopensource.relaxng.translate.Driver";
trang:new()=> trang:run(
("C:\Users\andy\git\qd-cmpx\src\main\content\components.rnc"
,"C:\Users\andy\git\qd-cmpx\src\main\content\output.xsd")
)
@apb2006
apb2006 / rex.xq
Last active May 20, 2016 12:15
call REx from XQuery
(: ebnf to Java parser for BaseX using REx :)
declare function local:REx-request($ebnf as xs:string,$name as xs:string,$command as xs:string)as item()+{
let $req:=
<request xmlns="http://expath.org/ns/http-client" href='http://www.bottlecaps.de/rex/' method="post">
<multipart media-type="multipart/form-data" boundary="xyzBouNDarYxyz">
<header name="Content-Disposition" value='form-data; name="command"'/>
<body media-type="text/plain"/>
<header name="Content-Disposition" value='form-data; name="input"; filename="{$name}.ebnf"'/>
<body media-type="text/plain"/>
</multipart>
@apb2006
apb2006 / app-basex.js
Last active June 25, 2016 13:59
Node.js + socket.io + BaseX = Xquery chatbot
// Andy bunce jan 2013 based on http://book.mixu.net/ch13.html
var fs = require('fs'),
http = require('http'),
sio = require('socket.io');
var port=8001;
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
res.end(fs.readFileSync('./index.html'));
});
server.listen(port, function() {
@apb2006
apb2006 / restxq.xqm
Created July 21, 2016 11:47
BaseX RESTXQ Content Negotiation
module namespace page = 'http://basex.org/modules/web-page';
declare
%rest:path("content")
%rest:produces("application/json")
%output:method("json")
function page:content1(){
<json type="object">
<msg>Hello</msg>
</json>
};
(: https://libertyseeds.ca/2015/07/21/Investigating-Final-Draft-s-XML-document-format-with-Ruby/ :)
declare function local:update($result as map(*),$next as element()) as map(*)
{
let $name:=name($next)
let $current:=$result($name)
let $atribs:=$next/@*[if(empty($current)) then true() else not( .=$current(name(.)))]
let $atribs:=map:merge(($current, $atribs!map:entry(name(.),string(.))), map{'duplicates': 'combine'})
return map:put($result,$name,$atribs)
};