Skip to content

Instantly share code, notes, and snippets.

@robbypelssers
robbypelssers / gist:5169525
Created March 15, 2013 12:21
Creating a random sequence of numbers for benchmarking XQuery performance
import scala.util.Random
import java.io._
val testdata = "let $values := (" + (Seq.fill(5000)(Random.nextInt(10000))).mkString(",") + ")"
val writer = new PrintWriter(new File("c:/tmp/testdata.txt" ))
writer.write(testdata)
writer.close()
@robbypelssers
robbypelssers / gist:5186812
Last active March 23, 2017 11:39
Unicode Normalization
import java.text.Normalizer
/**
* Problem: Characters with accents or other adornments can be encoded in several different ways in Unicode
* However, from a user point of view if they logically mean the same, text search should make no distinction
* between the different notations. So it's important to store text in normalized unicode form. Code below shows
* how to check if text is normalized and how you can normalize it.
**/
object NormalizationTest {
@robbypelssers
robbypelssers / gist:5187072
Last active December 15, 2015 02:28
XQuery3.0 Group By Clause
return
<groups>
{
for $person in $persons/person
group by
$country := $person/country,
$gender := $person/@gender
return
<group country="{$country}" gender="{$gender}">
<averageAge>{avg($person/age)}</averageAge>
@robbypelssers
robbypelssers / gist:5187224
Created March 18, 2013 13:43
XQuery 3.0: Using simple map operator
let $persons :=
<persons>
<person gender="male" country="Belgium">
<name>Person A</name>
<age>10</age>
</person>
<person gender="male" country="Netherlands">
<name>Person B</name>
<age>20</age>
</person>
@robbypelssers
robbypelssers / gist:5188190
Created March 18, 2013 15:53
XQuery3.0: Partial function example
xquery version "3.0";
declare function local:increase($nums as xs:integer*, $by as xs:integer) as xs:integer* {
$nums ! (. + $by)
};
let $numbers := (2,3,6,8,12,14)
let $increaseByThree := local:increase(?, 3)
let $increaseByFour := local:increase(?, 4)
return
<result>
@robbypelssers
robbypelssers / gist:5194737
Created March 19, 2013 09:23
XQuery testdata
xquery version "3.0";
let $persons :=
<persons>
<person gender="male">
<country>Belgium</country>
<name>Person A</name>
<age>10</age>
</person>
<person gender="male">
@robbypelssers
robbypelssers / gist:5194822
Created March 19, 2013 09:41
XQuery3.0: Higher-Order functions (Filtering)
(:
Below I show 2 different ways of using the filter function. Both do exactly the same but one uses
an inline function expression while the other uses a named function reference
:)
(:
*********************************************************************
Example of using filter with inline function expression
*********************************************************************
@robbypelssers
robbypelssers / gist:5196120
Last active December 15, 2015 03:39
XQuery3.0: Higher-Order functions (functional composition)
xquery version "3.0";
declare function local:compose($f1 as function(item()) as item(), $f2 as function(item()) as item())
as function(item()) as item() {
function($item) as item() {
$f2($f1($item))
}
};
(: 5 * x - 2 :)
@robbypelssers
robbypelssers / gist:5220758
Created March 22, 2013 12:01
XQuery3.0: Mimicking a FLWOR expression with higher-order functions
xquery version "3.0";
declare function local:flwor(
$ctxt as item(),
$find as function(item()) as item()*,
$predicate as function(item()) as xs:boolean,
$orderBy as function(item()) as item()) as item()* {
for $result in $find($ctxt)
where $predicate($result)
@robbypelssers
robbypelssers / gist:5263459
Last active December 15, 2015 12:58
Example of using Loan Pattern in Java
In Java you often deal with a lot of boilerplate code. A typical example is
opening an inputstream, using the inputstream and next closing it. But this
simple use case takes writing quite a bit of code.Some of this boilerplate
can be factored out by using the loan pattern, where the bank connects a
producer and consumer and takes care of the heavy lifting. This code can be
further generalized using generics but for illustrating purposes it serves
as a nice example.
**************************************************
Consumer Interface *
**************************************************