margareta.dev

F# Traverse on Result

May 03, 2020

Performing side effects in a loop with saving result from each one of them in a resulting list:

let createOrders (userId : int) (orders : Order seq) : Result<unit, ApiError> =
    seq {
        for order in orders ->
            orderService.CreateOrder userId order
    }
    |> Result.foldResultList
    |> Result.ignore

let processOrders (userId : int) (orders : Order seq) : Result<UserHistory, ApiError> =
    result {
        do! createOrders userId orders
        return! orderService.GetUserHistory userId
    }