Thomas
Thomas

Reputation: 12107

how can I make a recursive async function in F#?

I have the following:

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

I would like to move the line:

let candles = exchange.GetCandles(instrument, interval, f, now)

to an async call:

let! candles = exchange.GetCandlesAsync(instrument, interval, f, now)

but if I wrap the whole function in an async block, the recursive function doesn't compile and I get this error:

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.

Upvotes: 0

Views: 350

Answers (1)

Random Dev
Random Dev

Reputation: 52280

I don't see your code producing the error but you must have forgotten something (after the let) - this should work:

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

notice the return and the return! for the recursive call - you need those there

Upvotes: 3

Related Questions