4

我在 F# 中使用第三方供应商的 API。初始化时,API 返回一个嵌套 msg 容器的 C# 对象。它填充了状态消息,并且可能包含错误消息。供应商提供了我已移植 F# 的 C# 示例解析例程。

代码示例循环通过嵌套的 msg 容器提取致命和非致命错误,然后返回类型的元组列表BBResponseType * string

响应枚举:

type BBResponseType =
    | Status = 0
    | Data = 1
    | Error = 2
    | FatalError = -1 

我的 F# 端口如下所示:

 member private this.ProcessStatusMsg(eventObj: Blpapi.Event) = 
     let responseLst = List<(BBResponseType * string)>()
     for msg in eventObj do
         if msg.MessageType.Equals(SUBSTARTED) then
             if msg.GetElement(EXCEPTIONS).NumValues > 0 then // <- check for errors/exceptions                        
                 let e = msg.GetElement(EXCEPTIONS)
                 for i in 0..e.NumValues-1 do                                
                     let error = e.GetValueAsElement(i)
                     let field = error.GetElementAsString(FieldID)
                     let reason = error.GetElement(REASON)
                     let message = sprintf "Subscription Started w/errors( Field:   %s \n   Reason:   %s)" field (reason.GetElementAsString(DESCRIPTION))                                
                     responseLst.Add(BBResponseType.Error, message)
             else                                
                 let message = sprintf "Subscription Started"         
                 responseLst.Add(BBResponseType.Status, message)

         if msg.MessageType.Equals(SUBSCFAILURE) then // <- check for subscriptions failure
             if msg.HasElement(REASON) then
                 let reason = msg.GetElement(REASON)
                 let desc = reason.GetElementAsString(DESCRIPTION)
                 let message = sprintf "Real-time Subscription Failure:    %s" desc                            
                 responseLst.Add(BBResponseType.FatalError, message)
             else
                 let message = sprintf "Subscription Failure:  (reason unknown) "                                                
                 responseLst.Add(BBResponseType.FatalError, message)
     responseLst

完成后,我看着它并想,“哇,这几乎是你所能得到的没有功能的东西,而且仍然在 F# 中编码。”

它看起来确实比 C# 版本更清晰和简洁,但我认为必须有更好的方法来完成这一切,而无需使用这么多循环和 if/then's。

如何使用模式匹配和递归更好地解析这些嵌套结构?

4

2 回答 2

6

几点建议:

  1. 而不是返回一个元组列表,而是返回一个元组的序列 - 使用seq { }计算表达式来创建序列。
  2. 将 if/else 部分提取为类型函数并在表达式中Message -> (BBResponseType * string)使用此函数seq
  3. 在这个新函数(将 Message 转换为元组)内部,使用模式匹配来确定要返回的 (BBResponseType * string) 类型。
于 2013-08-02T06:30:26.673 回答
3

为了补充@Ankur的回答:

member private this.ProcessStatusMsg(eventObj: Blpapi.Event) =
    // 0. Define a parameterized active pattern to turn if/else into pattern matching
    let (|Element|_|) e msg =
        if msg.HasElement(e) then
            Some <| msg.GetElement(e)
        else None

    // 1. Wrapping the whole method body in a sequence expression 
    seq {
        for msg in eventObj do
            // 2. Extracting if/else part and using it in sequence expression
            match msg.MessageType with
            // 3. Using pattern matching to figure out what kind (BBResponseType * string) to return
            | SUBSTARTED ->
                match msg with
                // 4. Use active pattern to pattern match on message directly
                | Element EXCEPTIONS e when e.NumValues > 0 ->
                    for i in 0..e.NumValues-1 do                                
                        let error = e.GetValueAsElement(i)
                        let field = error.GetElementAsString(FieldID)
                        let reason = error.GetElement(REASON)
                        let message = sprintf "Subscription Started w/errors( Field:   %s \n   Reason:   %s)" field (reason.GetElementAsString(DESCRIPTION))                                
                        yield (BBResponseType.Error, message)
                | _ ->                                
                    let message = sprintf "Subscription Started"         
                    yield (BBResponseType.Status, message)

            | SUBSCFAILURE ->
                match msg with
                | Element REASON reason ->
                    let desc = reason.GetElementAsString(DESCRIPTION)
                    let message = sprintf "Real-time Subscription Failure:    %s" desc                            
                    yield (BBResponseType.FatalError, message)   
                | _ ->
                    let message = sprintf "Subscription Failure:  (reason unknown) "                                                
                    yield (BBResponseType.FatalError, message)

            // There are probably more cases, processing them here
            | _ -> ()
    }

评论中的第 1、2 和 3 点来自另一个答案。我添加了第 0 点和第 4 点以使用活动模式来轻松进行模式匹配。

于 2013-08-05T07:12:50.990 回答