我是第一次探索 ReScript。我想使用记录类型作为我的键类型来构建一个哈希图,并且我正在寻找有关实现哈希函数的指导。
这是我的 ReScript 代码:
type pointer = { id: string, table: string, spaceId: option<string> }
module PointerHash = Belt.Id.MakeHashable({
type t = pointer
let hash = a => 0 /* How do I implement this? */
let eq = (a, b) =>
a.id == b.id &&
a.table == b.table &&
switch (a.spaceId, b.spaceId) {
| (Some(aid), Some(bid)) => aid == bid
| _ => false
}
})
我浏览了文档并在线搜索,但没有找到有关如何实现该hash
功能的任何指导。
在期望您实现的其他编程语言(例如 Java )中hashCode()
,有无处不在的工具来支持组合现有的散列函数。
class Pointer {
public final id: String
public final table: String
public final @Nullable spaceId: String
/* omitting constructor, etc */
@Override
public int hashCode() {
// Standard helper for implementing hashCode()
return Objects.hash(this.id, this.table, this.spaceId);
}
}
我查看了Belt.HashMapString 的实现,看看是否有任何提示,看起来 HashMapString 使用caml_hash_mix_string
:
external caml_hash_mix_string : seed -> string -> seed = "caml_hash_mix_string"
external final_mix : seed -> seed = "caml_hash_final_mix"
let hash (s : key) =
final_mix (caml_hash_mix_string 0 s )
访问和组合“哈希混合”函数的最惯用方式是什么?这些是否可以通过 ReScript 的漂亮界面获得?