Difference between revisions of "Traverse a Map"
From CodeCodex
(→Ruby) |
|||
Line 22: | Line 22: | ||
Map.fold | Map.fold | ||
+ | |||
+ | ===Go=== | ||
+ | <HIGHLIGHTSYNTAX> | ||
+ | fmt.Println("The map contains the following associations:") | ||
+ | for key, value := range theMap { | ||
+ | fmt.Printf("(%v,%v)\n", key, value) | ||
+ | } | ||
+ | </HIGHLIGHTSYNTAX> | ||
===Java=== | ===Java=== | ||
Line 118: | Line 126: | ||
[[Category:D]] | [[Category:D]] | ||
[[Category:F sharp]] | [[Category:F sharp]] | ||
+ | [[Category:Go]] | ||
[[Category:Java]] | [[Category:Java]] | ||
[[Category:Objective Caml]] | [[Category:Objective Caml]] |
Revision as of 06:53, 31 July 2010
Contents
Implementations
C++
This code assumes that theMap is an object of type std::map<K,V>, that is, with key type K and value type V. It will loop over theMap and will list the name values pairs contained within the map. <HIGHLIGHTSYNTAX language="cpp"> std::cout << "The map contains the following associations:" << std::endl; for (std::map<K,V>::iterator it = theMap.begin(); i != theMap.end(); i++) { // iterate over the entries
K key = it->first; // Get the key V value = it->second; // Get the value std::cout << "(" << key << "," << value << ")" << std::endl;
} </HIGHLIGHTSYNTAX>
D
writefln("The map contains the following associations:"); foreach(key, value; theMap) writefln("(%s,%s)", key, value);
F#
Map.fold
Go
<HIGHLIGHTSYNTAX> fmt.Println("The map contains the following associations:") for key, value := range theMap {
fmt.Printf("(%v,%v)\n", key, value)
} </HIGHLIGHTSYNTAX>
Java
This code assumes that theMap is already instantiated, and is a Map<K,V>, that is, with key type K and value type V. It will loop over theMap and will list the name values pairs contained within the map. <HIGHLIGHTSYNTAX language="java122"> System.out.println("The map contains the following associations:"); for (Map.Entry<K,V> e : theMap.entrySet()) { // iterate over the Set of entries
K key = e.getKey(); // Get the key V value = e.getValue(); // Get the value System.out.println("(" + key + "," + value + ")");
} </HIGHLIGHTSYNTAX>
OCaml
If you want to just iterate through a map, you could use the iter function: <HIGHLIGHTSYNTAX language="ocaml"> module MyMap = Map.Make (String);; let the_map = MyMap.add "foo" 1 MyMap.empty;; MyMap.iter (fun k v -> Printf.printf "%s -> %d\n" k v) the_map;; (* outputs "foo -> 1" *) </HIGHLIGHTSYNTAX>
Or you can use a fold to collect the results together to do something: <HIGHLIGHTSYNTAX language="ocaml"> let sum_of_values = MyMap.fold (fun k v z -> v + z) the_map 0;; </HIGHLIGHTSYNTAX>
If you wanted to traverse a hashtable: <HIGHLIGHTSYNTAX language="ocaml"> let the_hash = Hashtbl.create 10;;
Hashtbl.iter (fun k v -> Printf.printf "%s -> %d\n" k v) the_hash;; let sum_of_values = Hashtbl.fold (fun k v z -> v + z) the_hash 0;; </HIGHLIGHTSYNTAX>
Perl
<HIGHLIGHTSYNTAX language="perl"> print "The map contains the following associations:\n"; while ($key, $val = each %theMap) {
print "($key,$val)\n";
} </HIGHLIGHTSYNTAX>
Alternately, you can get the keys or values with the "keys()" and "values()" functions, and then iterate them: <HIGHLIGHTSYNTAX language="perl"> print "The map contains the following associations:\n"; foreach $key (keys %theMap) {
print "($key,$theMap{$key})\n";
} </HIGHLIGHTSYNTAX>
PHP
<HIGHLIGHTSYNTAX language="php3"> print("The map contains the following associations:\n"); foreach ($array as $key => $val) {
printf("(%s,%s)\n", $key, $val);
} </HIGHLIGHTSYNTAX>
Python
<HIGHLIGHTSYNTAX language="python">
print "The map contains the following associations:" for key, val in theMap.iteritems(): print "(%s,%s)" % (key, val)
</HIGHLIGHTSYNTAX>
Ruby
This code assumes that theMap is an object of the Hash class.
print "The map contains the following associations:\n" for key, val in theMap printf "(%s,%s)\n", key, val end # or theMap.each {|key, val| puts "(#{key},#{val})"}
Seed7
This code assumes that the hash table theMap is already instantiated. It will loop over theMap and will list the name value pairs contained within the hash table.
const proc: writeTheMap is func local var base_type(typeof(theMap)): valueVar is base_type(typeof(theMap)).value; var key_type(typeof(theMap)): keyVar is key_type(typeof(theMap)).value; begin writeln("The map contains the following associations:"); for valueVar key keyVar range theMap do writeln(" (" <& keyVar <& "," <& valueVar <& ")"); end for; end func;