在 OCaml 中将哈希表转换为对(键,值)列表

发布于 2024-09-30 06:31:53 字数 350 浏览 2 评论 0原文

OCaml 中有没有办法将哈希表转换为(键,对)值列表?

我知道,给定哈希表 ht 我们可以

BatList.of_enum (BatHashtbl.enum ht)

使用电池库。这会将表转换为枚举,然后将枚举转换为列表。但我正在寻找一种不使用电池库的解决方案。在 标准 OCaml Hashtbl 模块 中似乎没有是一种将对提取为列表的方法或组合其功能以实现此目的的方法。有什么建议吗?

Is there a way of converting a hash table into a list of (key,pair) values in OCaml?

I'm aware that, given a hash table ht we can do

BatList.of_enum (BatHashtbl.enum ht)

using the batteries library. This would convert the table to an enumeration and then convert the enum to a list. But I'm looking for a solution that doesn't use the Batteries Library. In the standard OCaml Hashtbl Module there doesn't seem to be a way of extracting the pairs as a list or a way of combining its functions to achieve this purpose. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2024-10-07 06:31:53

在标准 OCaml Hashtbl 模块中似乎没有......

当然有!

val fold : ('a -> 'b -> 'c -> 'c) -> ('a, 'b) t -> 'c -> 'c

所以,使用:

fun h -> Hashtbl.fold (fun k v acc -> (k, v) :: acc) h []

In the standard OCaml Hashtbl Module there doesn't seem to be ...

Of couse there is!

val fold : ('a -> 'b -> 'c -> 'c) -> ('a, 'b) t -> 'c -> 'c

So, use:

fun h -> Hashtbl.fold (fun k v acc -> (k, v) :: acc) h []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文