Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Python: +Py3k dict comprehension
added standard ml
Line 976:
PHONEBOOK['J. Random Hacker'] = '553-1337'
</source>
=== Standard ML ===
The SML'97 standard of the [[Standard ML]] programming language does not provide any associative containers. However, various implementations of Standard ML do provide associative containers.
 
The library of the popular [[Standard ML of New Jersey]] implementation provides a signature (kind of like an "interface"), <code>ORD_MAP</code>, which defines a common interface for ordered functional (immutable) associative arrays. There are three general functors <code>BinaryMapFn</code>, <code>ListMapFn</code>, and <code>SplayMapFn</code> that allow you to create the corresponding type of ordered map (the types are [[self-balancing binary search tree]], sorted [[association list]], and [[splay tree]], respectively) using a user-provided structure to describe the key type and comparator. The functor returns a structure that follows the <code>ORD_MAP</code> interface. In addition, there are two pre-defined modules for associative arrays with integer keys: <code>IntBinaryMap</code> and <code>IntListMap</code>.
 
<pre>
- structure StringMap = BinaryMapFn (struct
type ord_key = string
val compare = String.compare
end);
structure StringMap : ORD_MAP
 
- val m = StringMap.insert (StringMap.empty, "Sally Smart", "555-9999")
val m = StringMap.insert (m, "John Doe", "555-1212")
val m = StringMap.insert (m, "J. Random Hacker", "553-1337");
val m =
T
{cnt=3,key="John Doe",
left=T {cnt=1,key="J. Random Hacker",left=E,right=E,value="553-1337"},
right=T {cnt=1,key="Sally Smart",left=E,right=E,value="555-9999"},
value="555-1212"} : string StringMap.map
- StringMap.find (m, "John Doe");
val it = SOME "555-1212" : string option
</pre>
 
Another Standard ML implementation, [[Moscow ML]], also provides some associative containers. First, it provides polymorphic hash tables in the <code>Polyhash</code> structure. Also, some functional maps from the SML/NJ library above are available as <code>Binarymap</code>, <code>Splaymap</code>, and <code>Intmap</code> structures.
 
=== Tcl ===