将哈希表转换为OCaml中的对(键,值)列表 [英] Converting hash table to list of pairs (key,value) in OCaml

查看:147
本文介绍了将哈希表转换为OCaml中的对(键,值)列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将哈希表转换为OCaml中的(键,对)值列表?

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

我知道,只要有哈希表ht,我们就可以做到

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

BatList.of_enum (BatHashtbl.enum ht)

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

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?

推荐答案

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

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

当然有!

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

因此,使用:

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

这篇关于将哈希表转换为OCaml中的对(键,值)列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆