Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FSharp-- Shortest Way to Filter Out Nones?

Tags:

f#

Let's say i'm working with a list like:

let items = [ Some(1); None; Some(8); ];;

What is the shortest means to get only the Some values in the list?

items |> List.filter Option.isSome;;

Is that the quickest? Does using Option.isSome have any drawbacks?

like image 376
Micah Avatar asked Jul 31 '14 11:07

Micah


1 Answers

To get the values of all Some instances in a list items :: a option list you can use List.choose:

let values = List.choose id items

this will yield [1; 8] for your example.

like image 109
Random Dev Avatar answered Sep 21 '22 03:09

Random Dev