Skip to content

Instantly share code, notes, and snippets.

View xquery's full-sized avatar

James Fuller xquery

View GitHub Profile
@xquery
xquery / memoization.xqy
Created May 4, 2011 05:57
Inline caching (memoization) for higher order xquery functions
xquery version "1.0-ml";
(: xquery memoization example for use with MarkLogic :)
declare variable $cache := map:map();
declare function local:factorial($n as xs:integer) as xs:integer {
if ($n < 0) then
(0)
else if ($n = 0) then
@xquery
xquery / gist:956835
Created May 5, 2011 10:24
idiv operator in xquery
xquery version "1.0";
(: The idiv operator returns an integer as a result, rounding toward 0. :)
let $a as xs:decimal := 20.1
let $b as xs:decimal := 1.678
let $result := $a idiv $b
return
$result
@xquery
xquery / gist:956929
Created May 5, 2011 12:03
using xpath id() function on xml:id
(: xml:id attribute needs to be unique within the specific document scope :)
let $xml := document{ <employees>
<employee xml:id="n001">
<name>Johny Rotten</name>
</employee>
<employee xml:id="n002">
<name>Billy Idol</name>
</employee>
</employees>}
@xquery
xquery / gist:1185659
Created September 1, 2011 07:46
XQuery prime number generation
(: simple - http://e-blog-java.blogspot.com/2010/03/determine-prime-numbers-with-xquery.html :)
<primes>
{
for $a in (2 to 1000)
return(
if(not(some $counter in (2 to ($a - 1)) satisfies $a mod
$counter=0)) then {$a}
else {$a}
)
}
@xquery
xquery / gist:1208273
Created September 10, 2011 12:49
approach to persistent queue on MarkLogic (untested!)
xquery version "1.0-ml";
declare variable $flush := 1;
declare function local:queue($map){
if ($flush eq 1 or fn:empty(xdmp:get-server-field('queue'))) then
let $queue as map:map := map:map( if ($map) then $map else <map:map/>)
let $serverfield := xdmp:set-server-field('queue',$queue)
return
$queue
@xquery
xquery / gist:1536327
Created December 29, 2011 21:37
xquery - split a comma delimited string and add text to each item
i've interpreted what you may want to achieve, but mileage may vary
xquery version "1.0";
let $string := "test,test,test,test,test"
return
for $text in tokenize($string,',')
return
<text>{concat('add some text: ', $text)}</text>
@xquery
xquery / gist:1615813
Created January 15, 2012 13:08
getting started - depx
cd /to/your/application/directory
/Applications/depx-0.1/depx
@xquery
xquery / gist:1885182
Created February 22, 2012 13:31
xquery impl of fizzbuzz - can you do faster or more elegant ?
for $n in (1 to 100)
let $fizz := if ($n mod 3) then () else "fizz"
let $buzz := if ($n mod 5) then () else "buzz"
return
if ($fizz or $buzz) then concat($fizz,$buzz) else $n
@xquery
xquery / Learning R
Created March 3, 2012 19:11
Learning R - installation and gettting started
I installed R via brew e.g.
>brew install R
which complained it needed fortran compiler which was solved with
>brew install gfortran
then re ran brew R installation and no problems.
@xquery
xquery / Learning R part 2
Created March 3, 2012 19:17
Learning R - some basics
R is an object-oriented language with every object having a type (and being a member of a class)
*** What is R written in ?
R itself, c and fortran (autoconf, shell script)
http://blog.revolutionanalytics.com/2011/08/what-language-is-r-written-in.html
though some R packages are in c++, Perl, etc