Array.choose用于元组的元素

dem*_*mas 2 f#

这是我目前的代码:

placeId
|> Sql.getEOByPlace
|> Array.map (fun eo -> (eo, getType eo))  // (Item * ItemType option)
|> Array.filter (fun (i, it) -> it.IsSome)
|> Array.map (fun (i, it) -> (i, it.Value) // (Item * ItemType)
Run Code Online (Sandbox Code Playgroud)

有没有办法简化最后三行代码?我需要类似的东西Array.choose,但是对于元组的一个元素.

Lee*_*Lee 5

你可以返回一个(a * b) option而不是一个a * b option并使用Array.choose:

placeId
|> Sql.getEOByPlace
|> Array.choose (fun eo -> (getType eo) |> Option.map (t -> (eo, t)))
Run Code Online (Sandbox Code Playgroud)