gpt4 book ai didi

f# - 试图了解F#事件模式,为什么我可以这样做:

转载 作者:行者123 更新时间:2023-12-04 05:04:14 24 4
gpt4 key购买 nike

我有一个Dictionary,因此最初对其进行了迭代:
myDictionary |> Seq.iter (fun kvp -> doSomething kvp.Key kvp.Value)
后来,我发现我可以利用KeyValue事件模式,并执行以下操作:
myDictionary |> Seq.iter (fun (KeyValue (k, v)) -> doSomething k v)
知道事件模式不是某种形式的预处理器指令,我如何用lambda中的kvp参数代替分解它的函数?

最佳答案

函数参数调用始终使用模式匹配进行解构。例如:

let getSingleton = fun [x] -> x
let getFirst = fun (a,b) -> a
let failIfNotOne = fun 1 -> ()
let failIfNeitherOne = fun (x,1 | 1,x) -> ()

在语义上, fun -> 大致等同于
fun x -> match x with | -> | _ -> raise MatchFailureException(...)

关于f# - 试图了解F#事件模式,为什么我可以这样做:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com