On Mon, Oct 09, 2017 at 11:05:03AM +0100, Richard W.M. Jones wrote:
> +let osinfo_ids = ref None
> +
> +let osinfo_get_short_ids () =
> + match !osinfo_ids with
> + | Some ids -> ids
> + | None -> (
> + let set = ref StringSet.empty in
> + Osinfo.iterate_db (
> + fun filepath ->
> + let doc = Xml.parse_file filepath in
> + let xpathctx = Xml.xpath_new_context doc in
> + let nodes = xpath_get_nodes xpathctx "/libosinfo/os/short-id" in
> + List.iter (
> + fun node ->
> + let id = Xml.node_as_string node in
> + set := StringSet.add id !set
> + ) nodes
> + );
> + osinfo_ids := Some (!set);
> + !set
> + )
If the Osinfo module implemented a ‘fold’ function (instead of iter)
then you could write this much more conveniently. It would be
something like:
let set = Osinfo.fold (fun set filepath -> ...) StringSet.empty in
where the ‘...’ bit non-imperatively updates ‘set’.
See attached for what I mean (only compiled, not tested).
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
libguestfs lets you edit virtual machines. Supports shell scripting,
bindings from many languages.
http://libguestfs.org