Skip to content

Instantly share code, notes, and snippets.

@xquery
Last active January 22, 2020 00:12
Show Gist options
  • Save xquery/5594054 to your computer and use it in GitHub Desktop.
Save xquery/5594054 to your computer and use it in GitHub Desktop.
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
for $key in distinct-values($intermediate/key)
return $reducer($key, $intermediate[key eq $key])
};
let $list := ("The quick brown fox jumped over the lazy grey dogs.","That's one small step for a man, one giant leap for mankind.","Mary had a little lamb, Its fleece was white as snow; And everywhere that Mary went, The lamb was sure to go.")
return
local:mapReduce(
$list,
function($key,$value){for $w in tokenize($value,' ') return <item list="{$key}"><key>{replace($w,"(\.|,|;)","")}</key><value>1</value></item>},
function($key,$values){<item lists="{distinct-values($values/@list)}"><key>{$key}</key><value>{sum($values/value)}</value></item>}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment