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 / gist:0b2a2291b31807e70bb8
Last active August 29, 2015 14:08
BaseX Full text search: To be or not to be
Using http://xml.coverpages.org/bosakShakespeare200.html
Timing BaseX 7.9 GUI find command, numbers in parentheses are fails
q1:to be or not to be
q2:Brevity is the soul of wit
index defined q1 q2
---------------------------------
none 500ms 400ms
fulltext~5mb 30ms 3ms
@apb2006
apb2006 / json-test.xqm
Created December 2, 2014 22:40
BaseX RESTXQ JSON test
(:~
: JSON test apb 2dec 2014
:)
module namespace page = 'json-test';
declare variable $page:data:=<json objects="json _">
<total type="number">15</total>
<entity>app</entity>
<items type="array">
@apb2006
apb2006 / javascripting.xq
Last active August 29, 2015 14:26
Java8 XQuery Javascript
(: Java8 XQuery Javascript :)
declare namespace se="java:javax.script.ScriptEngine";
declare namespace sm="java:javax.script.ScriptEngineManager";
let $engine:= sm:getEngineByName(sm:new(),"nashorn")
return (
se:put($engine,"a","This string is encoded!"),
se:eval($engine,"escape(a)")
)
import module namespace eval = "quodatum.eval" at "../../../app-doc/src/doc/lib/eval.xqm";
let $s:="
declare variable $state as element(state):=db:open('doc-doc','/state.xml')/state;
(replace value of node $state/hits with 1+$state/hits,
1+$state/hits)
"
let $b:= eval:update($s,"",map{})
return ($b,$b?result+100)
@apb2006
apb2006 / powerset.xq
Last active September 18, 2015 15:43
generate powerset
(: let $combs:=[(),1,2,3,(1,2),(2,3),(1,3),(1,2,3)] :)
declare function local:powerset($items as item()* )
as array(*){
if (count($items)=0) then [()]
else let $x:=local:powerset(tail($items))
return array:join(($x,
array:for-each($x,function($v){head($items),$v}))
)
};
@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 / 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>
};