i have something like
type A =
| X of string
| Y of int
i have a sequence of X types, [X "foo"; X "boo"; X "moo"]
is there a shortcut for doing a map to convert it to ["foo"; "boo"; "moo"] without doing a match?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I wouldn’t generally use a solution that doesn’t handle all cases of the pattern matching (e.g. when using
fun (X str) -> .... It is always a good idea to add handler, even if it just reported a more informative error (such as, that the case was unexpected because it was filtered before).You can extend kvb’s solution using the
functionsyntax (which is likefunwith pattern matching):Alternatively, if you wanted to ignore
Yvalues (so that[X "a"; Y 1; X "b"]becomes["a"; "b"]) you can useList.choosefunction:To write this using list comprehensions, you’d need to use full-blown pattern matching using
match, so it would be a bit longer than using higher-order functions.