如何有效地对列表/序列进行分组/筛选的快速问题。
- 筛选仅包含非空可选字段的记录
- 删除"option"参数,以使将来的处理更容易(因为None已被筛除)
- 分组(我认为这没有问题)
我是否采用了最佳方法?
谢谢!
type tmp = {
A : string
B : int option }
type tmp2 = {
A : string
B : int }
let inline getOrElse (dft: 'a) (x: 'a option) =
match x with
| Some v -> v
| _ -> dft
let getGrouped (l: tmp list) =
l |> List.filter (fun a -> a.B.IsSome)
|> List.map (fun a -> {A = a.A ; B = (getOrElse 0 (a.B)) })
|> List.groupBy (fun a -> a.A)