SE-0235 - Add Result to the Standard Library

I was the one to originally suggest this function:

I mentioned it specifically because some people were having trouble understanding how you can perform certain operations that are not expressible by maps and flatMaps alone. It's basically a function version of a switch statement over the result.

And yeah you can implement map, mapError, flatMap and flatMapError in terms of fold, but many times it's more concise to use the simpler, less general function.

The same is true of reduce on arrays. You can reimplement all of array's API in terms of reduce (including map, suffix, prefix, count, filter, etc.) in terms of reduce, but of course you want probably the more specific functions most of the time. However that doesn't mean we shouldn't also have the more powerful reduce.

3 Likes