Best way to condense a list of option type down to only elements that are not none?

前端 未结 2 1914
抹茶落季
抹茶落季 2020-12-24 00:09

I\'m unexpectedly having a bit of trouble with going from a list of \'a option down to a list containing only the elements that are Some.

My initial attempt was:

2条回答
  •  自闭症患者
    2020-12-24 00:39

    Simply

    List.choose id
    

    as in

    > [Some 4; None; Some 2; None] |> List.choose id;;
    val it : int list = [4; 2]
    

    List.choose

    id

提交回复
热议问题