F# 在F中,根据条件从字典中删除元素#

F# 在F中,根据条件从字典中删除元素#,f#,F#,如果我有一个可变字典,如何根据条件删除元素 我想我可以做任何一件事: myDict.Keys |> Seq.iter (fun x -> if dict.[x].criteria then dict.Delete(x) |> ignore) 或者,制作一个键列表,使用Seq.filter制作一个要删除的键列表,然后删除所有元素,等等 但是,由于我还不知道std库的所有功能,我想知道是否有一种机制可以做到这一点?当你说你有一个可变字典时,我假设你指的是System.Collec

如果我有一个可变字典,如何根据条件删除元素

我想我可以做任何一件事:

myDict.Keys |> Seq.iter (fun x -> if dict.[x].criteria then dict.Delete(x) |> ignore)
或者,制作一个键列表,使用Seq.filter制作一个要删除的键列表,然后删除所有元素,等等


但是,由于我还不知道std库的所有功能,我想知道是否有一种机制可以做到这一点?

当你说你有一个可变字典时,我假设你指的是System.Collections.Generic.dictionary,而不是一个F#immutable Dict。当我在C#中使用这种类型的字典时,我通常会得到类似于以下F#代码的代码(我在VS2019中的.fsx文件中运行了这个)

open System.Collections.Generic
让dict=newdictionary()
[1..4]|>国际热核实验堆顺序(乐趣n->dict.Add(n,n*n))
dict |>Seq.iter(printfn“%A”)
让removeKeys=dict |>Seq.filter(fun ele->ele.Value=9)|>Seq.map(fun ele->ele.Key)
移除键
removeKeys |>Seq.iter(fun k->dict.Remove(k)|>忽略)
dict |>Seq.iter(printfn“%A”)
因此,第一件事是建立一个需要删除的键列表,因为不允许在字典上迭代时删除键,然后在键上迭代以从字典中删除项

dict |> Seq.iter( fun ele -> if ele.Value=9 then dict.Remove(ele.Key) |> ignore<bool> else ())
dict |>Seq.iter(fun ele->如果ele.Value=9,则dict.Remove(ele.Key)|>忽略else())
当调用dict.Remove时,尝试上述操作会导致InvalidoOperationException(如果尝试此操作,请确保重新填充dict,只有在实际调用.Remove方法时才会失败)


作为后续问题,我建议你告诉我们为什么你认为你需要一本可变字典。在很多情况下,有一种方法允许使用F#immutable数据结构,如果真的需要,还有一些技术可以封装可变数据。

当你说你有一个可变字典时,我假设你指的是System.Collections.Generic.dictionary,而不是F#immutable Dict。当我使用这种类型的C#中的Dictionary通常以类似于以下F#代码的代码结束(我在VS2019中的.fsx文件中运行了此代码)

open System.Collections.Generic
让dict=newdictionary()
[1..4]|>国际热核实验堆顺序(乐趣n->dict.Add(n,n*n))
dict |>Seq.iter(printfn“%A”)
让removeKeys=dict |>Seq.filter(fun ele->ele.Value=9)|>Seq.map(fun ele->ele.Key)
移除键
removeKeys |>Seq.iter(fun k->dict.Remove(k)|>忽略)
dict |>Seq.iter(printfn“%A”)
因此,第一件事是建立一个需要删除的键列表,因为不允许在字典上迭代时删除键,然后在键上迭代以从字典中删除项

dict |> Seq.iter( fun ele -> if ele.Value=9 then dict.Remove(ele.Key) |> ignore<bool> else ())
dict |>Seq.iter(fun ele->如果ele.Value=9,则dict.Remove(ele.Key)|>忽略else())
当调用dict.Remove时,尝试上述操作会导致InvalidoOperationException(如果尝试此操作,请确保重新填充dict,只有在实际调用.Remove方法时才会失败)


作为后续问题,我建议你告诉我们为什么你认为你需要一本可变字典。在很多情况下,有一种方法允许使用F#immutable数据结构,如果确实需要,则有一些技术可以封装可变数据。

这不是对您问题的直接回答,但您是否考虑过使用内置F#immutable
映射
类型?在许多情况下,可变字典是一个更好的选择(它们更快),但map允许您使用
map.filter
函数精确地执行您的要求:

Map.ofSeq [ 1, "one"; 2, "two"; 3, "three" ]
|> Map.filter (fun k v -> k % 2 = 1)

这不是对您问题的直接回答,但您是否考虑过使用内置的F#immutable
Map
type?在许多情况下,可变字典是一个更好的选择(它们更快),但map允许您使用
map.filter
函数精确地执行您的要求:

Map.ofSeq [ 1, "one"; 2, "two"; 3, "three" ]
|> Map.filter (fun k v -> k % 2 = 1)
在F#中,有一种字典迭代的机制。活动识别器用于解构类型的值。在泛型词典中,这是其枚举器属性的返回类型

在.net中,有一种机制可以通过指定一个单独的键来从字典中删除元素,即方法来改变字典

其中一个是功能性的,另一个是强制性的。一种方法创建新的不可变字典,另一种方法从现有的可变字典中删除元素。任何最适合您的用例的

module Dict =
    open System.Collections.Generic

    // Create a new immutable IDictionary<_,_>, consisting of those 
    // elements for which the predicate function returns true
    let filter predicate source = 
        source |> Seq.choose (function
        | KeyValue(k, v) when predicate k v -> Some(k, v)
        | _ -> None ) |> dict

    // Mutate an existing Dictionary<'a,'b> by removing the keys of those
    // elements for which the predicate function returns true
    let removeWhen predicate (source : Dictionary<_,_>) =
        source |> Seq.choose (function
        | KeyValue(k, v) when predicate k v -> Some k
        | _ -> None )
        |> Seq.toArray
        |> Array.iter (source.Remove >> ignore)

// module Dict = begin
//   val filter :
//     predicate:('a -> 'b -> bool) ->
//       source:seq<KeyValuePair<'a,'b>> -> IDictionary<'a,'b> when 'a : equality
//   val removeWhen :
//     predicate:('a -> 'b -> bool) -> source:Dictionary<'a,'b> -> unit
// end
模块Dict=
open System.Collections.Generic
//创建一个新的不可变IDictionary,包括
//谓词函数为其返回true的元素
让筛选器谓词源=
source |>Seq.choose(函数
|谓词kv->Some(k,v)时的键值(k,v)
||>无)|>听写
//通过删除现有字典的键来改变现有字典
//谓词函数为其返回true的元素
let removeWhen谓词(来源:字典)=
source |>Seq.choose(函数
|谓词kv->Some k时的键值(k,v)
|(无)
|>序号:toArray
|>Array.iter(source.Remove>>忽略)
//模块Dict=开始
//val过滤器:
//谓词:('a->'b->bool)->
//来源:seq->IDictionary when'a:相等
//val removeWhen:
//谓词:('a->'b->bool)->来源:字典->单位
//结束
在F#中,有一种字典迭代的机制。活动识别器用于解构类型的值。在泛型词典中,这是其枚举器属性的返回类型

在.net中,有一种机制可以通过指定一个单独的键来从字典中删除元素,即方法来改变字典

其中一个是功能性的,另一个是强制性的。一种方法创建新的不可变字典,另一种方法从现有的可变字典中删除元素。任何最适合您的用例的

module Dict =
    open System.Collections.Generic

    // Create a new immutable IDictionary<_,_>, consisting of those 
    // elements for which the predicate function returns true
    let filter predicate source = 
        source |> Seq.choose (function
        | KeyValue(k, v) when predicate k v -> Some(k, v)
        | _ -> None ) |> dict

    // Mutate an existing Dictionary<'a,'b> by removing the keys of those
    // elements for which the predicate function returns true
    let removeWhen predicate (source : Dictionary<_,_>) =
        source |> Seq.choose (function
        | KeyValue(k, v) when predicate k v -> Some k
        | _ -> None )
        |> Seq.toArray
        |> Array.iter (source.Remove >> ignore)

// module Dict = begin
//   val filter :
//     predicate:('a -> 'b -> bool) ->
//       source:seq<KeyValuePair<'a,'b>> -> IDictionary<'a,'b> when 'a : equality
//   val removeWhen :
//     predicate:('a -> 'b -> bool) -> source:Dictionary<'a,'b> -> unit
// end
模块Dic