


There are a few more functions in the Map API, but the above should be more than enough to get you started.
To close out this section, here is a more realistic example: We will get a list of all the files in the current directory, then run stat on each to get information such as its size, enter the stat results into a map keyed by the filename, and finally extract a useful result from the resulting map:
#!/usr/bin/mythryl
package map = string_map;
stat_map = REF (map::empty: map::Map( posix::stat::Stat ));
foreach (dir::files ".")
(fn filename
=
stat_map := map::set (*stat_map, filename, stat filename)
);
my (file1, _) = the (map::first_keyval_else_null *stat_map);
printf
"First file is named '%s' and has size %d.\n"
file1
(the (map::get (*stat_map, file1))).size;
When run this should produce a result something like
linux$ ./my-script
First file is named '/pub/home/cynbe/mythryl7.110.58/Configure' and has size 328.
The experienced Mythryl programmer tends to dislike avoidable use of side-effects, hence might write the above script as:
#!/usr/bin/mythryl
package map = string_map;
filenames = dir::files ".";
file1 = head filenames;
stat_map
=
fold_backward
(fn (filename, stat_map) = map::set (stat_map, filename, stat filename))
(map::empty: map::Map( posix::stat::Stat ))
filenames;
printf
"First file is named '%s' and has size %d.\n"
file1
(the (map::get (stat_map, file1))).size;
The difference is mostly a matter of taste. The result when run will be identical either way:
linux$ ./my-script
First file is named '/pub/home/cynbe/mythryl7.110.58/Configure' and has size 328.


