<?xml version="1.0" encoding="utf-8" ?> <!-- for emacs: -*- coding: utf-8 -*- --> <!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>mapping over hash tables -- apply a function to each element of a hash table</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_error_sphandling.html">next</a> | <a href="_mapping_spover_splists.html">previous</a> | <a href="_error_sphandling.html">forward</a> | <a href="_mapping_spover_splists.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div> </td> </tr> </table> <div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_mapping_spover_sphash_sptables.html" title="apply a function to each element of a hash table">mapping over hash tables</a></div> <hr/> <div><h1>mapping over hash tables -- apply a function to each element of a hash table</h1> <div>Each entry in a hash table consists of a key and a value. We provide three ways to map functions over a hash table, depending on whether the function is to receive a value and return a new value, to receive a key and return a new key, or to receive a key-value pair and return a new key-value pair. The corresponding functions, <a href="_apply__Values.html" title="apply a function to each value">applyValues</a>, <a href="_apply__Keys.html" title="apply a function to each key in a hash table">applyKeys</a>, and <a href="_apply__Pairs.html" title="apply a function to each pair in a hash table">applyPairs</a> are illustrated in the next example.<table class="examples"><tr><td><pre>i1 : x = new HashTable from {a=>1, b=>2} o1 = HashTable{a => 1} b => 2 o1 : HashTable</pre> </td></tr> <tr><td><pre>i2 : applyValues(x, value -> 1000*value) o2 = HashTable{a => 1000} b => 2000 o2 : HashTable</pre> </td></tr> <tr><td><pre>i3 : applyKeys(x, key -> {key}) o3 = HashTable{{a} => 1} {b} => 2 o3 : HashTable</pre> </td></tr> <tr><td><pre>i4 : applyPairs(x, (key,value) -> (value,key)) o4 = HashTable{1 => a} 2 => b o4 : HashTable</pre> </td></tr> </table> The functions, <a href="_scan__Values.html" title="apply a function to each value in a hash table">scanValues</a>, <a href="_scan__Keys.html" title="apply a function to each key in a hash table or database">scanKeys</a>, and <a href="_scan__Pairs.html" title="apply a function to pairs in a hash table">scanPairs</a> are similar, but the values returned are discarded instead of being assembled into a new hash table.<table class="examples"><tr><td><pre>i5 : x = new HashTable from {a=>1, b=>2} o5 = HashTable{a => 1} b => 2 o5 : HashTable</pre> </td></tr> <tr><td><pre>i6 : scanValues(x, print) 1 2</pre> </td></tr> <tr><td><pre>i7 : scanKeys(x, print) a b</pre> </td></tr> <tr><td><pre>i8 : scanPairs(x, print) (a, 1) (b, 2)</pre> </td></tr> </table> The function <a href="_merge_lp__Hash__Table_cm__Hash__Table_cm__Function_rp.html" title="merge hash tables">merge</a> can be used to merge two hash tables. The result is a hash table whose keys are those occurring in one of the two incoming hash tables. We must provide a function of two arguments that is used to combine the values when a key occurs in both hash tables.<table class="examples"><tr><td><pre>i9 : y = new HashTable from {b=>200, c=>300} o9 = HashTable{b => 200} c => 300 o9 : HashTable</pre> </td></tr> <tr><td><pre>i10 : merge(x, y, plus) o10 = HashTable{a => 1 } b => 202 c => 300 o10 : HashTable</pre> </td></tr> </table> The function <a href="_combine.html" title="combine hash tables">combine</a> can be used to combine two hash tables <tt>x</tt> and <tt>y</tt> into a new hash table. Three functions must be provided. The first one produces new keys from a key of <tt>x</tt> and a key of <tt>y</tt>. The second one produces a new values from a value of <tt>x</tt> and a value of <tt>y</tt>. The third one is used to combine values when two new keys turn out to be the same.<table class="examples"><tr><td><pre>i11 : combine(x,y,identity,times,plus) o11 = HashTable{(a, b) => 200} (a, c) => 300 (b, b) => 400 (b, c) => 600 o11 : HashTable</pre> </td></tr> </table> </div> </div> </body> </html>