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:
let ga = List.filter (fun xx -> match xx with | Some(g) -> true | None -> false) gao
But of course, this result type is still 'a option list. I don't know how to use List.map to condense this, because you have to handle all cases in a match statement. I have an ugly solution, but I'm wondering if there is something better.
Ugly:
let rec gOptRemove gdec gacc = match gdec with | head :: tail -> match head with | Some(a) -> gOptRemove tail (a :: gacc) | None -> gOptRemove tail gacc | [] -> gacc
I would prefer to find a non-recursive solution or find out what the standard way is for this kind of thing.
Simply
List.choose id
as in
> [Some 4; None; Some 2; None] |> List.choose id;; val it : int list = [4; 2]
List.choose
id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With