Functional programming F忽略模式匹配中的模式

Functional programming F忽略模式匹配中的模式,functional-programming,f#,pattern-matching,Functional Programming,F#,Pattern Matching,我可能想得不对,但我想忽略除某个案例以外的任何案例。下面是我正在使用的一些示例代码| u824;->忽略,但这似乎是错误的。有没有更好或更惯用的方法来做到这一点?我正在将一些OOP C代码转换为F,可能会出现错误 match solarSystem.MinerCoords |> Map.tryFind minerId with | Some currentMinerCoords -> match solarSystem.Minables |> Map.tryFind c

我可能想得不对,但我想忽略除某个案例以外的任何案例。下面是我正在使用的一些示例代码| u824;->忽略,但这似乎是错误的。有没有更好或更惯用的方法来做到这一点?我正在将一些OOP C代码转换为F,可能会出现错误

match solarSystem.MinerCoords |> Map.tryFind minerId with
| Some currentMinerCoords ->
    match solarSystem.Minables |> Map.tryFind currentMinerCoords with
    | Some _ ->
        do! GetMinerActor(minerId).StopMining() |> Async.AwaitTask
    | _ -> ignore
| _ -> ignore

看起来您在一个返回async的异步计算表达式中。因此,应将ignore替换为return,其中为单位值,以便所有分支返回相同的类型:

match solarSystem.MinerCoords |> Map.tryFind minerId with
| Some currentMinerCoords ->
    match solarSystem.Minables |> Map.tryFind currentMinerCoords with
    | Some _ ->
        do! GetMinerActor(minerId).StopMining() |> Async.AwaitTask
    | _ -> return ()
| _ -> return ()
编辑:显示整个异步块的简化版本,以及如何在以后继续运行更多代码:

async {
    match Some 1 with
    | Some a ->
        printfn "Maybe do this"
        do! Async.Sleep 500
    | _ -> ()

    printfn "Always do this"
    do! Async.Sleep 500
    printfn "Finished" }

看起来您在一个返回async的异步计算表达式中。因此,应将ignore替换为return,其中为单位值,以便所有分支返回相同的类型:

match solarSystem.MinerCoords |> Map.tryFind minerId with
| Some currentMinerCoords ->
    match solarSystem.Minables |> Map.tryFind currentMinerCoords with
    | Some _ ->
        do! GetMinerActor(minerId).StopMining() |> Async.AwaitTask
    | _ -> return ()
| _ -> return ()
编辑:显示整个异步块的简化版本,以及如何在以后继续运行更多代码:

async {
    match Some 1 with
    | Some a ->
        printfn "Maybe do this"
        do! Async.Sleep 500
    | _ -> ()

    printfn "Always do this"
    do! Async.Sleep 500
    printfn "Finished" }

您可以使每个产生异步的分支,然后您可以执行它。像这样:

let dummyAsync = async { return () }
let theAsync =
    match solarSystem.MinerCoords |> Map.tryFind minerId with
    | Some currentMinerCoords when solarSystem.Minables |> Map.tryFind currentMinerCoords |> Option.isSome ->
        GetMinerActor(minerId).StopMining() |> Async.AwaitTask
    | _ ->
        dummyAsync
do! theAsync
注意使用when关键字删除一个不必要的分支

更习惯的做法是,当您以嵌套方式匹配多个选项值时,应使用函数option.bind和/或option.map:


您可以使每个产生异步的分支,然后您可以执行它。像这样:

let dummyAsync = async { return () }
let theAsync =
    match solarSystem.MinerCoords |> Map.tryFind minerId with
    | Some currentMinerCoords when solarSystem.Minables |> Map.tryFind currentMinerCoords |> Option.isSome ->
        GetMinerActor(minerId).StopMining() |> Async.AwaitTask
    | _ ->
        dummyAsync
do! theAsync
注意使用when关键字删除一个不必要的分支

更习惯的做法是,当您以嵌套方式匹配多个选项值时,应使用函数option.bind和/或option.map:


忽视需要争论。您在这里返回整个函数。您应该改为使用single,或者在CE中返回。ignore需要一个参数。您在这里返回整个函数。你应该用单曲代替,或者在CE中返回。好的,这是有道理的。你说得对,我在一个异步块中。如果我不想在该点返回或结束函数,该怎么办?我想继续下一部分的工作function@LukeP对于Async,实际上不需要返回单元,只需编写。但无论您使用哪一个,您都可以在下面编写更多的代码。看我编辑过的答案好的,有道理。你说得对,我在一个异步块中。如果我不想在该点返回或结束函数,该怎么办?我想继续下一部分的工作function@LukeP对于Async,实际上不需要返回单元,只需编写。但无论您使用哪一个,您都可以在下面编写更多的代码。查看我编辑的答案我试图采用更惯用的方法,但我遇到了这个错误:类型'Async Map.tryFind coords |>Option.Map fun miners->miners |>List.Map fun id->GetMinerActorid.StartMiningminableId |>Async.AwaitTask |>Option.defaultValue DummySync``看起来他们应该匹配,虽然DummySync不是一个列表,请删除[并且]我认为前面的选项.map fun/*一些代码*/|>Async.AwaitTask创建了它,所以我需要将DummySync设置为一个列表,因为它返回了一个异步列表?编辑此代码用于不同的代码段,而不是您回答原始问题的代码forNo,Option.map不返回列表,它返回传递函数内返回的值的选项值。我尝试采用更惯用的方法,但遇到了以下错误:“Async Map.tryFind coords |>option.Map fun miners->miners |>List.Map fun id->GetMinerActorid.StartMiningminableId |>Async.Task”|>Option.defaultValue DummySync``虽然DummySync不是一个列表,但类型看起来应该匹配,请删除[并且]我认为前面的Option.map fun/*一些代码*/|>Async.AwaitTask创建了它,所以我需要将DummySync设置为一个列表,因为它返回了一个异步列表?编辑这是为不同的代码段编写的,而不是您回答原始问题的代码。forNo,Option.map不返回列表,它返回传递函数内返回的值的选项值。