使用 F# 和 Bloomberg 累积每个中间递归调用的结果 API
Accumulate results of each intermediate recursive call using F# and Bloomberg API
我正在使用 B-Pipe API 从 Bloomberg 请求金融证券数据。大型请求被分成小组并返回为 PARTIAL_RESPONSE
,最终响应为 RESPONSE
。递归 handleSingleEvent
函数递归直到 reqEventQueue.NextEvent()
.
返回最终的 RESPONSE
我的问题是只有最后 RESPONSE
组证券被退回。函数不返回中间 PARTIAL_RESPONSE
组。但是,这些 PARTIAL_RESPONSE
个组是从彭博社收到的。
我想我可能需要一些累加器参数或者应该使用类似于 List.collect 的东西,但我不知道如何开始。
// list<Request> * EventQueue * Session -> list<list<string>>
let handleEvents (requests: Request list, reqEventQueue: EventQueue, session: Session) =
let rec handleSingleEvent (request: Request) =
try
let eventObj = reqEventQueue.NextEvent()
match eventObj.Type with
| Event.EventType.PARTIAL_RESPONSE ->
processReferenceResponseEvent eventObj
|> makeJson
|> ignore
handleSingleEvent request
| Event.EventType.RESPONSE -> processReferenceResponseEvent eventObj |> makeJson
| _ -> processMiscEvents eventObj |> makeJson
with
| ex -> failwithf "%s" ex.Message
List.map (fun request -> handleSingleEvent request) requests
更新
我进行了编辑,但现在代码似乎进入了无限递归。
let handleEvents (requests: Request list, reqEventQueue: EventQueue, session: Session) =
let rec handleSingleEvent (request: Request) : seq<list<string>> =
seq {
let eventObj = reqEventQueue.NextEvent()
match eventObj.Type with
| Event.EventType.REQUEST_STATUS -> yield processMiscEvents eventObj |> makeJson
| Event.EventType.ADMIN -> yield processAdminEvent eventObj |> makeJson
| Event.EventType.AUTHORIZATION_STATUS -> yield processAuthEvent eventObj session |> makeJson
| Event.EventType.PARTIAL_RESPONSE ->
yield processReferenceResponseEvent eventObj
|> makeJson
yield! handleSingleEvent request
| Event.EventType.RESPONSE -> yield processReferenceResponseEvent eventObj |> makeJson
| _ -> yield processMiscEvents eventObj |> makeJson
} |> ignore
handleSingleEvent request
List.map (fun request -> handleSingleEvent request) requests
我不熟悉 Bloomberg API,但这是一种相当常见的模式。我认为最简单的处理方法是递归地生成一个 JSON 字符串序列,使用 yield
处理当前事件,使用 yield!
递归处理队列的剩余部分。这样您就不必担心手动累积结果。所以像这样:
let rec loop () : seq<string> = // assumption: makeJson has type _ -> string
seq {
let eventObj = reqEventQueue.NextEvent()
match eventObj.Type with
| Event.EventType.PARTIAL_RESPONSE ->
yield processReferenceResponseEvent eventObj
|> makeJson
yield! loop ()
| Event.EventType.RESPONSE ->
yield processReferenceResponseEvent eventObj
|> makeJson
| _ ->
yield processMiscEvents eventObj
|> makeJson
}
loop ()
您的代码中还有一些我没有尝试解决的其他问题,例如根本没有使用 request
和 session
值这一事实。我想你可以很容易地解决这个问题。
我正在使用 B-Pipe API 从 Bloomberg 请求金融证券数据。大型请求被分成小组并返回为 PARTIAL_RESPONSE
,最终响应为 RESPONSE
。递归 handleSingleEvent
函数递归直到 reqEventQueue.NextEvent()
.
RESPONSE
我的问题是只有最后 RESPONSE
组证券被退回。函数不返回中间 PARTIAL_RESPONSE
组。但是,这些 PARTIAL_RESPONSE
个组是从彭博社收到的。
我想我可能需要一些累加器参数或者应该使用类似于 List.collect 的东西,但我不知道如何开始。
// list<Request> * EventQueue * Session -> list<list<string>>
let handleEvents (requests: Request list, reqEventQueue: EventQueue, session: Session) =
let rec handleSingleEvent (request: Request) =
try
let eventObj = reqEventQueue.NextEvent()
match eventObj.Type with
| Event.EventType.PARTIAL_RESPONSE ->
processReferenceResponseEvent eventObj
|> makeJson
|> ignore
handleSingleEvent request
| Event.EventType.RESPONSE -> processReferenceResponseEvent eventObj |> makeJson
| _ -> processMiscEvents eventObj |> makeJson
with
| ex -> failwithf "%s" ex.Message
List.map (fun request -> handleSingleEvent request) requests
更新 我进行了编辑,但现在代码似乎进入了无限递归。
let handleEvents (requests: Request list, reqEventQueue: EventQueue, session: Session) =
let rec handleSingleEvent (request: Request) : seq<list<string>> =
seq {
let eventObj = reqEventQueue.NextEvent()
match eventObj.Type with
| Event.EventType.REQUEST_STATUS -> yield processMiscEvents eventObj |> makeJson
| Event.EventType.ADMIN -> yield processAdminEvent eventObj |> makeJson
| Event.EventType.AUTHORIZATION_STATUS -> yield processAuthEvent eventObj session |> makeJson
| Event.EventType.PARTIAL_RESPONSE ->
yield processReferenceResponseEvent eventObj
|> makeJson
yield! handleSingleEvent request
| Event.EventType.RESPONSE -> yield processReferenceResponseEvent eventObj |> makeJson
| _ -> yield processMiscEvents eventObj |> makeJson
} |> ignore
handleSingleEvent request
List.map (fun request -> handleSingleEvent request) requests
我不熟悉 Bloomberg API,但这是一种相当常见的模式。我认为最简单的处理方法是递归地生成一个 JSON 字符串序列,使用 yield
处理当前事件,使用 yield!
递归处理队列的剩余部分。这样您就不必担心手动累积结果。所以像这样:
let rec loop () : seq<string> = // assumption: makeJson has type _ -> string
seq {
let eventObj = reqEventQueue.NextEvent()
match eventObj.Type with
| Event.EventType.PARTIAL_RESPONSE ->
yield processReferenceResponseEvent eventObj
|> makeJson
yield! loop ()
| Event.EventType.RESPONSE ->
yield processReferenceResponseEvent eventObj
|> makeJson
| _ ->
yield processMiscEvents eventObj
|> makeJson
}
loop ()
您的代码中还有一些我没有尝试解决的其他问题,例如根本没有使用 request
和 session
值这一事实。我想你可以很容易地解决这个问题。