如何在 F# 中创建递归异步函数?
how can I make a recursive async function in F#?
我有以下内容:
let getCandlesFrom (exchange: IExchange) (instrument: Instrument) (interval: TimeSpan) (fromTime: DateTime) =
let rec get (c: CandleData array) (f: DateTime) =
let now = DateTime.UtcNow
//info $"requesting {f} - {now}"
let candles = exchange.GetCandles(instrument, interval, f, now)
if candles.IsError then
failwith candles.GetError.Describe
else
//info $"received data {candles.Get.[0].Timestamp} - {candles.Get.[^0].Timestamp}"
let c = Array.append c candles.Get
if c.[^0].Timestamp < now - interval then
get c (c.[^0].Timestamp + interval)
else
c
get [||] fromTime
我要移线:
let candles = exchange.GetCandles(instrument, interval, f, now)
异步调用:
let! candles = exchange.GetCandlesAsync(instrument, interval, f, now)
但是如果我将整个函数包装在一个异步块中,递归函数不会编译,我会得到这个错误:
DataSource.fs(14, 13): [FS0588] The block following this 'let' is unfinished. Every code block is an expression and must have a result. 'let' cannot be the final code element in a block. Consider giving this block an explicit result.
我没有看到您的代码产生错误,但您一定是忘记了什么(在 let
之后)- 这应该有效:
let getCandlesFrom (exchange: IExchange) (instrument: Instrument) (interval: TimeSpan) (fromTime: DateTime) =
let rec get (c: CandleData array) (f: DateTime) =
asnyc {
let now = DateTime.UtcNow
//info $"requesting {f} - {now}"
let! candles = exchange.GetCandlesAsync(instrument, interval, f, now)
if candles.IsError then
return (failwith candles.GetError.Describe)
else
let c = Array.append c candles.Get
if c.[^0].Timestamp < now - interval then
return! get c (c.[^0].Timestamp + interval)
else
return c
}
get [||] fromTime
注意递归调用的 return
和 return!
- 你需要那些
我有以下内容:
let getCandlesFrom (exchange: IExchange) (instrument: Instrument) (interval: TimeSpan) (fromTime: DateTime) =
let rec get (c: CandleData array) (f: DateTime) =
let now = DateTime.UtcNow
//info $"requesting {f} - {now}"
let candles = exchange.GetCandles(instrument, interval, f, now)
if candles.IsError then
failwith candles.GetError.Describe
else
//info $"received data {candles.Get.[0].Timestamp} - {candles.Get.[^0].Timestamp}"
let c = Array.append c candles.Get
if c.[^0].Timestamp < now - interval then
get c (c.[^0].Timestamp + interval)
else
c
get [||] fromTime
我要移线:
let candles = exchange.GetCandles(instrument, interval, f, now)
异步调用:
let! candles = exchange.GetCandlesAsync(instrument, interval, f, now)
但是如果我将整个函数包装在一个异步块中,递归函数不会编译,我会得到这个错误:
DataSource.fs(14, 13): [FS0588] The block following this 'let' is unfinished. Every code block is an expression and must have a result. 'let' cannot be the final code element in a block. Consider giving this block an explicit result.
我没有看到您的代码产生错误,但您一定是忘记了什么(在 let
之后)- 这应该有效:
let getCandlesFrom (exchange: IExchange) (instrument: Instrument) (interval: TimeSpan) (fromTime: DateTime) =
let rec get (c: CandleData array) (f: DateTime) =
asnyc {
let now = DateTime.UtcNow
//info $"requesting {f} - {now}"
let! candles = exchange.GetCandlesAsync(instrument, interval, f, now)
if candles.IsError then
return (failwith candles.GetError.Describe)
else
let c = Array.append c candles.Get
if c.[^0].Timestamp < now - interval then
return! get c (c.[^0].Timestamp + interval)
else
return c
}
get [||] fromTime
注意递归调用的 return
和 return!
- 你需要那些