Collections
Sometimes you may find yourself working with a collection of optionals that contain the same value type, e.g. List<Option<T>>
. The following methods are provided to assist you when working with collections containing optional types.
Filter
Provides the same functionality as Filter but for a collection of options. All options that do not match the provided predicate are flipped into a None
.
List<Option<string>> collection = [
Option.Some("Hello"),
Option.Some("World"),
Option.None<string>()
];
List<Option<string>> filtered = collection.Filter(x => x == "Hello");
// ^? [Option.Some("Hello"), Option.None<string>(), Option.None<string>()]
Map
Provides the same functionality as Map but for a collection of options. The same transformation will be applied to all members of the collection if they are Some
.
List<Option<string>> collection = [
Option.Some("Hello"),
Option.Some("World"),
Option.None<string>()
];
List<Option<string>> mapped = collection.Map(x => $"{x}!");
// ^? [Option.Some("Hello!"), Option.Some("World!"), Option.None<string>()]
FirstOrNone
Returns the first element of the collection that matches the predicate, or a None
if there are no matches.
List<Option<string>> collection = [
Option.Some("Hello"),
Option.Some("World")
];
Option<string> first = collection.FirstOrNone(x => x.StartsWith("H"));
// ^? Option.Some("Hello")
FirstOr
Returns the first element of the collection that matches the predicate, or the provided fallback value if there are no matches.
List<Option<string>> collection = [
Option.Some("Hello"),
Option.Some("World")
];
Option<string> first = collection.FirstOr("Victor", x => x.StartsWith("V"));
// ^? Option.Some("Victor")
FirstOrElse
Returns the first element of the collection that matches the predicate, or executes the provided factory to construct the fallback value if there are no matches.
List<Option<string>> collection = [
Option.Some("Hello"),
Option.Some("World")
];
Option<string> first = collection.FirstOrElse(() => "Victor", x => x.StartsWith("V"));
// ^? Option.Some("Victor")
Last updated
Was this helpful?