Functional extensions for result
In an earlier post, I created Result<TValue, TError>, a generic, allocation-free struct that lets code avoid using exceptions for control flow. On its own, it supports only a procedural style, where each call is followed by a check of Success or Failure. A few steps in, most of the code is branching. Functional programming offers a way to hide the branches from the calling code. Instead of checking a result, the code composes operations on it. The operations do the checking. F# ships a Result module for exactly that. In this post I implement C# counterparts of its functions for Result<TValue, TError> as extension methods: map, mapError, bind, iter, iterError, defaultValue and defaultWith. I also add Match, an addition of my own. With these methods, steps can be chained without checking Success or Failure after each call. ...