nlkl / Optional

A robust option type for C#
MIT License
903 stars 73 forks source link

Transforming IEnumerable<Option<T>> to IEnumerable<T> #36

Closed punmechanic closed 7 years ago

punmechanic commented 7 years ago

What would be the correct way to transform an IEnumerable of Option to simply an IEnumerable of T?

Let's say for example that I have a list of values and I only care about the ones that definitely have values and would like to both filter out the ones that are None as well as cause the type signature to match.

nlkl commented 7 years ago

Hi,

The currently recommended way to do this is:

var values = options.SelectMany(option => option.ToEnumerable());

However, as of version 4 which is just around the corner, this will be provided as an extension method directly:

using Optional.Collections;

...

var values = options.Flatten();

The exact name of the extension method is still something I am considering though, and it might end up as options.Values() instead, to allow a similar extension method for Option<T, TException> exceptional values (options.Exceptions()).

Hope it helps.

/Nils

punmechanic commented 7 years ago

Cool, thanks!