Skip to content

Instantly share code, notes, and snippets.

@arolle
Last active December 4, 2022 20:06
Show Gist options
  • Save arolle/6e2ceb14a8af0bc1d42ec1f3aa1ed2f7 to your computer and use it in GitHub Desktop.
Save arolle/6e2ceb14a8af0bc1d42ec1f3aa1ed2f7 to your computer and use it in GitHub Desktop.
(*
Hashtable.empty size hashfunction comparison : ('a, 'b) Hashtable.hashtable
size : int
hashfunction : ('a -> int)
comparison : ('a -> 'a -> ordering)
*)
fun println str = TextIO.print (str ^ "\n");
val toStr = Int.toString;
val toInt = id
val cmp = Int.compare;
val nodes = [(1,"a"), (2,"b")];
val hmap = Hashtable.empty 2 toInt cmp : (int,string) Hashtable.hashtable;
datatype ty = A | B;
val toStr = fn x => case x of A => "a" | B => "b";
val toInt = fn x => case x of A => 0 | B => 1;
val cmp = fn x => fn y => Int.compare (toInt x) (toInt x);
val nodes = [(A,"a"), (B,"b")];
val hmap = Hashtable.empty 2 toInt cmp : (ty,string) Hashtable.hashtable;
List.map (fn (key,value) => (
Hashtable.insert hmap key value;
println ((toStr key) ^ " " ^ value)
)) nodes;
println
(Int.toString (List.length (Hashtable.toAscList hmap))
^ " key(s):");
List.map (println o toStr o fst) (Hashtable.toAscList hmap);
(*
output for hmap : (int,string) Hashtable.hashtable
1 a
2 b
2 key(s):
1
2
output for hmap : (ty,string) Hashtable.hashtable
a a
b b
1 key(s):
a
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment