f# - F#中的哈希表

标签 f# hashmap hashtable

是否有替代 System.Collections.Generic.Dictionary 的替代方案?或 System.Collections.Hashtable ?

我对前者不满意,因为它使用 byref 返回值,即,我需要做烦人的

let x = ref ""
if hashtable.TryGetValue (key, x) then
    // Found, value in !x
else
    // Not found. 

我对后者不满意,因为它不是通用的。

编辑。我更喜欢在语法上看起来像 Map.tryFind 的东西, IE。,
match Hashtable.tryFind k hashtable with
| None -> ...    // Not found
| Some v -> ...  // Found v. 

最佳答案

输出参数是 .NET 框架的一部分。然而,F# 确实通过自动将它们与返回值一起进行元组化来最大程度地减少痛苦。因此,使用 Dictionary<_,_>你可以做:

match d.TryGetValue(key) with
| true, x -> ... //tuple of return value and out parameter
| _ -> ...

Passing by Reference on MSDN .

您可以轻松地将其包装到扩展名中:
type System.Collections.Generic.Dictionary<'K, 'V> with
  member x.TryFind(key) =
    match x.TryGetValue(key) with
    | true, v -> Some v
    | _ -> None

关于f# - F#中的哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921725/

相关文章:

java - 如何根据负载因子对 ConcurrentHashMap 中的元素进行分组

java - 获取 HashMap 中具有未知键的第一个值

reflection - F# 反射 : Passing an Array as Argument to MethodInfo. 调用

F# unbox<int> 返回 obj

java - 从 HashMap 中获取前 k 个值

c++ - 在 C++ 中删除哈希表

dictionary - 为什么 F#'s idiomatic dictionary collection (Map<K,V>) needs the type K to implement comparable while C#' s Dictionary<K,V> 没有?

java - 在 Java 中存储预排序键值对的内置方法?

.net - F# csv 类型提供者问题

visual-studio-2012 - 使用 Visual Studio 编译时如何设置额外的 F# 编译器标志?