Transform
Methods that take an Option<T> and give you back an Option.
Every method here returns an Option, so the chain continues. To end it, see Consume.
Map
Option<TOut> Map<TOut>(Func<T, TOut> map)Applies a transformation to the value if there is one.
Option<string> maybeName = Option.Some("Henry Crabgrass");
Option<int> maybeLength = maybeName.Map(name => name.Length);On a None: the delegate never runs, and you get None<TOut>.
AndThen
Option<TOut> AndThen<TOut>(Func<T, Option<TOut>> map)Chains a step that itself returns an Option. Map would give you Option<Option<TOut>>; AndThen keeps it flat.
Option<string> maybeDomain = maybeSigil.AndThen(TryExtractDomain);On a None: short-circuits. Later steps never run.
Called FlatMap before 5.4.0. FlatMap was [Obsolete] through 5.x and 6.0.0 removed it, so a call to it is CS0117 rather than a warning. WM2014, the rule that reported each call site, retired with it — delete any .editorconfig entry for that id.
Filter
Option<T> Filter(Predicate<T> predicate)Keeps the value only if it passes. If it does not, you get None.
On a None: the predicate never runs.
Zip
Pairs two options into one holding a tuple.
If either side is None: you get None.
ZipWith
The same, but you combine the two values instead of getting a tuple.
If either side is None: you get None, and the delegate never runs.
ZipWith has no state overload and is not getting one — it already hands the delegate both values.
Unzip
Reverses a Zip. An extension method, in Waystone.Monads.Options.Extensions.
A component that equals its type's default is an ordinary value, so Option.Some((0, "x")).Unzip() gives (Some(0), Some("x")). This threw before 6.0.0.
And
Returns the second option, but only if the first was Some. It ignores what the first held, so it answers "did both arrive?" rather than combining them.
Evaluated eagerly. Reach for AndThen when producing the second one costs something, or when it depends on the first one's value.
Reduce
Merges two options of the same type. When both hold a value your function combines them; when only one does, you get that one back untouched.
When both are None: you get None, and the delegate never runs.
Reduce has no state overload and is not getting one.
Or
The first Some of the two.
Evaluated eagerly. Use OrElse if the fallback costs something.
OrElse
The same as Or, but the factory runs only when the receiver is None.
Xor
Exclusive or. The value comes back only if exactly one of the two is Some.
When both are Some, or both None: you get None.
Last updated
Was this helpful?