Map
Use a non-Optional function in a chain.
Functional or fluent?
Once we have used Some<T>(T) -> Option<T>
to lift a value into the world of Option<T>
, we need to do something with it by chaining our functions together. The Option<T>
class comes with a load of methods which are actually wrappers for the functions that do the work.
So, although you can use the functions directly, you'll find it much more convenient to write chains using the common C# fluent syntax. These two are functionally identical:
In that snippet, functional
and fluent
are both Some<string>
with Value "42"
.
From now on, I will give function signatures as their equivalent method on Option<T>
because that should help keep things a little clearer, and it's what you will actually use.
Map<U>(Func<T, U>, Handler) -> Option<U>
Map<U>(Func<T, U>, Handler) -> Option<U>
Map
does a switch
and behaves like this:
if the input
Option<T>
isNone<T>
, returnNone<U>
with the original Reasonif the input
Option<T>
isSome<T>
...get the Value
T
use that as the input and execute
Func<T, U>
, then wrap the result inSome<U>
catch any exceptions using
Handler
See it in action here:
If you are using LINQPad to to you change the second Map
in that sample so you have x / 0
instead of x / 2
and re-run the snippet, you will see that you still end up with an Option<string>
, only this time it's None<string>
with a DivisionFailedMsg
as the Reason.
There are two additional things to note here:
DefaultHandler
is available for you to use, but you should use it sparingly, and not rely on it, unless you really don't care about having helpful messages.AuditSwitch(Action<T> some, Action<IMsg> none)
is useful for logging - the 'some' action receives the current Value (if the input isSome<T>
) and the 'none' action receives the current Reason (if the input isNone<T>
).
Last updated