我目前正在以类似的方式生成一个序列:
migrators
|> Seq.map (fun m -> m())
migrator
函数最终返回一个区分联合类型,例如:
type MigratorResult =
| Success of string * TimeSpan
| Error of string * Exception
我希望在遇到第一个
Error
时停止map
,但我需要在最终序列中包含Error
。我有以下代码来向用户显示最终消息。
match results |> List.rev with
| [] -> "No results equals no migrators"
| head :: _ ->
match head with
| Success (dt, t) -> "All migrators succeeded"
| Error (dt, ex) -> "Migration halted owing to error"
所以我需要:
- 当映射步骤中的一个产生
Error
时停止映射的方法 - 将该错误作为最终添加到序列中的元素的方法
我知道可能有其他顺序方法可以做到这一点,但我对 F# 还不熟悉,在网上搜索还没有得到任何结果!