Skip to content

Instantly share code, notes, and snippets.

View xquery's full-sized avatar

James Fuller xquery

View GitHub Profile
@xquery
xquery / gist:2863342
Created June 3, 2012 12:51
XQuery 3.0 simple map operator
let $f := function($a){upper-case($a)}
let $data := ("test","test2","test3")
return
$data ! $f(.)
will result in
("TEST", "TEST2", "TEST3")
http://www.w3.org/TR/xquery-30/#id-map-operator
@xquery
xquery / gist:5128611
Created March 10, 2013 13:41
getting bored of map:get ?
xquery version "1.0-ml";
let $map := map:map()
let $_ := map:put($map,"test","test")
let $data:= map:get($map,?)
return
$data('test')
@xquery
xquery / gist:5549109
Created May 9, 2013 17:36
MarkLogic XQuery - averaging data with same keys across multiple maps, returning ordered map
xquery version "1.0-ml";
declare option xdmp:mapping "true";
let $map1:= map:map()
let $_ := map:put($map1,'a',xs:float(1.0))
let $_ := map:put($map1,'b',xs:float(1.0))
let $_ := map:put($map1,'c',xs:float(1.0))
let $_ := map:put($map1,'d',xs:float(1.0))
let $_ := map:put($map1,'e',xs:float(1.0))
let $_ := map:put($map1,'f',xs:float(2))
@xquery
xquery / gist:5594054
Last active January 22, 2020 00:12
illustration of mapreduce using xquery 3.0 word count example
xquery version "3.0";
declare function local:mapReduce(
$list as item()*,
$mapper as function(*),
$reducer as function(*)
)
{
let $intermediate := for $l at $pos in $list return $mapper("list" || $pos,$l)
return
@xquery
xquery / gist:6389383
Last active December 22, 2015 00:28
serving up gzipped content from MarkLogic
In a lot of cases, gzipping content before you send it to an HTTP client can make a huge difference in download times.
compare downloaded file size of the following two approaches;
xquery version "1.0-ml";
let $page :=<html>
<body>
<h2>Without Gzip</h2>
@xquery
xquery / gist:6768359
Created September 30, 2013 18:55
yes a bit of xml in memory can have base-uri as well
let $doc := <root xml:base="http://example.org/mydoc"></root>
return
base-uri($doc)
@xquery
xquery / gist:6785050
Last active June 27, 2018 11:35
the 'naked' Y-combinator in XQuery 3.0 using classic factorial example, providing the ability to recurse within anonymous functions
xquery version "3.0";
let $makeFact := function($givenFact) {
let $fact := function($n) {
if( $n lt 2 )
then 1
else $n * $givenFact($n - 1)
}
return $fact
}
@xquery
xquery / gist:6790119
Created October 2, 2013 07:21
fully generalized recursion with Y-combinator in xquery 3.0 ... recursion via 'deferred execution'
xquery version "3.0";
let $makeFactorialFunc := function($givenFactFunc) {
let $fact := function($n) {
if( $n lt 2 )
then 1
else $n * $givenFactFunc($n - 1)
}
return $fact
}
@xquery
xquery / gist:6831758
Created October 4, 2013 19:52
weighted random sampling - the following somewhat contrived code will choose from a sequence a value with larger values having a higher probability of being chosen ... can you do better (as in more elegant, faster, more fp goodness)
xquery version "1.0-ml";
let $data := (1,1,1,1,1,1,1,1,1,20)
let $choose := xdmp:random( sum($data) )
let $pos := filter( function($a){if($choose le sum($data[1 to $a])) then true() else false()}, 1 to count($data) )[1]
return "weighted choose: " || $choose || " | data position: " || $pos || " value: " || $data[$pos]
@xquery
xquery / gist:7561028
Last active December 28, 2015 20:59
my first frameless example - would like to be able to set default source (like saxon-ce data-source on <script/> element
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Frameless Example</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<body>
<div>